0

I am willing to create a Shop Cart using simple PHP SESSION ARRAY. I tried to search different StackOverFlow problems, but none of those is giving me exact solution of my problem. Maybe I am doing any silly mistake. However,

I am doing this:

<!-- SHOPPING CART -->
<?php
if(isset($_REQUEST['atc']))
{
    $item = $_REQUEST['atc'];
    $_SESSION['cart'] = array();

    array_push($_SESSION['cart'], $item);
    //$_SESSION['cart'][] = $item;

     foreach($_SESSION["cart"] as $key => $val)
    { 
        echo $key . ">" . $val;
    }
}
?>
<!-- SHOPPING CART -->

I am receiving $_REQUEST['ate'] (Integer Value/Product ID) when User is Clicking "ADD TO CART" Button on the same page. Then I am putting the value in $item, then I am declaring $_SESSION['cart'] as Array. Then I have tried array_push, and even tried $_SESSION['cart'][] to Push Integer Value. But each time only the first element is updated, therefore $_SESSION['cart'][0] is storing the value, not $_SESSION['cart'][1] or the rest.

3
  • Are you using AJAX to call this file or how do you manage "when User is Clicking"? Commented Dec 7, 2013 at 14:34
  • No AJAX, just simple PHP. Commented Dec 7, 2013 at 14:35
  • <a href=\"".$_SERVER['PHP_SELF']."?atc=".$songs['song_id']."\" title=\"Click Here to Add this song to Cart\" class=\"buy\" target=\"\"><img src=\"images/cart.png\"></a> Commented Dec 7, 2013 at 14:37

2 Answers 2

1

The problem is that you redefine $_SESSION['cart'] every time as an empty array via $_SESSION['cart'] = array(); and then only push one element.

Try this

if(isset($_REQUEST['atc']))
{
    $item = $_REQUEST['atc'];

    if (!isSet($_SESSION['cart']))
        $_SESSION['cart'] = array();

    array_push($_SESSION['cart'], $item);
    //$_SESSION['cart'][] = $item;

     foreach($_SESSION["cart"] as $key => $val)
    { 
        echo $key . ">" . $val;
    }
}

Now only the first time a user wants to add an item, $_SESSION['cart'] will be initiated as an empty an array. The second time ($_SESSION['cart'] already is an array with one element), the second element will properly be pushed.


If you want the elements to be unique (as said in comments), you can use the elements id as key (and an array can only have unique keys).

if(isset($_REQUEST['atc']))
{
    $item = $_REQUEST['atc'];

    if (!isSet($_SESSION['cart']))
        $_SESSION['cart'] = array();

    if (!array_key_exists($item, $_SESSION['cart']))
        $_SESSION['cart'][$item] = 1;
    else
        $_SESSION['cart'][$item]++;

    foreach($_SESSION["cart"] as $key => $val)
    { 
        echo $key . ">" . $val;
    }
}

This will first check, if the item is already in the cart (array_key_exists), if not it will be added. If it is, it will increment the value, so you can keep track how often a specific item is in the cart (if you don't want that functionality, just lose the else statement)

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

2 Comments

You are welcome. If this (or devBinnooh's) answer helped you, please consider upvoting and/or marking an answer as accepted
alright sure, I will. Can you give me one more solution? How can I add distinct element in an array. Therefore, the products are unique. So, if visitor adds multiple same item, only one Item-ID will be added to the Array.
0

The problem you are facing is that you override $_SESSION['cart'] every time you get an item. Try

 if(!isset($_SESSION['cart'])
    $_SESSION['cart'] = array();

    //array_push($_SESSION['cart'], $item);
    $_SESSION['cart'][] = $item;

Check first that the session is not already exist, then add items.

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

1 Comment

Can you give me about one more solution? How can I add distinct element in an array. Therefore, the products are unique. So, if visitor adds multiple same item, only one Item-ID will be added to the Array.

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.