Forum Moderators: coopster

Message Too Old, No Replies

Using explode function

To populate a select box

         

wruk999

6:43 pm on Oct 20, 2003 (gmt 0)

10+ Year Member



Hi,

I need to achieve the following:
Have a column in my database called Items and another called Option.
I need to print a select form menu to the page, using the two fields above, the Option for the name and the Items for the menu items, but these are to be stored in an array, split by a ,

There will be varying amounts of options in the Option column for different products, so it needs to be able to be dynamic and not fixed at a certain amount off variables.

So, in short, I need to get the following:


Option ¦ Colour
Items ¦ Red,Green,Blue,Orange,Violet

to print out like so:


<select name="Colour">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Orange">Orange</option>
<option value="Violet">Violet</option>
</select>

I really help someone can help me out here, this is something that has stumped me! :(
I have looked at the explode function, but couldn't get past it printing out the list of values.

Cheers,
wruk999

jatar_k

7:00 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



assuming $row[1] is the comma seperated items

$itemlist = explode [ca.php.net](",",$row[1]);

echo "<select name=\"Colour\">\n";
foreach ($itemlist as $selectitem) {
echo "<option value=\"$selectitem\">$selectitem</option>\n";
}
echo "</select>\n";

should work

wruk999

9:03 pm on Oct 20, 2003 (gmt 0)

10+ Year Member



Hey Jatar,

That worked an absolute treat. Thanks very very much :)

Appreciated.

wruk999.