store array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jiggy1com
Forum Newbie
Posts: 6
Joined: Sun Jul 11, 2004 11:55 pm
Location: orange county, ca

store array

Post by jiggy1com »

feyd | Please use

Code: Select all

tags when posting code.  Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


can you store an array in a mysql db, and retrieve it and use it as an array? (does the field need to be TEXT or something special?)

when i use phpmyadmin i see the value is Array basically saying i have an array stored in there -- that's fine. when i pull it out and echo it, i get Array again; that makes sense. 

if i count($array) i get 1, or whatever number of items it has.

however, when i try to loop the array it doesn't run the loop. any ideas??

see code:

Code: Select all

while($question = mysql_fetch_object($run_form_data))
		{
			while($chosethis = mysql_fetch_object($run))
			{				
			$here = eval("return \$chosethis->question_{$question->formdataid};");
			print $here;	// prints "Array"				
			print count($here);	// prints "1"		
				foreach($here as $temp)
				{						
				print "[".$temp."]"; // is never run, i never see "[]"
                                }
			}
		}


feyd | Please use

Code: Select all

tags when posting code.  Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're storing the string, "Array", nothing more.. use [php_man]serialize[/php_man]() to compact the array into a string. To expand it back, use [php_man]unserialize[/php_man]().
jiggy1com
Forum Newbie
Posts: 6
Joined: Sun Jul 11, 2004 11:55 pm
Location: orange county, ca

thkx

Post by jiggy1com »

thanks - i dont think im storing a string ... im storing ... i have checkboxes with the same name, when submitted php creates an array, correct? that's what's stored.

i'll try what you recommended and that sounds like it'll work.

sorry for not using <?php ?> -- this being a PHP site i thought i could omit them :)

...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can test if you are storing a string or not by switching "print $here;" to "var_dump($here);"
jiggy1com
Forum Newbie
Posts: 6
Joined: Sun Jul 11, 2004 11:55 pm
Location: orange county, ca

delayed thanks!

Post by jiggy1com »

hey i know its been some time but i just got back to working on this thing.

looks like your suggestions payed off: serialize(), store data, retrieve data, unserialize() ... now i can work with it.

thanks!!!!!
Post Reply