Jump to content

populating pulldown menu from database


wkilc

Recommended Posts

I am editing a script that pulls values from a database to populate a pulldown menu:

   if($_GET['car'] ==   $row['car']  ){  echo "selected=\"selected\""; }
    print " value=\"$page_name&car=" . $row['car'] . "\">" . $row['car'] . "</option>\n";
  }
  print "</select>";

This works great, except some of the "car" values in my table are empty... so I get one row in my pulldown menu that's empty:

 

<option selected="selected" value="/list.php?car="></option>
<option value="/list.php?car=Honda">Honda</option>
<option value="/list.php?car=Hyundai">Hyundai</option>

etc...

 

How can I tell it to not print an "empty" result in my pull down?

 

My apologies if I am not explaining this well.

 

Thank you.

 

Link to comment
Share on other sites

If you have a properly normalized database with one table with a list of car names you shouldn't have any empty rows. You would also want to be using the ID of the car name not the actual name itself for the value. Additionally, the file name does not belong in the value.

Edited by benanamen
Link to comment
Share on other sites

@Zane, the if is just a quick fix band-aid for what is pretty clearly a bad database design.

 

 

@wkilc, if your wanting to get things done correctly, post your DB schema and we will help you get this right from the ground up. Your Database is the foundation on which you will write all your code. If that is not right, the code you build on it will not be right and you will continue to run into unnecessary problems.

Edited by benanamen
  • Like 1
Link to comment
Share on other sites

Well the OP didn't ask for opinions on his db setup, and didn't post any information about his db structure, so I don't honestly know what his db looks like.  I can't just assume he doesn't have a primary key index setup, but that seems to be what everyone is assuming.

Link to comment
Share on other sites

Well the OP didn't ask for opinions on his db setup, and didn't post any information about his db structure,

 

Yeah, that's the thing with noobs (Don't mean it in a bad way), they don't know what to ask or provide. It the typical XY problem. http://xyproblem.info/

 

OP sees a particular problem and asks how to fix that problem not realizing it is just a symptom of the real problem.

 

My experience says there is a very high probability that the DB structure is wrong which is why I asked the OP to provide it. I am sure you would agree that the DB is the first thing to ensure is correct and if not to fix that first.

Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.