Jump to content

select field echo selected


fife

Recommended Posts

hi

 

Im trying to make this select drop down echo selected but with no joy.  Please help

 


$qGetCounty = "SELECT * FROM Counties ORDER BY FIELD(country,'England','Wales','Scotland'), county ";
$rGetCounty = mysql_query($qGetCounty);

echo "<select name=\"county\">";
$PrevCountry = "";
while ($county = mysql_fetch_assoc($rGetCounty))
{
	if ($county['country'] != $PrevCountry)
	{
		$PrevCountry = $county['country'];
		echo "<optgroup label='$PrevCountry'>";
	}
//MyDetails is the array

	echo " <option " . ($selected ? "selected=\"selected\"$MyDetails['county']" : "") . " value= '{$county['county']}'>{$county['county']}</option>";
}
echo "</select>" ;

Link to comment
https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/
Share on other sites

correction

code change but still not working

$qGetCounty = "SELECT * FROM Counties ORDER BY FIELD(country,'England','Wales','Scotland'), county ";
$rGetCounty = mysql_query($qGetCounty);

echo "<select name=\"county\">";
$PrevCountry = "";
while ($county = mysql_fetch_assoc($rGetCounty))
{
	if ($county['country'] != $PrevCountry)
	{
		$PrevCountry = $county['country'];
		echo "<optgroup label='$PrevCountry'>";
	}
	$selected = ($row_ClubDetails['county']) ? ' selected="selected"' : '';
	echo " <option value= '\"$i\"$selected'>{$county['county']}</option>";
}
echo "</select>" ;

 

You're treating $row_ClubDetails['county'] as a boolean. You should be comparing it to whatever's in the value= attribute of the tag. You also have included the $selected variable within the quotes for the value= attribute, which it should not be. And where is the value of $i coming from?

 

	$selected = ($row_ClubDetails['county']) ? ' selected="selected"' : ''; // No comparison to another value?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.