SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to count the number of PRODUCTS bought in a cart.

    A shopping cart has a table called "cart" where the customer's selections are stored.
    The customer buys 1 item of Product C, 2 items of Product H and 3 items of Product T.
    I need to be able to count the number of PRODUCTS bought NOT the number of items. i.e. the answer here is 3.
    What is the EASIEST way to do this?
    Thanks.
    :

    :

  2. #2
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on your table structure, why don't you post it?

    But the command you're looking for is something like
    SELECT COUNT( DISTINCT *something* )

  3. #3
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought about using the line below:

    productqty int(3) NOT NULL default '1',
    and then counting it somehow.

    Will the following work?

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

    $item_result = mysql_query($result);

    $item_count = mysql_result($item_result,0,"sum(productqty)" );

    THE TABLE>>>>>>>>>>>>>>>>

    $sql2 = ("CREATE TABLE cart (
    cartId int(11) NOT NULL auto_increment,
    cookieId varchar(50) default NULL,
    itemId int(11) default NULL,
    qty int(11) default NULL,
    itemName varchar(50) default NULL,
    productqty int(3) NOT NULL default '1',
    PRIMARY KEY (cartId),
    UNIQUE KEY id (cartId)
    ) TYPE=MyISAM;" );
    :

    :

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT COUNT( DISTINCT itemID ) FROM car WHERE cookieID = '" ... '" )

    You don't necessarily need the distinct portion, but you can count the item IDs. Or heck, you can do select count( * ) and it will work, too.

  5. #5
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, MattR
    :

    :


Bookmarks

Posting Permissions

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