Re: Addition question.

From: Date: Sat, 19 Aug 2000 02:19:26 +0000
Subject: Re: Addition question.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-12556@lists.php.net to get a copy of this message
Michael writes: > I am using the following to display a table full of infomation: > > while(list($var1,$var2,$var3)=mysql_fetch_row($result)) { > printf("<tr> > <td>$var1</td> > <td>$var2</td> > <td>$var3</td> > </tr> > "); > } > > Based on search criteria, $var3 can be returned 1....X amount of times and > likely will have a different value each time. I want to total up these > values. Could someone provide me with assistance along those lines? > > thanks! This'll do it: <?php $var3_total = 0; while(list($var1,$var2,$var3)=mysql_fetch_row($result)) { printf("<tr> <td>$var1</td> <td>$var2</td> <td>$var3</td> </tr> "); $var3_total += $var3; } echo $var3_total; ?> BTW, for an example like this, you don't need the power of printf(); since the variables are just being substituted straight into the output, it may reduce overhead a bit to replace printf with echo or print. Of course, if the real code is different, please ignore this comment. :) Cheers, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#12556) next »