Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quierying the $_SESSION object 1

Status
Not open for further replies.

ralphonzo

Programmer
Apr 9, 2003
228
GB
Having a bad day trying to check through the $_SESSION["cart_array"] to see if the product ID in question has associated extras (over 100 different colours to choose from). If it does and it's already evident in the basket, it needs to add to quantity, otherwise be added as a new product. At the moment everything is adding as a new product, even if it exists in the first place.

I know I'm trying to manipulate the $_SESSION object incorrectly, but I don't know how to get it to echo anything but "A

PHP:
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
		foreach ($_SESSION["cart_array"] as $each_item) { 
		  $i++;
		  while (list($key, $value) = each($each_item)) {
			  if (($key == "item_id" && $value == $pid) && ($key == "item_extratype" && $value == $extype) && ($key == "item_extra" && $value == $ext)) {
				  // That item is in cart already so let's adjust its quantity using array_splice()
				  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "item_extratype" => $extype, "item_extra" => $ext, "quantity" => $each_item['quantity'] + 1)));
				  $wasFound = true;
			  } // close if condition
		  } // close while loop
	   } // close foreach loop
	   if ($wasFound == false) {
		   array_push($_SESSION["cart_array"], array("item_id" => $pid, "item_extratype" => $extype, "item_extra" => $ext, "quantity" => 1));
	   }

Sorry to be a putz!
 
First we would need to clarify what the structure of your cart_array looks like, so run this:

Code:
echo [COLOR=#FF0000]"<pre>"[/color] [COLOR=#990000].[/color] [b][COLOR=#000000]print_r[/color][/b][COLOR=#990000]([/color]$_SESSION[COLOR=#990000][[/color][COLOR=#FF0000]'cart_array'[/color][COLOR=#990000]],[/color][COLOR=#993399]1[/color][COLOR=#990000])[/color] [COLOR=#990000].[/color] [COLOR=#FF0000]"</pre>"[/color][COLOR=#990000];[/color]

Second don't quite understand what your IF is actually looking to check, but I don't think it will ever evaluate to true.
Code:
[b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000](([/color]$key [COLOR=#990000]==[/color] [COLOR=#FF0000]"item_id"[/color] [COLOR=#990000]&&[/color] $value [COLOR=#990000]==[/color] $pid[COLOR=#990000])[/color] [COLOR=#990000]&&[/color] [COLOR=#990000]([/color]$key [COLOR=#990000]==[/color] [COLOR=#FF0000]"item_extratype"[/color] [COLOR=#990000]&&[/color] $value [COLOR=#990000]==[/color] $extype[COLOR=#990000])[/color] [COLOR=#990000]&&[/color] [COLOR=#990000]([/color]$key [COLOR=#990000]==[/color] [COLOR=#FF0000]"item_extra"[/color] [COLOR=#990000]&&[/color] $value [COLOR=#990000]==[/color] $ext[COLOR=#990000]))[/color]

How can $key be equal to 'item_id' and 'item_exchange' at the same time. I would suspect its either one or the other.

And finally tell us how you know if a product has associated extras.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Right. The results of the echo are:

PHP:
Array
(
    [0] => Array
        (
            [item_id] => 39
            [item_extratype] => 0
            [item_extra] => 0
            [quantity] => 1
        )

    [1] => Array
        (
            [item_id] => 62
            [item_extratype] => 1
            [item_extra] => 80
            [quantity] => 1
        )

)

I'm trying to establish that if item_id == 62 && item_extratype == 1 && item_extra == 80 then the new product should be added as a plus 1 quantity of product 62, otherwise a fresh entry in the cart.

The client landed us with this extra task after everything else was finished, otherwise I would have scripted it differently !!
 
O.k if I understand this correctly, something like this maybe?
Code:
[b][COLOR=#0000FF]foreach[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$_SESSION[/color][COLOR=#990000][[/color][COLOR=#FF0000]"cart_array"[/color][COLOR=#990000]][/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$each_item[/color][COLOR=#990000])[/color] 
[COLOR=#FF0000]{[/color]
[tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$each_item[/color][COLOR=#990000][[/color][COLOR=#FF0000]'item_extratype'[/color][COLOR=#990000]][/color] [COLOR=#990000]==[/color] [COLOR=#993399]1[/color] [COLOR=#990000]&&[/color] [COLOR=#009900]$each_item[/color][COLOR=#990000][[/color][COLOR=#FF0000]'item_extra'[/color][COLOR=#990000]][/color] [COLOR=#990000]==[/color] [COLOR=#993399]80[/color][COLOR=#990000])[/color]
[tab][COLOR=#FF0000]{[/color]
[tab][tab][COLOR=#009900]$each_time[/color][COLOR=#990000][[/color][COLOR=#FF0000]'quantity'[/color][COLOR=#990000]]++;[/color]

[tab][COLOR=#FF0000]}[/color]
[COLOR=#FF0000]}[/color]



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Dear me! Is that all I needed to do? I'm not a PHPer I'm afraid. I didn't know that $each_item was actually an array. I'll give this a go. Thank you very much. Look out for that star!!
 
Dear me! Is that all I needed to do? I'm not a PHPer I'm afraid. I didn't know that $each_item was actually an array. I'll give this a go. Thank you very much. Look out for that star!!

Which is why outputting what you are working with is essential to knowing how to access it. I didn't know what it was until you posted back the output of the echo, which shows its an array. From there accessing is simple.

Thanks for the star.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I was outputting, but all I could get out of it was "Array". Give me ASP any day!!
 
the string representation of an array object is "Array". that's why you were getting that word. for debug purposes take a look at var_dump().
 
Yup, if you think its an array, you can use var_dump, or print_r which I like to use. Directly echoing out an array will just print the Array word. Which would be a hint that the variable is an array.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top