Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate Html Select with MySQL data.

Status
Not open for further replies.

robcarr

Programmer
May 15, 2002
633
GB
Hi,

I have a MySQL database which looks like this:

Code:
ID             | NAME            | DESCRIPTION                     | DRESSINGS
----------------------------------------------------------------------------------------------------------------
ham_and_cheese | Ham and Cheese  | A sandwich with ham and cheese  | Mayonnaise, Salad Cream, Chutney, No Dressing
tuna_mayo      | Tuna Mayonnaise | A sandwich with tuna mayonnaise | Water Cress, Pesto


I need to loop through each record in the database and then populate a select element with the different dressings.
For example:

For ham_and_cheese, it would get the select element with the id "ham_and_cheese_dressings" and then the different options in that select would be "Mayonnaise", "Salad Cream", "Chutney" and "No Dressing".

I'm not very familiar with getting MySQL data and displaying it client side, how would I go about doing this?

Hope this is of use, Rob.[yoda]
 
having the dressings as a comma separated list for each record makes it little harder.

However, you need to define how you are displaying things currently. do you want to build a select element for each sandwich?

How are you displaying the sandwiches?

The most straight forward way to do this would be top explode the dressings into an array and loop through it as you display your sandwiches to build the select boxes.

explode()

Code:
$dressingarray = explode(",",$row['dressings']);
$slectHTML = "<select name='dressings'>";
foreach($dressingarray as $key => $item)
{
[indent]$selectHTML ="<option value='" . $item . "'>" . $item . "</option>"; [/indent]
}
$selectHTML = "</select>";
Without any code, its hard to be any more specific.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top