Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Session isnt passing variables into array

    For some reason my session variable isn't been populated with my items as they are being passed? Can anyone point me in the direction why?

    It will add an item into the array, however it will only display one at a time on the cart page. Meaning if I add one item A and then add an item B, item B will only be shown on the cart page.

    This is just a test i'm playing with to help understand php a little more - obviously I need more practice!

    Product Page
    $result = mysql_query ('SELECT * FROM stock WHERE item="redplate"');
    // fetch the resulting row as an associative array
    while ($row = mysql_fetch_assoc ($result)){
      echo '£', number_format( $row ['price'] / 100, 2, '.', ' ' );
    <form id="form1" name="form1" method="post" action="cart.php">
    <input type="hidden" name="item" id="item" value="redplate" />
    <input type="submit" name="button" id="button" value="Add to Shopping Cart" />
    Shopping Cart Page
    <?php session_start(); 
    <?php include 'dbconfig.php'//connects to database?>
    <?php include 'connect.php'//checks my credentials?>
    ////Adding something to the shopping basket from product page
    if (isset($_POST['item'])){
        $item = $_POST['item'];
        $wasFound = false;
        $i = 0; 
        //If cart is not set or cart array empty
        if (!isset($_SESSION['cart_array'])||count($_SESSION['cart_array']) < 1) {
        //Run if the cart is empty or not set
        $_SESSION['cart_array'] = array(1 => array('item' => $item, 'quantity' => 1));
        //Run if the cart has at least one item
         foreach ($_SESSION['cart_array'] as $each_item) {
         while (list($key, $value) = each($each_item)) {
            if ($key == 'item' && $value == $item) {
            //This item is already in our shopping cart so add one to quantity
            array_splice($_SESSION['cart_array'], $i-1, 1, array(array('item' => $item, 'quantity' => $each_item['quantity']+1)));
            $wasFound = true;
         } //Close if condition
    } //Close While loop
    }//Close foreach loop
    if ($wasFound == false) {
    array_push($_SESSION['cart_array'],array('item' => $item, 'quantity' => 1));
    //////If a user empties their cart
    if (isset($_GET['cmd'])&& $_GET['cmd']=="emptycart")
    /////This will empty their cart and reset array
    ///// Puts cart together for shopper to view
    if (!isset($_SESSION['cart_array'])|| count($_SESSION['cart_array'])<1){
    $cartOutput = "<h2 align='center'>Your shopping basket is empty</h2>";
    } else {
        $i = 0;
        foreach ($_SESSION['cart_array'] as $each_item){
        $item_name = $each_item['item'];
        $sql = "SELECT * from stock WHERE item = '$item'";
        $result = mysql_query($sql) or die(mysql_error());
        while ($row = mysql_fetch_array($result)){
        $cartOutput = "<h2>Your Shopping Basket $i</h2>";
        $cartOutput = "Item Name: " .$each_item['item']. "<br />";
        $cartOutput = "Item Quantity: ". $each_item['quantity']. "<br />";
        //while (list($item, $price) = each($each_item)){
        //$cartOutput = "$key: $value<br />";
        //  }
    //If user chooses to empty their shopping cart)
    if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") 
    <a href="cartaction.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    When I output the Array using dump I get:
    array(1) { ["cart_array"]=> array(1) { [0]=> array(2) { ["item"]=> string(0) "" ["quantity"]=> int(2) } } }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    This is more complicated than you need it to be. array_splice in particular has some overhead to it, so I'd reserve it for when removing a single item from the cart instead of when adjusting an existing item within ot.
    The results you have do concern me though, 'item' is listed as an empty string, but I can't see why it is. 'item' is submitted as 'redplate' from the html form so that should be fine.

    As for cleaning up:
    1. If no cart exists:
    - Create cart
    2. If item exists in cart
    - increment quantity
    3. else
    - insert into cart.
    Simple as that.

    PHP Code:
    if (isset($_POST['item']))
        if (!isset(
    $_SESSION['cart_array'] = array();
    // we will not insert it at this time.
    $bExists false;
        foreach (
    $_SESSION['cart_array'] AS &$item)
            if (isset(
    $item['item']) && strcasecmp($item['item'], $_POST['item']) == 0)
    $bExists true;
    $item['quantity'] = isset($item['quantity']) && is_int($item['quantity']) ? ++$item['quantity'] : 1;
        if (!
    // doesn't exist, so insert it in.
    $_SESSION['cart_array'][] = array('item' => $_POST['item'], 'quantity' => 1);

    I cheated and used a foreach with a break. If you don't want to do that using for or while would be fine too, but watch the use of the reference on $item which you cannot get from a list or for loop.


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts