Session/object problem (and a general architecture question)

From: Date: Mon, 15 Oct 2001 18:20:29 +0000
Subject: Session/object problem (and a general architecture question)
Groups: php.general 
Request: Send a blank email to php-general+get-71126@lists.php.net to get a copy of this message
I'm developing a shopping cart system and have run into some problems. Any insight anyone can provide would be helpful. My first run at the problem involved creating a temp table in the database (Oracle 8.1.6) to store the items in the cart. At order time, these records were deleted after the order was written to the order page. This doesn't feel right. So now I'm trying to do this with classes and having lots of problems. This is what I have: cart.php Class Cart { var $items; //this is an array carrying item #, price, qty, desc var $product_total; var $shipping_total; var $tax_total; var $cart_id; function add($sell_price,$qty,$shipping,$tax,$product_id,$short_desc,$note) { //add the item } //other functions for deleting, updating, displaying }//end class include.php require_once("./include/cart.php"); require_once("./include/db_lib.php"); session_start(); if (!$sess_cart) { session_register('sess_cart'); $sess_cart = new Cart; srand((double)microtime()*1000000); $randval = rand(); $sess_cart->cart_id = md5($randval); } if(!$PHPSESSID) { session_register('sess_login'); session_register('sess_cart'); } else if((!$sess_login)||(!$sess_cart)) { session_register('sess_login'); session_register('sess_cart'); } My other scripts all have these lines at the top: require_once("./include/cart.php"); require_once("./include/db_lib.php"); session_start(); Running a script to select a product and add it to the cart gives me Fatal error: Call to a member function on a non-object If I echo gettype($sess_cart), it shows as a null. So my questions are: 1. What am I doing wrong with my session/object that the cart doesn't get instantiated? 2. Is an object the best way to do this? I want to use an array for $items, because I want to carry the item #, price, qty, and description to display for the customer. Is this going to require a 2 dimensional array? Would it be easier to just carry the item # and qty, and look up the other info every time the customer displays the cart? That seems like way too much database thrashing to me, and is what led me to abandon my first effort. Thanks for your time. Sheila

« previous php.general (#71126) next »