Setting initial option value of dropdown box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Setting initial option value of dropdown box

Post by cjkeane »

Hi everyone.

I have a dropdown box retrieving information from a column in a table. I wish the initial value to be <select from list>. Can anyone suggest a way to do that? thx.

<?php
$query="select CategoryName from categorytypes";
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='CategoryTypes'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['CategoryName']}'>{$row['CategoryName']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Setting initial option value of dropdown box

Post by social_experiment »

Code: Select all

//
$dropdown = "<select name='CategoryTypes'>";
$dropdown .= "<option selected=\"selected\">Select from list</option>";
//
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply