View Full Version : drop down menu

08-23-2007, 05:49 PM
hi guys

I have a database field called category. This can ewither be pork, beef, chicken. if i pull a record out and display it on the screen and the category is beef, how can i populate a category drop down with all 3 options but with beef selected?

anyhelp would be great

Thanks in advance

08-23-2007, 06:25 PM
<select name="meatSelect">
<option value="chicken" <?php echo ($databaseValue == "chicken" ? "selected" : ""); ?> >Pork</option>
<option value="pork" <?php echo ($databaseValue == "pork" ? "selected" : ""); ?> >Pork</option>
<option value="beef" <?php echo ($databaseValue == "beef" ? "selected" : ""); ?> >Beef</option>

You will of course need to put your own variable names in there. This is basically just adding a PHP "if" statement to each <option> tag using the ternary syntax of (<condition> ? <true> : <false>), adding the keyword "selected" to the one option tag where the database value matches the option value.

08-23-2007, 07:07 PM
thanks for that works perfectly.