Re: Shopping Cart Schema - Sessions

From: Date: Wed, 10 Jan 2001 17:33:18 +0000
Subject: Re: Shopping Cart Schema - Sessions
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-33696@lists.php.net to get a copy of this message
Hi JB! On Wed, 10 Jan 2001, JB wrote: > 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... > yap, just like that! have you tried it yet? Just remember to test if the array variable isn't already set so not to overwrite the old values by accident (use session_is_registered(), I think) > 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. > My advice is to have a clear idea of what you really want, and write it down first. Clearely formulate your problem to solve, and don't jump at coding as most guru coders do :) Start simple. > so.. all i need is a good simple method to register the following variables: > uid (user id) > p1 (product id for item 1) > q1 (qty for item 1) > [products.php] [cart.php] {uid, product_id, quantity} ---> {$cart, $user} in cart.php you do: session_start(); // don't mix different data (products w/ user data) // have them in separate entities if (!session_is_registered('cart')) { $cart = array(); session_register('cart'); } if (!session_is_registered('user')) { $user = array(); session_register('user'); } // you may have different operations on the cart. // let's say we add something, so we receive {uid,product_id, quantity} // from products.php ... $cart[$product_id] = $quantity; I am not sure why do you need user id, as every session is `customized' for a user, who has a corresponding session_id in your application. > and a method to increase the next variable to p2, q2, p3,q3 etc to represent > other items in the cart. for this example, I thought the `key' in the array to be the product_id (a user can have one or more products identified by their id). > 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. You must view it this way when writing your php code for the problem: you have a session associated with a user (1 to 1 relationship) in which you store pertinent data related to users actions. We call it session because it persists over several requests. A user can request one or more products (a 1 to n relationship), and the information will be stored in the session. Simple, isn't it? :) cheers, -- teodor

« previous php.general (#33696) next »