Re: hrm... varaible varaibles and looping

From: Date: Tue, 14 Nov 2000 05:38:08 +0000
Subject: Re: hrm... varaible varaibles and looping
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-25119@lists.php.net to get a copy of this message
Kurth Bemis writes: > At 09:09 PM 11/13/2000 -0800, Lars Torben Wilson wrote: > > code is below......i have changed it a bit...and now i need something > different...and on top of all that....i have a quick question....first teh > question.... > > i'm hacking up a quick shopping cart..all it does it take the product id's > and put them into a large variable for storing in a session......what other > ways could i do this? i thought about mysql...but then decided against it > because it was getting complicated to having to add fields and this and > that.... Check out serialize(). It takes a variable (say, an array) and makes it into a string which unserialize() can reform into a copy of the original variable. The string can then be stored, i.e. in a database text field or something. http://www.php.net/serialize http://www.php.net/unserialize > and with the loop....look at the code now...its should be easier to > understand... > > if (session_is_registered("cart")){ OK, this is the relevant bit: > while (explode (":",$cart)){ You're not assigning the result of the explode() to anything. First off, this means that if $cart contained anything, this loop would never finish: every time, it's explode()ing the same string, giving the same result. > $cartitem = "item[".$numofitems ."]"; Here, $cartitem becomes a string with the value 'item[n]', where n is whatever $numofitems has gotten to via the loop. Among other things, the first time through, this will be 'item[]', unless you've earlier initialized $numofitems to 0 or something. > $$cartitem = $cartarray[$numofitems]; Where did $cartarray come from? Anyway, PHP doesn't support variable array variables. i.e. if $cartitem == 'item[3]', and you assign something to it like that, you get different results from what you may expect. Check out the following snippet: <?php $cartitem = 'item[3]'; $$cartitem = 'testing'; echo $item[3]; echo "$cartitem == '" . $$cartitem . "'<br>\n"; ?> This assigns the value of 'testing' to a variable which has the name 'item[3]'. This doesn't mean that the array $item has a third element the value of which is 'testing'; it means that PHP has created a scalar variable with the name 'item[3]', and assigned it the value of testing. If you run the above code with error_reporting() turned up, you'll get something like this: Warning: Undefined variable: item in /home/torben/public_html/phptest/__phplist.html on line 4 item[3] == 'testing' See what I mean? > $numofitems++; > echo $cartitem; > } > > ok - first of all we check to see if the cart exists....if it does we > continue...if not we exit......then i take the variable ($cart) and split > it apart at the ':'s. the problem that i run in to is that i can't get the > loop to stop looping when there is nothing more to be split because it is > at the end of $cart. is there a way to do this? do i make sense? > > ~kurth Well, if the code worked as expected, it looks like it would just take $cartarray (i.e., the result of explode()ing $cart), and duplicate it into an array named $item. Is that what you want? What should the resulting array look like? -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#25119) next »