Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    Shopping cart development with sessions

    All right, I can't figure out the best way to do this.

    I want to create a shopping cart on my site which will allow users to add multiple items to their cart. Sounds easy enough. I've done it client side, but now am "expanding my horizons" with server side.

    What I would like to do is register a session array named items, which will contain the SKUs of the product ordered.

    So every time someone "adds" an item to their cart it pushes this array and maintains the data throughout the session. Only problem is I can't get that to work.

    I've tried the following:

    session_start();
    session_register('items[]');
    array_push($items,$sku);

    but I get an items not defined error.

    Is this how you would go about it? Is there a better way? Why doesn't the method above work?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It would probably be better to create a shopping cart class. Within that class you can writen any methods needed such as adding items, deleting items, showing items, etc. Then you would simply instantiate that class first and then register it with that session. For example:

    PHP Code:

    class MyShoppingCart

    var 
    $items = array(); 

    function 
    add_item($sku) { 
     if (
    $this->items[$sku]){
     
      
    $this->items[$sku] = $this->items[$sku] + 1

    else {
     
    $this->items[$sku] = 1;
     } 


    function 
    delete_item($sku) { 
     
    $this->items[$sku] = 0
    }


    Then when they want to use the cart:

    $cart = new MyShoppingCart();
    session_register("cart");


    oila!!
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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