| | |
need some help with a dynamically populated dropdown menu...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
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!
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!
php Syntax (Toggle Plain Text)
<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>
Last edited by peter_budo; May 16th, 2008 at 6:49 pm. Reason: Keep It Organized - please use [code] tags
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:
I hope I'm getting what you mean.
php Syntax (Toggle Plain Text)
$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>"; } }
Last edited by ryan_vietnow; May 16th, 2008 at 1:04 am.
![]() |
Similar Threads
- using Onchange() in PHP (PHP)
- Help with dynamic menu (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: An Upload and Image Display code?
- Next Thread: elow guyz.......
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend





