Have dynamic drop list display a loaded value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deddog
Forum Commoner
Posts: 55
Joined: Thu Sep 26, 2002 6:05 am
Location: Brighton

Have dynamic drop list display a loaded value

Post by Deddog »

Hello and Help.

I have recently built an update page for my database. The problem is that i use dynamicaly built droplist. How can i change the below procedure so that it by default displays the relevent value of the row to be updated?


<?
function Dept_List(){
$Dept = 'Department';
$sql = mysql_query("SELECT Dept_Desc, Dept_Id FROM Departments");
echo "<select name=\"$Dept\">";
echo"<option> </option>";
while(list($Dept_Desc, $Dept_Id)=mysql_fetch_array($sql)){
$Dept_Desc = stripslashes($Dept_Desc);
echo "<option value=\"$Dept_Id\">$Dept_Desc</option>";
}
echo "</select>";
mysql_free_result($sql);
}

Cheers,

Lee.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if a <option>-element has the attribute selected (or even better selected="true") it will be displayed as -hmmm- selected (in a one-row dropdown that's the visible entry)
i.e.

Code: Select all

<select name="s1">
   <option>a</option>
   <option>b</option>
   <option selected="true">C</option>
   <option>d</option>
</select>
How to identify the record you have to mark with selected we don't know since you didn't give us the WHERE-clause of your update query ;)
Post Reply