SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Create Shopping Cart Session

    Hi all, having a bit of a problem. I'm building an online shop, I've now got to the stage where I need to build the shopping cart system. I'm adding the products to the basket using session but I seem to be having a problem, they're being added but not quite as I like! Please see my code below:

    PHP Code:
    $_SESSION['cart'][]['prod'] = $_POST['prodName'];
    $_SESSION['cart'][]['id'] = $_POST['prodId'];
    foreach(
    $_SESSION['cart'] as $id=>$value):
        echo 
    "You have added ".$value['prod']." which is product ref!".$value['id']."<br/>";
    endforeach; 
    This is the output when I print the array $_SESSION, I have various products added at the moment:

    PHP Code:
    Array ( 
    [
    cart] => 
    Array ( [
    0] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [1] => 
    Array ( [
    id] => 1415 ) [2] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [3] => 
    Array ( [
    id] => 1415 ) [4] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [5] => 
    Array ( [
    id] => 1415 ) [6] => Array ( [prod] => Manfrotto 190XDB MN 804RC2 Head ) [7] => 
    Array ( [
    id] => 1428 ) [8] => Array ( [prod] => Manfrotto 190XDB MN 804RC2 Head ) [9] => 
    Array ( [
    id] => 1428 ) ) ) 

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would you like?
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there, I want the id and name to be in the same array but under one large array (cart).

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this?

    <?php

    $cart = array();

    $arrProductItem = array("1"=>"Some product");
    array_push($cart, $arrProductItem);

    $arrProductItem2 = array("2"=>"Some product 2");
    array_push($cart, $arrProductItem2);

    print_r($cart);

    ?>
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  5. #5
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh..
    output is:

    Array ( [0] => Array ( [1] => Some product ) [1] => Array ( [2] => Some product 2 ) )
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, thanks for the message. Managed to fathom it out in the end. Instead of:

    PHP Code:
            $_SESSION['cart'][]['prod'] = $_POST['prodName'];
            
    $_SESSION['cart'][]['id'] = $_POST['prodId']; 
    I replaced it with the following and it's working perfectly

    PHP Code:
            $_SESSION['cart'][$i]['prod'] = $_POST['prodName'];
            
    $_SESSION['cart'][$i]['id'] = $_POST['prodId']; 

  7. #7
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good on ya!
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys, got a small problem, my $_SESSION is only accepting two products, if I had another then it just replaced the second array. Please see my code below:

    PHP Code:
    class cart {
        function 
    addItems(){
            
    $i=0;
            
    $_SESSION['cart'][$i]['prod'] = $_POST['prodName'];
            
    $_SESSION['cart'][$i]['id'] = $_POST['prodId'];
            
    $i++;
            
    //print_r($_SESSION);
            //session_destroy();

        
    }

    I can see what I'm doing wrong, each time I call the method it starting back at 0 - not sure of a way around this! Really appreciate any help

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you show us the form you're using please?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Morning buddy, yeah sure here is my form...

    PHP Code:
    <?php
    if(isset($_POST['cartadd'])):
    $cart = new cart();
    $cart->addItems();
    endif;
    ?>

    <form action="<?php echo $_SERVER['REQUEST_URI']?>" method="post">
            <input type="hidden" name="prodName" value="<?php echo $result['products_name'?>"/>
                <input type="hidden" name="prodId" value="<?php echo $result['products_id'?>" />
                    <input type="submit" name="cartadd" value="Add to Cart" />
        </form>

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well, ideally you want to store your cart like so...
    PHP Code:
    <?php
    $cart 
    = array(
      
    => array(
        
    'name'      => 'Foo Biscuits',
        
    'quantity'  => 56,
        
    'price'     => 1.99
      
    ),
      
    => array(
        
    'name'      => 'Bar Sandwich',
        
    'quantity'  => 3,
        
    'price'     => 4.99
      
    ),
    );
    You would then add a product like this...
    PHP Code:
    <?php
    $cart
    $_POST['product_id'] ] = array(
      
    'name'  => $_POST['product_name']
    )
    ...and iterate over them a bit like...

    PHP Code:
    <?php
    foreach($cart as $id => $product){
      
    printf(
        
    'You have %d of (%d)%s at %f each' PHP_EOL,
        
    $product['quantity'],
        
    $id,
        
    $product['name'],
        
    $product['price']
      );
    }

    /*
      You have 56 of (1)Foo Biscuits at 1.990000 each
      You have 3 of (2)Bar Sandwich at 4.990000 each
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, thanks for the message, massive help, also think I've worked out a solution, not sure if it's a good way forward, what are your thoughts?

    PHP Code:
    class cart {
        function 
    addItems(){
            
    $_SESSION['cart'][$_SESSION['count']]['prod'] = $_POST['prodName'];
            
    $_SESSION['cart'][$_SESSION['count']]['id'] = $_POST['prodId'];
            
    $_SESSION['count']++;
            
    //print_r($_SESSION);
            //session_destroy();

        
    }


  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I don't see why 'count' is needed?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, no I guess it's not really, ok think I'll drop that bit, it's kind of pointless...late night, maybe that's why I'm being a bit of a noob! Cheers bud

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ha.

    No worries.

    Anthony.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  16. #16
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may look this cart.


Bookmarks

Posting Permissions

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