[thelist] Query selected

Martin Kuplens-Ewart martin at takingitglobal.org
Tue Mar 4 07:41:09 CST 2003

>Can anyone tell me why this 'selected' isn't working?  It does
>keep a fixed id selected but not the one that was selected and
>aslo doesn't return any results?
>echo "<select name=\"CityID\" size=\"1\" class='menuForm'>";
>$result=mysql_query("SELECT City, CityID FROM city ORDER BY
>City"); while ($row = mysql_fetch_array($result))
>    {
>        $city_id=$row['CityID'];
>		$city=$row['City'];
>echo "<option value=\"$city_id\" selected> $city </option>";
>echo "</select>";

1st off - I dunno why ppl still echo out html - so mucky, and all those

2nd: you're setting each and every option as selected! I'm guessing that
what you're seeing is the last option constantly selected...

I'm not sure what array you're puling the person's selections in thru but
what you wanna do is, er, using your print html stuff...

if ($city_id == $user[CityID]) {
	echo "<option value=\"$city_id\" selected> $city </option>";
} else {
	echo "<option value=\"$city_id\"> $city </option>";

If you weren't echoing the html like that, it'd be easier still..

//while stuff...
	<option value="<?=$city_id?>"<? if ($city_id == $user[CityID]) { ?>
selected<? } ?>> <?=$city?> </option>
//end while, etc.

More information about the thelist mailing list