Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.