fife Posted July 8, 2011 Share Posted July 8, 2011 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>" ; Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/ Share on other sites More sharing options...
fife Posted July 8, 2011 Author Share Posted July 8, 2011 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>" ; Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/#findComment-1240104 Share on other sites More sharing options...
fife Posted July 8, 2011 Author Share Posted July 8, 2011 oh and the $i was removed. sorry Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/#findComment-1240107 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 have you debugged your mysql query? what are your actual results? Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/#findComment-1240109 Share on other sites More sharing options...
fife Posted July 8, 2011 Author Share Posted July 8, 2011 yes the query works fine as i am displaying other information from the same query. With the current code however if I click update it insert into county.... "" Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/#findComment-1240111 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2011 Share Posted July 8, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241415-select-field-echo-selected/#findComment-1240126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.