Hello, all: I need some help with a dynamically populated dropdown menu...

I have managed to dynamically populate the dropdown menu from my item_categories table, and it works fine, EXCEPT once I have entered an item and want to bring it back into the form to UPDATE it, how do I make it so the category for the item is remembered and automatically appears as "selected"

Basically there are 2 tables:

the "item_category" table that hold the list of categories with their respective ID's, like this:

category_id category_name
1 art
2 clothing
3 misc


and second "item" table that hold the products, and is related to the "item_category" table thru the category ID's like this:

item_id title category
109 painting 1
110 drawing 1
111 hat 2

So, as you see in the 'select" form drop down menu script below, the categories are pulled from the 'item_categorty" table just fine, BUT how do I make it so when I bring up, let's say, item #111 the right category appears already as selected???

Appreciate any help!!

thanks!

<SELECT name="category" id="category"> <?php

$query=("select * from item_category order by category_name, category_name desc");

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 

while($row=mysql_fetch_array($result)){ 

echo "<OPTION VALUE=".$row['category_name'].">".$row['category_name']."</OPTION>";
}
?>
</SELECT>

hi, Ryan...

by bring it up, I mean for the menu to automatically "select" the category that an item has already been labeled as... as in, if I was to UPDATE and item and that item is under "art" category, when it shows back in form, "art" category should already appear as "selected"...

if what you mean when you update something that new update thing will be the selected one,try adding a counter in your loop so if the counter is 1,it won't be labeled like selected:

$counter=0;
while($row=mysql_fetch_array($result)){
if ($counter==0) //first one(selected)
{
echo "<OPTION VALUE=".$row['category_name']." selected='selected'>".$row['category_name']."</OPTION>";
}

else //not selected
{
echo "<OPTION VALUE=".$row['category_name'].">".$row['category_name']."</OPTION>";
}
}

I hope I'm getting what you mean.

This article has been dead for over six months. Start a new discussion instead.