1

I am using a $_SESSION array to store item description, quantity and price for items placed in a shopping cart. This all works dandy, here's the beginning portion of the code:

$quantity = 1;

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
    $_SESSION['cart'] = array();
    $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);
    header("Location: http://website.com/cart.php");
    }

This loads the $_SESSION['cart'] array with the appropriate information and when I foreach loop it in the cart page, I get the exact contents of my cart. All of this works as desired.

So now what I want to do is search the $_SESSION['cart'] array to determine if a specific item is in the $_SESSION['cart'] array, such as "iPhone case - Black".

I have tried various forms of echo $_SESSION['cart']['product_description'] and get nothing. When I print_r the array, I get this:

Array
(
    [iPhone case - Black] => Array
        (
            [quantity] => 1
            [price] => 9.49
        )

)

Which is correct. Performing a var_dump yields the expected result:

array(1) {
  ["iPhone case - Black"]=>
  array(2) {
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(4) "9.49"
  }

}

Certainly, if there is a better way to construct the array, I'm open to it.

All I want to do is find out if "iPhone case - Black" is in the $_SESSION['cart'] array and serve up the appropriate code based on the result.

I have tried if (in_array('iPhone case - Black', $_SESSION['cart'])) which yields nothing, and when I expand that to if (in_array('iPhone case - Black', $_SESSION['cart'][0])) or ...[1] or ...['product_description'] I receive the error:

Warning: in_array() expects parameter 2 to be array, null given in...

What am I missing that would simply let me check the $_SESSION['cart'] array to find the value, "iPhone case - Black"? Is my only option a foreach loop?

Many sincere thanks in advance!

UPDATE Code posting as per request from @rmmoul:

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
$_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);
header("Location: http://website.com/cart.php");
}

Returns this through a print_r:

Array
(
    [0] => Array
        (
            [product_description] => iPhone case - Black
            [quantity] => 1
            [price] => 9.49
        )

    [1] => Array
        (
        [product_description] => iPhone case - Black
        [quantity] => 1
        [price] => 9.49
    )

)
1
  • You are assigning session for values and accessing by key. try to fix that Commented Aug 28, 2014 at 5:32

3 Answers 3

2

since you are using $_POST['product_description'] as key the assigned value will become key i.e. 'iPhone case - Black'.

try this:

 if (isset($_SESSION['cart']['iPhone case - Black'])){

foreach($_SESSION['cart']['iPhone case - Black'] as $key=>$value){

echo $key."  ".$value."<br>";

}
Sign up to request clarification or add additional context in comments.

1 Comment

I like the simplicity of the answer, hence the upvote. I feel the answer from @rmmoul helped to streamline my code so I'm pursuing that route for now, but thank you for replying so quickly!
1

What about isset?

if ( isset( $_SESSION['cart']['iPhone case - Black'] ) )

If it's true then $_SESSION['cart']['iPhone case - Black'] will contain the info:

array(2) {
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(4) "9.49"
}

1 Comment

I like the simplicity of the answer, hence the upvote. I feel the answer from @rmmoul helped to streamline my code so I'm pursuing that route for now, but thank you for replying so quickly!
1

You're not putting the value into a key called "product_description", you're naming the key after your product description. Try changing this line:

 $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);

To:

 $_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);

Then you will be able to echo the values like:

echo $_SESSION['cart'][0]['quantity'];
echo $_SESSION['cart'][0]['price'];
echo $_SESSION['cart'][0]['price'];

That should also let you have multiple products easily as $_SESSION['cart'][0], $_SESSION['cart'][1], etc.

You could then loop through the cart array to check for certain items like this:

foreach($_SESSION['cart'] as $product)
{
    if($product['product_description'] == 'black iphone')
    {
        //do something here
        echo $product['price'];
    }
}

This is untested though, and written from memory, so it might need tweaked a little to get it to run, but it's got the gist of what should work for you.

Edit:

I updated the foreach to be more useful.

6 Comments

Gonna give that a try now, thank you. I'll have to rewrite my foreach loop in the cart but after all this searching, I'll try it!
Sounds good! Also I updated the foreach to just check the product description directly instead of searching the whole array. That echo in there too should give you a good idea of how to access the product data when going through the session's cart array's too.
You must be setting it twice somehow. Can you share your updated code? You can edit your original question to add it.
Looking at the code you added, have your tried destroying the session or changing the values of the product that you've added to make sure that both are actually being added at once, and that it's not a case of your session having those values from your testing? If you just change the name of the product and run the page again, and dump the cart array and still see the two black iphone values, then you'll know if it's just a case of needing to clear your session. Also, why are you only adding an item to the cart if there isn't an item in the cart already, can users only buy one item?
I have a button that lets me clear the cart and start over that wipes the session array. I've tried it in different browsers...the behavior is the same...perplexing!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.