Re: Shopping Cart Schema - Sessions

From: Date: Wed, 10 Jan 2001 09:09:26 +0000
Subject: Re: Shopping Cart Schema - Sessions
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-33682@lists.php.net to get a copy of this message
ok, this all looks good, but then I again I am having a problem figuring out that code you wrote below. the code i'm attempting to write isn't going to be complex. there's maybe 10 or 15 items going to be sold. it would be very rare if the customer ever had 1 or 2 things in their basket, let alone more than that. i think i'm looking for a simpler solution. from what i had read, though i couldn't get to work, this would be an acceptable method of starting a sessions and trapping the variables without a million session_register calls. session_start(); session_register("sessvar"); $sessvar["user"]["name"] = $login_name; $sessvar["cart"]["item1"] = $prod_id; $sessvar["cart"]["qty1"] = $qty; Then.. I should be able to access them like: print "$sessvar['user']['name']"; etc... does this make sense to you? or anyone else? all i need is a sure fire way to easily hold a few variables throughout the site. i'm not concerned about holding the keys in a db, as with the small activity on this site, and the fact the writing to a local file will not rpoduce any noticable difference over a db query on my machine w/ its traffic, the db is not needed for this. so.. all i need is a good simple method to register the following varables: uid (user id) p1 (product id for item 1) q1 (qty for item 1) and a method to increase the next variable to p2, q2, p3,q3 etc to represent other items in the cart. i think i should be able to figure out how to manipulate/extract the data and work with it from there (in an array) once i am able to get them registered. really i would just need a method for walking through the array(s) to pull out the data. but if anyone has any suggestions on that too i'd love to hear em. thanks for the help everyone. > > Hey, > > > > I'm looking for little information from those who have coded their own shopping cart apps. what would you say the best way to setup a cart would be? more specificly, the method for adding items to the cart. > > > eww, lost wrapping ... > > > say the user is broswing around. when they entered the website a new session was created. when they go to add an item, what is the best way store variables (such as what they bought and quantity) in the session. i have read that it is possible to put all of your session variables in a single associative array. however, i have not had any luck doing this myself. Under such a method of an array, how would i store each item (product id and quantity, possibly price as well) in the cart into the session. obviously i don't expect anyone to write an entire app. i am comfortable starting the session, and doing th db extraction and presentation. i just need to know how to store a variable in a session (passed through post) and how to extract it when need be (considering it is an array). Thanks a lot! > > I codded a shopping cart which allowed thin sessions' and > fat sessions'. > > Thin would mean I store only the IDs, then retrieve the rest of the > information about the product from the DB (more DB queries, but smaller > session data), while `fat' would mean I store all the data I need to display > when the user invetories his/her cart, namely name, price &|short desc., > quantity. > > Some code snippets would be: > > if (!$SES->isRegistered ('cart.object')) { > $CART = new Cart(); > $CART->setLanguage (CART_LANGUAGE); > $SES->setAttribute ('cart.object', $CART); > } > > $CART = &$SES->getAttribute ('cart.object'); > > then go ahead and use the cart object: > ... > $CART->addItem ($sku, 1, new Product($arr_attr)); > $CART->dropItem ($sku); > > And the Product class has among others, these two methods: > > __setStorageType ($t) > > if ($t == CART_FAT_SESS) { > $this->_slots = array_keys (get_object_vars ($this)); > unset ($this->_slots['_desc']); > } > } > > function __sleep () > { > return (isset ($this->_slots) ? $this->_slots : array ('_quant')); > } > > This is in the manner of Session class from PHPLIB which records what is to > be saved in the session (in this case, what attributes.) > > And the Cart class has: > > function __wakeup () > { > if ($this->STORAGE == CART_THIN_SESS) { > $pr_attrs = Cart::getItemsAttr (array_keys($this->_items)); > > foreach ($pr_attrs as $sku => $attrs) { > $attrs['quant'] = $this->_items[$sku]->getQuantity(); > $this->_items[$sku] = new Product ($attrs); > } > } > } > > Note that __sleep() and __wakeup() are PHP serialisation hooks, which can customize > the marshalling/unmarshalling process (e.g. restore DB connections, etc.) > > cheers, > > -- teodor > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#33682) next »