SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting items in basket inc. quantities

    I writing a little script to count the total number of items in a shopping basket. It's fairly straight forward counting the amount of different items, but the problem I'm having is counting the items with more than 1 as the quantity. The code below generates the go to checkout button, but I'm also using it to get the number of items in the basket.

    Code:
    for($i=0; $i < count($cartContents); $i++){
    $val = $i + 1;
    $counting = count($cartContents);
    $items .= "
         <input type='hidden' name='item_name_$val' value='". $cartContents[$i][1] ."' /> 
         <input type='hidden' name='amount_$val' value='". $cartContents[$i][3] ."' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_$val' value = '" . $cartContents[$i][2] . "' />     
    ";
    }
    count($cartContents) - Gets number of different items
    $cartContents[$i][2] - Gets the quantity of each item

    Can anyone help me with the code to get the total number of items please?

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    I'm not quite sure what $counting is doing, but this will get you the total number of items:
    Code:
    $total_items = 0;
    for($i=0; $i < count($cartContents); $i++) {
        $total_items += $cartContents[$i][2];
    }
    Hope that helps.

  3. #3
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked perfect, thank you very much. Really appreciated!

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    No problem, glad to help.


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
  •