mfallon Posted March 23, 2009 Share Posted March 23, 2009 Hi, I have a page which updates a value in my DB for a record. The value is selectable from a drop down list however I have no way of defaulting the drop down list to the current value in the DB. Can anyone tell me if this is possible, and if so how. </table> <tr> <td align="right"><strong>Current Status: </strong></td> <td><? echo $req_sid_info['stype']; ?></td> </tr> <tr> <td align="right"><strong>New Status: </strong></td> <td><select name="Status" id="Status"> <option value="1" selected="selected">Verified</option> <option value="0">Unverified</option> </select></td> </tr> </table> The list I have provided is just a basic one. I guess I could do it by using an if value = 0{ else value = 1{ but I would rather not do this as I have a couple of very large drop down lists and it would make it much more complex. Thanks in advance for the help. Matt Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/ Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 in your current syntax i think the only way to do it would be to have an IF statement check the current value with the value in the database and if it matches, echo "selected=selected" the quicker way to change it would be to have all of the possible options in an array and then a foreach loop to go through the array and check each one to see if it matches Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-791935 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 Here's the code I use, as an example I'll populate a list of countries... $htmCountries=''; $query=mysql_query("SELECT id,country FROM countries ORDER BY country ASC"); while ($row=mysql_fetch_assoc($query)) { $htmCountries.='<option value="'.$row['id'].'"'.($row['id']==$_POST['country'] ? ' selected="selected"' : '').'>'.$row['country'].'</option>'; } Then to display it... Country: <select name="country"><?php echo $htmCountries; ?></select> This bit: $row['id']==$_POST['country'] ? ' selected="selected"' : '' if exactly the same as this: if ($row['id']==$_POST['country']) { $htmCountries.=' selected="selected"'; } Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-791938 Share on other sites More sharing options...
rhodesa Posted March 23, 2009 Share Posted March 23, 2009 edit: similar as other posts...but i'm posting anyways the easiest way is to get the choices into an array (either manually defining the array or via info from a db)...then looping over the values and testing: <?php $list = array( '1' => 'Verified', '0' => 'Unverified', ); print "<select name=\"Status\" id=\"Status\">\n"; foreach($list as $key => $val){ $selected = ($req_sid_info['status'] == $key) ? ' selected="selected"' : ''; print " <option value=\"{$key}\"{$selected}>{$val}</option>\n"; } print "</select>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-791940 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 btw, that "fancy" if statement is called a ternary conditional. http://www.almsamim.com/ternary-conditions-t17.html Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-791942 Share on other sites More sharing options...
mfallon Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks guys, much appreciated, I'm sure I can get it working. Matt Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-791986 Share on other sites More sharing options...
mfallon Posted April 4, 2009 Author Share Posted April 4, 2009 Thanks guys, I did it like this. <option value="1" <? if ($req_sid_info['type'] == "CPSA"){ echo "selected=\"selected\""; } ?>>CPSA</option> Thanks for your help. Matt Link to comment https://forums.phpfreaks.com/topic/150740-solved-automate-selected-value-in-drop-down-list/#findComment-801315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.