robert_gsfame Posted April 5, 2010 Share Posted April 5, 2010 I create session for my shopping cart n wish to check whether certain product has been added into cart. I use in_array but not working... session_start(); $productname=$_GET['productname']; if(in_array($productname,$_SESSION['cart'])){ echo "Product has been added";}else{ echo "Not yet added";} I always get the second answer although i have created the session for the product... thx for any helps Quote Link to comment Share on other sites More sharing options...
trq Posted April 5, 2010 Share Posted April 5, 2010 What does $_SESSION['cart'] look like? Maybe you need to use array_key_exists or simply isset. Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted April 5, 2010 Author Share Posted April 5, 2010 I just put product id inside $_SESSION['cart'][x] Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 If you only put the product id in the cart array then you can't use the product name in your in_array() call, which is what you have done in post 1. You'll need to use the product id in the in_array() call, not the product name because from what you say, that doesn't get stored. Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted April 5, 2010 Author Share Posted April 5, 2010 $_SESSION['basket'][$x] and when $x already exists, then $x++ I've checked everything and nothing wrong with the array, except using in_array to check whether the record exists I can do with loop, but i don't think it's efficient enough Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted April 5, 2010 Author Share Posted April 5, 2010 Sorry guys!! I forgot that i've encoded the product id... Thx for all helps! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.