Jump to content

Ensuring a value returned from a database is displayed in a select list


dabbsy

Recommended Posts

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>
[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.

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.

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.