MYSQL populated combo boxes with a twist.

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
Junga
Forum Newbie
Posts: 2
Joined: Mon Mar 23, 2009 11:31 pm

MYSQL populated combo boxes with a twist.

Post by Junga »

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
User avatar
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.

Post by jayshields »

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>';
 
Junga
Forum Newbie
Posts: 2
Joined: Mon Mar 23, 2009 11:31 pm

Re: MYSQL populated combo boxes with a twist.

Post by Junga »

Thanks for that haven't tested it yet but makes sense just wasn't sure what function to use.
Post Reply