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>
Link to comment
Share on other sites

[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
Share on other sites

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
Share on other sites

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.