March 4, 200818 yr I have an array stored in my_sql using serialize, makes a messy string in my database. I use unserialize to turn back into an array. how do i display JUST the data stored inside the array?? This is all i can work out to display; a:2:{i:0;s:5:"white";i:1;s:5:"black";} Or Array ( [0] => white [1] => black [2] => red [3] => blue ) all i want to display is just the colours like this: white, black, red, blue could some one point me in the correct direction as i cant find it anyware? Thank you in advance! Leah
March 4, 200818 yr Serialzing data for MYSQL is great. We use this method for forms. Here's how you can get your data out... I'll comment it so you know what is going on... $query="SELECT * FROM colours"; // change the 'colours' to the table you have your array in $result=mysql_query($query); // perform the query $numrows=mysql_num_rows($result); // count how many results returned... if($numrows>0) // and if there's anything in the table, process it { while($colours_list=mysql_fetch_array($result)) // This loop checks for multiple sets of colours { $colours_set=unserialize($colours_list['colours']); // Unserailize the 'set' of colours foreach($colours_set as $single_colour) // This loop sorts one at a time each colour from the unserialized array { print $single_colour.', '; // prints the colour name, followed by a comma. } // end of 'for each' loop } // end of 'while' loop } // end of 'if numrows>0' loop if it's true else { print 'That table's empty!'; // Display a message that the table is empty } // end of 'if numrows>0' loop if it was false Hope that makes sense!
March 4, 200818 yr Author I get an error here; foreach($colors_set as $single_colour) Warning: Invalid argument supplied for foreach() in /home/lingerie/public_html/new/admin/list.php on line 48 I haven't been able to use the "foreach($ as $ )" command as it gives me errors every time i try to use it?
March 5, 200818 yr I get an error here; foreach($colors_set as $single_colour) Warning: Invalid argument supplied for foreach() in /home/lingerie/public_html/new/admin/list.php on line 48 I haven't been able to use the "foreach($ as $ )" command as it gives me errors every time i try to use it? What PHP version are you using? Foreach has been around since version 4. You may be able to subsitute a "while/each" loop for the foreach.
Create an account or sign in to comment