dabbsy Posted November 17, 2006 Share Posted November 17, 2006 I'm writing some code to allow a user to do some updates. To do this, I want to return all the potential options, but display the existing value in a select list. I can use the following to restore all the potential values, but assuming the os_id was 3 say (AIX in this case), how do I ensure that AIX is the value that is displayed at the top of the list?Hope someone can help,J<td align="left">OS Type:</td><td><select name="os_type"> <?php require_once ('./../mysql_connect.php'); $query = "SELECT os_id, os_type FROM os_type"; $result = mysql_query ($query); // Run the query. while ($row = mysql_fetch_array ($result, MYSQL_NUM)) { echo "<option value=\"$row[0]\""; echo ">$row[1]</option>\n'"; } ?> /></td> Link to comment https://forums.phpfreaks.com/topic/27571-ensuring-a-value-returned-from-a-database-is-displayed-in-a-select-list/ Share on other sites More sharing options...
komquat Posted November 17, 2006 Share Posted November 17, 2006 [code=php:0]while ($row = mysql_fetch_array($qry_result)) { $a = $row['a']; $a = $row['b']; $b = $row['c']; $user_option_menu .= "<option value='$a'>$b</option>"; }//Create sort block$user_sort_block = " <!--Creating Team sort portion--> <form method='post' action='script.php'> <p>Select User: <select name ='a' value='1'> $user_option_menu </select> <input type = 'submit' value = 'Select'> </form></p>";[/code]The above is the code that I use in many of my pages that I am doing this with. Link to comment https://forums.phpfreaks.com/topic/27571-ensuring-a-value-returned-from-a-database-is-displayed-in-a-select-list/#findComment-126116 Share on other sites More sharing options...
freakus_maximus Posted November 17, 2006 Share Posted November 17, 2006 You basically need an IF statement to do a little comparison. Here is what I do:This IF statment checks a previously declared variable '$na_name' against options for the dropdown list. $na_name was set when the form was loaded with data against a request. If the variable is equal to any options in the dropdown list it will render that <option> tag with 'selected' set so it will be displayed as the current selection. The list itself will still display in whatever sort order you have it set up to do.[code]<P align="center"><SELECT id="analyst" style="WIDTH: 125px" name="analyst" title="Select Network Analyst"><? $result = mysql_query ('SELECT developers FROM itpub_developer WHERE active = "Y" ORDER BY developers ASC') or die ("Query failed");while($row=mysql_fetch_array($result)){ if ($na_name == $row['developers']){ echo '<option value="' . $row['developers'] . '"selected>' . $row['developers'] . "</option>\n"; }else echo '<option value="' . $row['developers'] . '">' . $row['developers'] . "</option>\n";}echo '</select>';?>[/code]Hope that helps. Link to comment https://forums.phpfreaks.com/topic/27571-ensuring-a-value-returned-from-a-database-is-displayed-in-a-select-list/#findComment-126155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.