need some help with a dynamically populated dropdown menu...

Reply

Join Date: Mar 2007
Posts: 63
Reputation: websurfer is an unknown quantity at this point 
Solved Threads: 0
websurfer websurfer is offline Offline
Junior Poster in Training

need some help with a dynamically populated dropdown menu...

 
0
  #1
May 15th, 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 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!


  1. <SELECT name="category" id="category"> <?php
  2.  
  3. $query=("select * from item_category order by category_name, category_name desc");
  4.  
  5. $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
  6.  
  7. while($row=mysql_fetch_array($result)){
  8.  
  9. echo "<OPTION VALUE=".$row['category_name'].">".$row['category_name']."</OPTION>";
  10. }
  11. ?>
  12. </SELECT>
Last edited by peter_budo; May 16th, 2008 at 6:49 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 561
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: need some help with a dynamically populated dropdown menu...

 
0
  #2
May 15th, 2008
what do you mean "bring up"?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 63
Reputation: websurfer is an unknown quantity at this point 
Solved Threads: 0
websurfer websurfer is offline Offline
Junior Poster in Training

Re: need some help with a dynamically populated dropdown menu...

 
0
  #3
May 16th, 2008
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"...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 561
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: need some help with a dynamically populated dropdown menu...

 
0
  #4
May 16th, 2008
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:

  1. $counter=0;
  2. while($row=mysql_fetch_array($result)){
  3. if ($counter==0) //first one(selected)
  4. {
  5. echo "<OPTION VALUE=".$row['category_name']." selected='selected'>".$row['category_name']."</OPTION>";
  6. }
  7.  
  8. else //not selected
  9. {
  10. echo "<OPTION VALUE=".$row['category_name'].">".$row['category_name']."</OPTION>";
  11. }
  12. }
I hope I'm getting what you mean.
Last edited by ryan_vietnow; May 16th, 2008 at 1:04 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC