I have a drop down box like this:

<select name="DateDropDown">
<option value="2010 selected">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
</select>

I use the value that the user selects to refresh the page and perform a query on a database. Is there a better way than the following to keep the drop down box in the correct position?

<php?
echo '<select name="DateDropDown">';
if($MyDate = '2010')
{
echo '<option value="2010 selected">2010</option>';
}
else
{
echo '<option value="2010">2010</option>';
}

... same for each entry ...

</select>
?>

?

Thanks,

David

<select name="DateDropDown">
    <option value="2010" selected="selected">2010</option>
    <option value="2009">2009</option>
    <option value="2008">2008</option>
    </select>

im not sure if this is what you mean. :D

Since you are using a date drop down and using PHP will it not be easier if use a for loop? Also you can check if the $MyDate==$year option and set the selected='selected' statements. Check the code below.

<select name="DateDropDown">
<?php
	$myDate=$_GET['year'];
	for ($year=2010;$year>=2006;$year--){
		if ($myDate==$year){
			$s="selected='selected'";
		}
		else{
			$s="";
		}
		echo "<option value='$year' $s>$year</option>";	
	}
?>
</select>

Also note that in HTML what ivawafoo said is the correct syntax for the selection and in PHP you start of a PHP code with <?php not <php?.

You can test the above code by using the ?year=2006 URI.

sudeepjd, yep - that'll do it. You're right, I had typed the code directly in Daniweb and the ? is indeed in the wrong place - just a typo.

Thanks,
David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.