View Full Version : list box

05-06-2006, 10:43 PM
i know its possible using php to create a list box populated from a database and this i have done in the past.
What i want to do now is:
I have records that have colour options in a single field seperated by a comma eg red,yellow,green,blue
What i want to do is add each of those colours into a list box using php pulling that data from the db. These colours are different for each record in the db and are all in one field. Is this doable?


05-07-2006, 12:16 AM
Loop over the query result set:

$b = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$color = $row['color']
$a = explode(',', $color);
$b = array_merge(array_diff($a, $b), $b);
echo '<pre>';
echo '</pre>';

In $b you will have an array with all the distinct colors.

05-07-2006, 10:11 AM
thats great i will try that out later, how would i create a list box reflecting those colours now?

05-07-2006, 03:31 PM
by using CSS. every item with an individual style

05-08-2006, 02:33 PM
$col = explode(',', $row_rs['colour']);
<select name="mySel">
foreach($col as $opt){
<option style="background-color: <?php echo $opt; ?>" value="<?php echo $opt; ?>"><?php echo $opt; ?></option>