Results 1 to 2 of 2

Thread: shopping cart explain please?

  1. #1
    Join Date
    Jun 2003

    shopping cart explain please?

    Hi all i have a shopping cart, which uses db.php which contains the database details, db.php also contains:

    function GetCartId()

    return $_COOKIE["cartId"];

    setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
    return session_id();

    Now this is all in db.php along with the mysql_link and all of that.

    db.php is refered to when search results are done, and in cart.php.

    Now the coding for cart.php for adding an item is:

    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    There is also a table called cart which is as follows:

    create table cart
    cartId int auto_increment not null,
    cookieId varchar(50),
    itemId int,
    qty int,
    primary key(cartId),
    unique id(cartId)

    Now could someone please explain to me how this setup actually works(im a newbie to this and it will help my understanding and also help in making advances to this setup)?

    Many Thanks


  2. #2
    Join Date
    Feb 2003
    Derby, UK
    I will do my best; in order to operate a shopping cart system you need a way to tie the user to their cart items (as opposed to someone else's). Usually this is done directly or indirectly with cookies (I will assume you know what a cookie is, though you said you are a newbie I am guessing PHP newbie not toal web newbie?).

    So, the general idea is that when a user of your site first tries to access the cart they are assigned a cookie with a unique id and from then on (at least until the cookie/cart is cleared) this id will stay with them and identify their cart items.

    In your code any access to the cart presumable fuirst calls the GetCartId function. This function checks to see if they already have the cookie (a cookie called 'cartId') and either returns it if they have or assigns them one if they haven't. In order to ensure that the cartid they are given is unique, the function uses PHPs built in sessions (again I will assume you understand the concept of sessions). By starting a session for this user, PHP will automatically assign thenm a unique session id, which is then also used as the cookie id:-

    setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));

    This line sets a cookie called cartID, with the value of the PHP session id - session_id() is a built-in function -and such that it will last for 30 days (the last parameter for the setcookie call is a time expressed in seconds since 1970and this line takes the current time - time() - and adds 3600 (=1 hr) * 24 (=1 day) * 30.

    While it would have been possible for the function to use the session id directly as the key into the table, the PHP session only lasts for one visit, so by setting a cookie with the value, the cart is made semi-permanent, provided the user comes back on the same machine within 30 days.

    So then in the cart table we have

    cartId int auto_increment not null,
    - primary key allows you to access each line individually
    cookieId varchar(50),
    - this is waht identifies which user it belongs to
    itemId int,
    - this is an identifier for the type of item, which allows you to have different items in the cart at the same time, e.g. 3 fish, 5 loaves and 1000 people.
    qty int,
    - how many of this item the user has in the basket

    so one record in the table represents the number of items of a particular type that a particular user currently has in the cart. There can be 0,1 or several lines for a given user, if they have added different types of item to the basket, equally there can be multiple lines for each item type but for different users. If a user has not selected any of a particular item, then there will be no line in the table at all for that user and item (NOT a line with 0 as the qty).

    cartid - cookieid - itemid - qty
    1 - 1 - 1 - 2
    1 - 1 - 2 - 5
    1 - 2 - 3 - 4
    1 - 4 - 5 - 1

    - User 1 has 2 items of type 1 (line 1) and 5 items of type 2 (line2) currently in the basket.
    - User 2 has 4 items of type 3 (line 3) in the basket
    - User 4 has 1 item of type 5 (line 4) in the basket
    - User 3 has nothing in the basket (no lines with cookieid = 3)

    Lastly in the add call

    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    this finds out whether this user already has any of the item in question in the basket. If the query returns 1 (or more but should probably only be one) then I imagine the code performs an update query to add the new items to the ones already in the basket (i.e. it changes the qty in the existing row rather than creating another row for the same item). If the query returns 0 it means this user has not currently got any of these items in the basket so I imagine there will be an insert query to create a new line of that item type for that user.


    Last edited by DaiWelsh; 06-25-2003 at 08:07 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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