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!
print_r($_SESSION['cart']);print_rcan't output5,5, it should give output similar toArray(cart => Array([0] => 5 [1] => 5)). However, if it really does give5,5, then it means you put it in a string and that's whyin_arraydoesn't work. And please do not post updates to your questions as answers - rather post it as comment or update the question.