Page 1 of 1

Count() array

Posted: Wed Dec 03, 2008 1:26 pm
by Frozenlight777
I have an associative array of sorts and would like to count the quantities as a total.

Code: Select all

$_SESSION['cart'][$_GET['id']]['id'] = $_GET['id'];
$_SESSION['cart'][$_GET['id']]['qty'] += $_GET['qty'];
 
foreach($_SESSION['cart'] as $item){
     echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
}
and that displays
ID:25 , Qty: 12
ID:24 , Qty: 3
ID:1 , Qty: 2

which is fine, but how can I get it to display a total Qty such as 17 in this case?

I tried:
echo count($_SESSION['cart']);
which returns 3 and thats accurate but not what i'm looking for....

I also tried
echo count($_SESSION['cart']['qty']);
which didn't work....


any ideas?

Re: Count() array

Posted: Wed Dec 03, 2008 1:38 pm
by dude81
Here
Frozenlight777 wrote:

Code: Select all

 
$_SESSION['cart'][$_GET['id']]['id'] = $_GET['id'];
$_SESSION['cart'][$_GET['id']]['qty'] += $_GET['qty'];
 
foreach($_SESSION['cart'] as $item){
     echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
}
do something like this

Code: Select all

 
$total=0;
foreach($_SESSION['cart'] as $item){
     echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
    $total = $total +  $item['qty'];
}
echo $total;
 

Re: Count() array

Posted: Wed Dec 03, 2008 1:39 pm
by Frozenlight777
Wow i'm an idiot :banghead:

Thank you.

Re: Count() array

Posted: Wed Dec 03, 2008 3:52 pm
by Frozenlight777
Bleh now I'm stuck again.

I have:

Code: Select all

 
$total_qty= 0;
    foreach($_SESSION['cart'] as $item)
    {
    echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
    $total_qty = $total_qty +  $item['qty'];
    }
    echo "You have " . $total_qty . " items";
    echo "<br/>";
 
which works perfectly... Each ID has a unique price which is being queried from the database and is currently stored in $row[pd_price]...
So how can I calculate the total for each ID?

Right it displays,
ID:25 , Qty: 5
ID:24 , Qty: 1
ID:1 , Qty: 5
You have 11 items

so how would calculate the total for something like
$total = (price_for_ID25 * 5) + (price_for_ID24 * 1) + (price_for_ID1 * 5)
in the loop somehow?

Re: Count() array

Posted: Wed Dec 03, 2008 5:11 pm
by dude81
Exactly as per the formula $total = (price_for_ID25 * 5) + (price_for_ID24 * 1) + (price_for_ID1 * 5)

Code: Select all

 
 $total_qty= 0;
$total_price =0;
     foreach($_SESSION['cart'] as $item)
     {
 
     echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
     $price_for_id = $row['price']
     $total_qty = $total_qty +  $item['qty'];
     $total_price = $price_for_id*$item['qty'] + $total_price;
     }
     echo "You have " . $total_qty . " items";
     echo "<br/>";
 

Re: Count() array

Posted: Wed Dec 03, 2008 5:29 pm
by Frozenlight777
Yeah, I ended up getting that, but there's a different price for a different ID, the price isn't static.

Re: Count() array

Posted: Wed Dec 03, 2008 5:36 pm
by dude81
Frozenlight777 wrote:Yeah, I ended up getting that, but there's a different price for a different ID, the price isn't static.

I knew that, I thought you are smart enough that you would know to fix it :wink: . Hope it is solved now :?:

Re: Count() array

Posted: Wed Dec 03, 2008 5:51 pm
by Frozenlight777
haha well my head isn't quite working well right now. I did this, but it is inaccurate

Code: Select all

                       
                        $total_qty= 0;
            $total_price = 0;
            $price = $_GET['price'];
            foreach($_SESSION['cart'] as $item)
            {
                echo "ID:" .  $item['id'] . " , Qty: " . $item['qty'] . "<br />";
                $total_qty = $total_qty +  $item['qty'];
                $total_price = $price * $item['qty'] + $total_price;
                
            }
            echo "You have " . $total_qty . " items";
            echo "<br/>";
            echo $total_price;
 

what it displays is
ID:1 , Qty: 2
ID:24 , Qty: 1
ID:25 , Qty: 2
You have 5 items
25

where the price for ID:1 is 5, the price for ID:24 is 15 and the price for ID:25 is 10... so the math isn't working correctly, so the total should be 30


sorry let me clear this up, the foreach is executing every time the "add" button is pressed so it's multiplying the quantity more than it should

Re: Count() array

Posted: Wed Dec 03, 2008 6:08 pm
by dude81
Is this correct?

Code: Select all

 
$_SESSION['cart'][$_GET['id']]['qty'] += $_GET['qty'];
 
If not then the solution should be and problem should be solved
Also as per the math your sum should be 40
i.e.

Code: Select all

 
$total_price = 2*5 +1*15+2*10
// ID1Price*ID1_quantity + ID2Price*ID2_quantity +ID3Price*ID3_quantity