1

My simple shopping cart stores products id's in a session array.

I'm trying to set up an if/else statement to enable/disable my "Add to cart" button based on the product ID being in array or not.

<?php
session_start();

//Show cart array

print_r($_SESSION['cart']);
echo '<br><br>';

//Return "yes" or "no"

$panier = $_SESSION['cart'];
$produit = "5";

if (in_array($produit, $panier)) {
print "yes man!";
} 
else {
print "no man!";
}

?>

I'm making sure 5 is part of the array values by displaying them of this test page, but the second part always returns "no man!"

looks simple enough to me. What am i doing wrong ?

print_r command output is

5,5

no man! 

that is because i've added 2 of the "5" product id to my cart

If I change this line

 print_r($_SESSION['cart']);

for

 print_r($_SESSION);

I get

 Array ( [cart] => 5,3,3,3,3,3,3,3,2 )

 no man!
2
  • 7
    Show the output of print_r($_SESSION['cart']); Commented Nov 17, 2014 at 2:28
  • print_r can't output 5,5, it should give output similar to Array(cart => Array([0] => 5 [1] => 5)). However, if it really does give 5,5, then it means you put it in a string and that's why in_array doesn't work. And please do not post updates to your questions as answers - rather post it as comment or update the question. Commented Nov 17, 2014 at 2:51

2 Answers 2

3

So, according to you, $_SESSION['cart'] = "5,5"; and it means it is a string. So the right code to look up your value is strpos():

$pos = strpos($produit, $_SESSION['cart']);
if($pos !== false) {
    echo "YES";
}
else {
    echo "NO";
}

BUT there's a huge risk to get the wrong answer for this. Imagine, you have two products in your cart - first with id 15 and the other with id 7. You'll be looking for id 5. What would the above code output? It will output "YES".

So, instead of using a string, I suggest you use multidimensional array (if you want to stick with sessions). In this particular case then, the variable $_SESSION["cart"] would be an array and with adding new products it would look like this:

$_SESSION["cart"] = array(); // initial value, don't call it every time or it'll flush your array
$_SESSION["cart"][] = $product_ID;

Or similar to it.

print_r will give you a similarly-looking output:

Array(
    cart => array(
        [0] => 5
        [1] => 17
        [2] => 5
    )
)

Then, in_array should work. But, plan your storing wisely ;)

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

4 Comments

Thanks for your quick answer, According to what the script returns after my changes i guess i'm really having an array in there. Isn't there any other way to check for my value in it that would not require my to change the way I write $_SESSION ? This would be a pain!
@FélixDesmeules you are wrong, according to your output, you got a string in $_SESSION['cart'], not an array. An easier way? Well, there always is a way and you can store variables however you want. But in time as your code grows bigger, you will come to conclusion that it's a wrong way, trust me. So better rewrite it now than later ;)
Then my problem is everything currently works with strings, altough i'm sure you are right about the rewrite part, I will have to go this way for now. could I change the string to an array on this code ?
@FélixDesmeules if you want to keep strings, then store product ids in unique separators, for example [], so the string would look like $_SESSION['cart'] = "[5][3][3][5][5][17]"; and when you look for $produit, wrap it around in [ and ]. In this case you will not be getting wrong answers in case you searh for 5 in a string of 15,7,2.
0

As jon asked it is better put always the output of your program But for now I am suspecting your problem is in the in_array check this link A problem about in_array it might help

3 Comments

Your post does not answer the question completely, so in future please don't post it as answer, but as a comment under the question.
Got it, following are upcoming answers. Thanks
@Nordenheim They don't have the rep to put a comment.

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.