...

View Full Version : list box



robojob
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?

Thanks

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


<?php
$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>';
print_r($b);
echo '</pre>';
?>

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

robojob
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?

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum