Hi all,
I am having some trouble populating a combo box with mysql values.
Yes I know there are plenty of tutorials showing how but wait for the twist.
I have a contact database with a field category. So when you add a new contact the form has a combo box with all the different categories in it which is populated from a table. What I am having trouble with is with my edit contact screen. It is essentially the same screen with the data all ready in all the fields as per that contacts record but the category combo box is set to the top value in the combo box. I need the combo to be populated as with the new contact form but sets the currently selected value as per that contacts category field. So everytime you edit a contact the category isn't automatically reset to the default top value in the combo box.
Hope that makes sense.
Thanks in advance.
Junga
MYSQL populated combo boxes with a twist.
Moderator: General Moderators
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: MYSQL populated combo boxes with a twist.
Use something like this:
Code: Select all
echo '<select name="xxx">';
foreach($whatever as $k => $v)
{
echo '<option value="'.$v.'"';
if($something == $v) echo ' selected="selected"';
echo '>'.$k.'</option>';
}
echo '</select>';
Re: MYSQL populated combo boxes with a twist.
Thanks for that haven't tested it yet but makes sense just wasn't sure what function to use.