Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP] hrm... varaible varaibles and looping

0 views
Skip to first unread message

Lars Torben Wilson

unread,
Nov 13, 2000, 11:11:05 PM11/13/00
to
Kurth Bemis writes:
> look at this
>
> i have $cart with "fields" separated by ':'. i explode the
> variable...however..i can't get it to loop and add one to the variable that
> points to the array...i can't figure out how to lloop until the end of the
> array is reached.....any ideas?
>
> look at the code.

Where's the loop? It's not quite clear what you're attempting to do
here. Can you include the loop itself and explain a bit more exactly
what effect you're going for?

> if (session_is_registered("cart")){
> $cartitem = "item[".$numofitems ."]"; //set cartitem to item[#]
> $cartarray = explode (":",$cart); //separate cart at the colons
> if ($cartarray){
> $$cartitem = $cartarray[$numofitems]; //make $$cartitem equal to item[#]
> and then set that to cartarray[#]
> $numofitems++; //duh
> echo $cartitem;
> }elseif (!$cartarray){
> echo "done!";
> }
>
> }else{
> session_register("cart");
> }
>
> ~kurth
> Kurth Bemis - Network/Systems Administrator, USAExpress.net/Ozone Computer
>
> There is no sin except stupidity. -- Oscar Wilde
>
> ku...@usaexpress.net | http://www.usaexpress.net/kurth
> PGP key available - http://www.usaexpress.net/kurth/pgp
>
> Fight Weak Encryption! Donate your wasted CPU cycles to Distributed.net
> (http://www.distributed.net)
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general...@lists.php.net
> For additional commands, e-mail: php-gene...@lists.php.net
> To contact the list administrators, e-mail: php-lis...@lists.php.net
>

--
+----------------------------------------------------------------+
|Torben Wilson <tor...@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 tor...@netmill.fi|
+----------------------------------------------------------------+

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general...@lists.php.net
For additional commands, e-mail: php-gene...@lists.php.net
To contact the list administrators, e-mail: php-lis...@lists.php.net

Kurth Bemis

unread,
Nov 13, 2000, 10:48:39 PM11/13/00
to
look at this

i have $cart with "fields" separated by ':'. i explode the
variable...however..i can't get it to loop and add one to the variable that
points to the array...i can't figure out how to lloop until the end of the
array is reached.....any ideas?

look at the code.


Kurth Bemis

unread,
Nov 13, 2000, 11:20:37 PM11/13/00
to
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....

and with the loop....look at the code now...its should be easier to
understand...

if (session_is_registered("cart")){
while (explode (":",$cart)){


$cartitem = "item[".$numofitems ."]";

$$cartitem = $cartarray[$numofitems];
$numofitems++;
echo $cartitem;
}
}else{
session_register("cart");
}

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

>Kurth Bemis writes:
> > look at this
> >
> > i have $cart with "fields" separated by ':'. i explode the
> > variable...however..i can't get it to loop and add one to the variable
> that
> > points to the array...i can't figure out how to lloop until the end of the
> > array is reached.....any ideas?
> >
> > look at the code.
>

>Where's the loop? It's not quite clear what you're attempting to do
>here. Can you include the loop itself and explain a bit more exactly
>what effect you're going for?
>

>--
>+----------------------------------------------------------------+
>|Torben Wilson <tor...@php.net> Netmill iTech|
>|http://www.coastnet.com/~torben http://www.netmill.fi|
>|Ph: 1 250 383-9735 tor...@netmill.fi|
>+----------------------------------------------------------------+

Kurth Bemis - Network/Systems Administrator, USAExpress.net/Ozone Computer

Maxim Maletsky

unread,
Nov 13, 2000, 11:29:07 PM11/13/00
to
explode is always true, or always false when there's nothing to explode,

you need :

a. put an

if(whatever here makes you happy)
Break;

to stop the loop when you are happy with the result,

b. (the best solution)

$var = explode (":",$cart);

and then loop your $var ...

so your code will look like this:


if (session_is_registered("cart")){
$exploded = explode (":",$cart)
for ($i=0; $<sizeof($exploded ); $i++){

/*


$cartitem = "item[".$numofitems ."]";
$$cartitem = $cartarray[$numofitems];
$numofitems++;

*/
// I commented this because It doesn't make here any sence ...
// just rewrite your code basing on the for loop of the exploded object
($exploded)

echo $cartitem;
}
}else{
session_register("cart");
}

Lars Torben Wilson

unread,
Nov 13, 2000, 11:39:18 PM11/13/00
to
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?

0 new messages