Forum Moderators: coopster

Message Too Old, No Replies

Make an array from a MySQL set data type field

         

2004mark

3:43 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



I have a MySQL table with a column called 'colour', this column is a SET data type.

I use it to store the colours that each product is available in i.e. 'red,green,blue'

I need to send the content of this column to an array so that for each colour the product is available in I can output a radio button using the foreach function.

My code is:


$resultCol = mysql_query("SELECT colour FROM productsWHERE code='$id'");
while ($array = mysql_fetch_array($resultCol))
{
$data[] = $array[0];
}

My problem is that when I print the array using:

echo '<pre>' . print_r($data, true) . '</pre>';

I see it has inserted all the values into one variable in the array, I get:

Array
(
[0] => blue,green,white
)

When what I need is:

Array
(
[0] => blue
[1] => green
[2] => white
)

Any advise would be greatly appreciated.

mooger35

4:53 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



I think explode [ca3.php.net] is what you need

$new_array = explode(',',$data[0]);

2004mark

8:19 am on Oct 21, 2008 (gmt 0)

10+ Year Member



Thanks mooger, I'll check that out.

2004mark

8:35 am on Oct 21, 2008 (gmt 0)

10+ Year Member



It worked a treat, thank you very much