Calculations in For Loop

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

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Calculations in For Loop

Post by facets »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey Gang,

I'm having trouble grasping calculations in my loop.
Here's the loop, I wish to calculate total and print to grandtotal.
Can someone point me to an example of calcs in a for loop?

ta, Wil

Code: Select all

$query = "SELECT * FROM statements WHERE client_id = '".$id."'";
$dbh->query($query);

print "<table width=500><tr><td width=50>Total</td></tr>";

for ( $i = 0; $i < $dbh->numrows; $i++ ) {

$dbh->get();

$id = $dbh->rowdata[0];
$client_id = $dbh->rowdata[1];
$total = $dbh->rowdata[8];

print "<tr><td>$id</td><td>**$total**</td></tr>";
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
SimonJ621
Forum Commoner
Posts: 36
Joined: Thu Jul 27, 2006 5:07 am
Location: Ohio

Post by SimonJ621 »

What error are you getting?

Jason
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

What are you trying to do?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

thanks for the responses! I worked it out.

Code: Select all

$grandtotal = $total + $grandtotal;
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, I still have no idea what you are talking about, but you may want to replace this line:

Code: Select all

$grandtotal = $total + $grandtotal;
With this one:

Code: Select all

$grandtotal += $total;
The result will be exactly the same :wink:
Post Reply