Ezilon.com - Target Your Audience, be Seen in Your Region

Need Help with a PHP counting Loop

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 359 posts

Joined: Mar 1999

I need to get the total from an array of $$ amounts contained in a mysql query. I know I need to use a loop to do it, but I can't figure out exactly how to implement it.

The field sprice contains the amount that I want added up. Basically, it's a very simple shopping cart.

<?php
$sum
= mysql_query ("select sprice from sell_quote where orderid = '$cartid' and complete = -1");
if (
$row1 = mysql_fetch_array($sum)){
$sprice = $row1["sprice"];
}
?>

'

Any help will be greatly appreciated,

Dan
Recycle Video Games Network

Stupidity killed the cat, curiosity was framed!

Mark Hensler's picture

He has: 4,044 posts

Joined: Aug 2000

just add all the sprices, right?

<?php
$total
= 0;
$sum = mysql_query ("select sprice from sell_quote where orderid = '$cartid' and complete = -1");
if (
$row1 = mysql_fetch_array($sum)){
$sprice = $row1["sprice"];
$total += $sprice;
}
?>

'

Mark Hensler ["Max Albert"] [Email]
If there is no answer on Google, then there is no question.

They have: 359 posts

Joined: Mar 1999

Thanks Mark,

Got me started in the right direction anyway. As was listed above, it would only return the first $sprice in $total.

After consulting the PHP manual "again", it occurred to me that with the 'if' statement, it really wasn't creating a loop. Changing the 'if' to 'while' fixed the problem and life is good and I like computers again. Smiling

Dan
Recycle Video Games Network

Stupidity killed the cat, curiosity was framed!

Mark Hensler's picture

He has: 4,044 posts

Joined: Aug 2000

sorry I missed that..

They have: 359 posts

Joined: Mar 1999

You can stop beating your face on your desk. I think the World has enough Bulldogs.

Although, if you feel it is necessary and want to feel like you have accomplished something at the same time, I would recommend chasing parked cars. Smiling

Dan
Recycle Video Games Network

Stupidity killed the cat, curiosity was framed!