mac007 Posted May 15, 2008 Share Posted May 15, 2008 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 below, the categories are pulled from the 'item_categorty" table, BUT how do i make it so when I bring up, let's say, item #111 the right category appears as selected??? Appreciate any help!! thanks! I <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> Link to comment https://forums.phpfreaks.com/topic/105688-help-with-dynamically-populated-dropdown/ Share on other sites More sharing options...
moselkady Posted May 15, 2008 Share Posted May 15, 2008 When you create <option> inside while loop, check if the category in the option is equal to the category of the selected item. If so, you add SELECTED top the option tag. Below I assume the selected item, e.g. #111, is in variable $item: <SELECT name="category" id="category"> <?php $query="select category_name from item_category, item where item.item_id=$item and item.category=item_category.category_id"; $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); $row=mysql_fetch_array($result); $select_category = $row['category_name']; $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)){ $selected = ($select_category == $row['category_name']) ?" selected" :""; echo "<OPTION VALUE=".$row['category_name'].$selected.">".$row['category_name']."</OPTION>"; } ?> </SELECT> Link to comment https://forums.phpfreaks.com/topic/105688-help-with-dynamically-populated-dropdown/#findComment-541537 Share on other sites More sharing options...
mac007 Posted May 15, 2008 Author Share Posted May 15, 2008 Hi, moselkady: I tried to apply you code below but it doesnt seem to be doing it... could it be there's a missing character somewhere? or maybe wrong variable?? not sure why you are calling the $item variable or how it's being referred back in the script.. anyways, maybe I missed a step? Thanks a lot for your reply...! anymore ideas? Link to comment https://forums.phpfreaks.com/topic/105688-help-with-dynamically-populated-dropdown/#findComment-542502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.