Page 1 of 1

Calculations in For Loop

Posted: Sun Jul 30, 2006 6:41 am
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]

Posted: Sun Jul 30, 2006 6:52 am
by SimonJ621
What error are you getting?

Jason

Posted: Sun Jul 30, 2006 7:00 am
by Oren
What are you trying to do?

Posted: Sun Jul 30, 2006 7:25 am
by facets
thanks for the responses! I worked it out.

Code: Select all

$grandtotal = $total + $grandtotal;

Posted: Sun Jul 30, 2006 8:09 am
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: