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!
Hi there, I'm having an issue trying to get the correct item selected in a drop down menu. I'm populating the menu from one table and pulling the current value from another... the code is as follows:
<?php
//Get the Category name based on the CategoryID from user record
$query2 = "SELECT * FROM category WHERE CategoryID='" . $row["CategoryID"] . "'";
$result2 = mysql_query($query2) or die ('I cannot connect to the database because: ' . mysql_error());
$row2 = mysql_fetch_array($result2);
//Get all Category names to populate the drop down menu
$query = mysql_query("SELECT category FROM category ORDER BY category");
echo ("<SELECT NAME=\"Category\">");
while ($r = mysql_fetch_array($query))
{
$category = $r["category"];
echo "<option value=$category";
if ($category == $row2["Category"]) echo " SELECTED";
echo ">$category</option>";
}
echo("</SELECT></td></tr>");
?>
The list gets populated, but the value from the db doesn't become the selected item in the list...
However looking at the source of the page it looks like it's right...
//Get all Category names to populate the drop down menu
$query = "SELECT category,CategoryID FROM category ORDER BY category"
$result = mysql_query($query) or die(mysql_error().': '.$query);
echo '<select name="Category">';
while ( $r=mysql_fetch_array($result) )
{
$selected = ($r['CategoryID']==$row['CategoryID']) ? ' selected="selected"': '';
echo '<option', $selected, '>', htmlentities($category), '</option>';
}
echo '</select>';
Thanks! part of the problem was Firefox, even after inserting you code, it did nothing. I switched over to IE and it works... any ideas on why it wouldn't work properly with FF?