0

I am trying to implement a 'shopping cart' where, if the 'Add to Cart' has been clicked, the respective item is either added to the cart or incremented by one if already present. However, the following code isn't working and giving me a "Cannot use a scalar value as an array" error. Any help would be highly appreciated.

if (isset($_POST['fan']) && ($_POST['fan']=="Add to Cart")) {
    if (($_SESSION['cart']['fan']==0) || (!isset($_SESSION['cart']['fan']))) {
      $_SESSION['cart']['fan']=1;
    } else {
      $_SESSION['cart']['fan']++;
    }
}
1
  • The code you posted won't produce the scalar error. Even with your undefined cart element, it will only produce an undefined index notice. You must have some other code that is causing the error. Find out which line it is and post it. Commented Oct 10, 2012 at 9:32

2 Answers 2

1

i suppose you may have forgot to add something like this:

if (!isset($_SESSION['cart']))
{
  $_SESSION['cart']=array();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to declare $_SESSION['cart'] as array() if it is not set,

$_SESSION['cart']=array();

Comments

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.