PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Dec 03, 2008 1:26 pm
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?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Wed Dec 03, 2008 1:38 pm
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;
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Dec 03, 2008 3:52 pm
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?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Wed Dec 03, 2008 5:11 pm
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/>";
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Dec 03, 2008 5:29 pm
Yeah, I ended up getting that, but there's a different price for a different ID, the price isn't static.
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Wed Dec 03, 2008 5:36 pm
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
. Hope it is solved now
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Dec 03, 2008 5:51 pm
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
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Wed Dec 03, 2008 6:08 pm
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