Hi,
I'm using PHP with MySql. How can i populate a combo/drop-down box with data from a table stored in a MySql db?
Thanks.
Nick
Populating a combo box
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Code: Select all
//assuming you have already done the query
echo '<select name="awesome">';
while ($info = mysql_fetch_assoc($query))
{
echo '<option value="'.$info['yourFieldName'].'">'.$info['anotherFieldName'].'</option>';
}
echo '</select>';- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
echo '<select name="awesome">';
while ($info = mysql_fetch_assoc($query))
{
echo '<option value="'.$info['yourFieldName'].'"'. (!empty($_POST['awesome']) && $_POST['awesome'] == $info['yourFieldName'] ? ' selected=selected' : '').'>'.$info['anotherFieldName'].'</option>';
}
echo '</select>';