Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006
    Posts
    4

    Exclamation Unanswered: save a object to a $_Session

    I am trying to build a very simple shopping cart.
    I created a cart class - allows me to add, delete all etc..

    I have a store front, that directs to items to be bought.
    On the items page a 'add to cart' button that adds the selected item to the cart:
    ###########################
    nclude "gCart.php"; <-- cart class
    session_start();
    $cart =& $_SESSION['gCart'];
    if(!is_object($cart)) ------------------------------------------------ <--- In store front.php
    $cart = new gCart();
    ############################
    nclude "gCart.php"; <-- cart class
    session_start();

    if($_post['add'])
    {
    $product = $products[$_POST['id']]; ----------------------------------- <--- In item.php
    $cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
    }
    that works fine.
    $_SESSION['gCart'] = $cart; <----- ???? (I WANT TO SAVE updated OBJECT)

    Anyone help?? I am really new to php, but know java fairly well.

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    You can do this. Your logic for getting/testing/creating should be this -
    PHP Code:
    include "gCart.php";
    session_start();
    // check if session variable exists, if so, set $cart, if not create new instance
    if (isset($_SESSION['gCart'])){
    $cart $_SESSION['gCart'];
    } else {
    $cart = new gCart();
    // you can probably also do the following, so that the session variable can be used directly -
    $_SESSION['gCart'] = new gCart();
    }
    // code to add items here
    $cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
    // note, if you set $_SESSION['gCart'] as above, so it is an instance of the class, you can probably do the following (but don't quote me on this.)
    $_SESSION['gCart']->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);

    // save/set the results back into the session variable
    $_SESSION['gCart'] = $cart// this will update the session variable if it exists or create it and set it if it does not exist 
    Edit: I just confirmed the functioning of the $_SESSION only method. Therefore, if you want, the above code becomes this -
    PHP Code:
    include "gCart.php"
    session_start(); 
    // check if session variable exists, if not create new instance 
    if (!isset($_SESSION['gCart'])){ 
    $_SESSION['gCart'] = new gCart(); 

    // code to add items here 
    $_SESSION['gCart']->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']); 
    Last edited by dbmab; 07-18-06 at 20:49.

  3. #3
    Join Date
    Jul 2006
    Posts
    4

    Thumbs up Thanks a Ton!

    The part that I was missing after reading your thread was

    $_SESSION['gCart'] = $cart; I was not "reloading" the saved information on the new pages. You helped out huge!

    Thnks.

Posting Permissions

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