#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Problems with session array


    I'm building a shopping cart that stores items "added to cart" posted by hidden form field in a $_SESSION['cart'] variable. I have it save the number in cart, and that is the count for the session array. However, I cannot seem to get my program to output the list in the array. Here is my code:

    included header.html:
    PHP Code:
    <?php
    session_start
    ();

    if (empty(
    $_SESSION['count_cart'])) {
        
    $_SESSION['count_cart'] = 0;
    }

    if (
    $_POST['name'] == $PHPSESSID && isset($_POST['mdl'])) {
        
    $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $mdl $_POST['mdl'];
        if (isset(
    $cartArr)) {
            
    $cartArr[]=$_POST['mdl'];
        }else{
            
    $cartArr=array();
        }
        
    $_SESSION['cart'] = $cartArr;
    }
    ?>

    File where header is included:
    PHP Code:
    <?php
    foreach ($_SESSION['cart'] as $key=>$value)
    {
        echo 
    $key.".) ".$value;
    }
    ?>
    I'm not getting the results I require and I've been working on it for too long now. The count is increasing, the list is NOT printing and when I can get it to print with some horrible code, it prints 1 array element behind where it should be. "Add to Cart" first time, empty, "Add another item", it shows item 0 instead of item 1. Can somebody help me?
    Last edited by BitZoid; March 22nd, 2013 at 03:20 PM.
    -- Success achieved from tribulation --
  2. #2
  3. hiding my <b> from ur <strong>
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    959
    Rep Power
    181
    Before I look more in depth, just making sure, by "header.html" you mean "header.php", right?
    ****
    Enjoy my post? Drop some props by hitting the scales button up top. JBL

    Website Design in Los Angeles and Washington, DC by PoweredPages.com
  4. #3
  5. hiding my <b> from ur <strong>
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    959
    Rep Power
    181
    What does a print_r output before you echo the parts of the array. I.E., what do you get when you do:
    PHP Code:
    <?php 
    print_r
    ($_SESSION['cart']);
    foreach (
    $_SESSION['cart'] as $key=>$value) { 
        echo 
    $key.".) ".$value

    ?>
    ****
    Enjoy my post? Drop some props by hitting the scales button up top. JBL

    Website Design in Los Angeles and Washington, DC by PoweredPages.com
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Array ( )

    is what I get
    -- Success achieved from tribulation --
  8. #5
  9. hiding my <b> from ur <strong>
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    959
    Rep Power
    181
    Oh, and $cartArr is going to be empty each time you go to the page. It's not stored page to page. So any time you run header.php, it's only going go to the first part of the following:
    PHP Code:
    if (isset($cartArr)) { 
        
    $cartArr[]=$_POST['mdl']; 
    }else{ 
        
    $cartArr=array(); 

    ****
    Enjoy my post? Drop some props by hitting the scales button up top. JBL

    Website Design in Los Angeles and Washington, DC by PoweredPages.com
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    where's the logic for fetching the current $_SESSION['cart'] and storing it in (I guess) $cartArr?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Nope - I've never worked with sessions before and am not great with php syntax.

    I would like to store every item that is added into the $_SESSION['cart'] array, but am not sure how.

    I have the above code, and here is the form that submits it:
    PHP Code:
    <form enctype="multipart/form-data" action="shoppingcart-customer.php" method="post">
    <input type="hidden" name="name" value="<?php echo $PHPSESSID?>"/>
    <input type="hidden" name="mdl" value="<?php echo $mdl_key?>"/>
    <input type="submit" name="addtocart" value="Add To Cart" id="hyperlink-style-button"/>
    </form>
    -- Success achieved from tribulation --
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Well I figured out sessions can be multidimensional arrays. I got it to work with this code..... thanks.....

    header.html
    PHP Code:
    <?php
    session_start
    ();

    if (empty(
    $_SESSION['count_cart'])) {
        
    $_SESSION['count_cart'] = 0;
    }

    if (
    $_POST['name'] == $PHPSESSID && isset($_POST['mdl'])) {
        
    $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $_SESSION['item'][$idx] = $_POST['mdl'];
    }
    ?>
    shoppingcart.php
    PHP Code:
    include (header.html);
    <?php
    foreach ($_SESSION['item'] as $key=>$value) { 
         echo 
    $key.".) ".$value
    }  
    ?>
    Last edited by BitZoid; March 22nd, 2013 at 04:32 PM.
    -- Success achieved from tribulation --
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I'd now like to pass these additional $_POST variables to the session in the current index count. How can I add these additional dimensions to my $_SESSION['item'] array?

    PHP Code:
    <input type="hidden" name="model" value="<?php echo $model?>"/>
    <input type="hidden" name="manufacturer" value="<?php echo $manufacturer?>"/>
    <input type="hidden" name="sellprice" value="<?php echo $sellprice?>"/>
    Last edited by BitZoid; March 22nd, 2013 at 04:55 PM.
    -- Success achieved from tribulation --
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Figured it out again.

    PHP Code:
    session_start();

    if (empty(
    $_SESSION['count_cart'])) {
        
    $_SESSION['count_cart'] = 0;
    }

    if (
    $_POST['name'] == $PHPSESSID && isset($_POST['mdl_key'])) {
        
    $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $_SESSION['item'][$idx][0] = $_POST['mdl_key'];
        
    $_SESSION['item'][$idx][1] = $_POST['model'];
        
    $_SESSION['item'][$idx][2] = $_POST['manufacturer'];
        
    $_SESSION['item'][$idx][3] = $_POST['sellprice'];

    PHP Code:
    foreach ($_SESSION['item'] as $value) { 
        foreach (
    $value as $value2)
            echo 
    $value2." ";
        }

    Last edited by BitZoid; March 22nd, 2013 at 06:20 PM.
    -- Success achieved from tribulation --

IMN logo majestic logo threadwatch logo seochat tools logo