RE: [PHP] Calculating Totals from table columns without a second query to the DB

From: Date: Fri, 06 Sep 2002 16:05:35 +0000
Subject: RE: [PHP] Calculating Totals from table columns without a second query to the DB
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-115505@lists.php.net to get a copy of this message
[snip] > Challenge; Can this be made as simple for rows too? <thinking out loud :-)> <?php $rowTotals = array (); while ($row = mysql_fetch_object($result)) { $rowTotal = 0; foreach ($row as $key=>$val) $rowTotal += $val; // display whatever is needed from $row $rowTotals[] = $rowTotal; // or echo it out to the last column of the table } ?> </tol> [/snip] :^] Or based on the original query; while($row = mysql_fetch_object($result)){ print("<td>" . $row->value . "</td>\n"); print("<td>" . $row->another_value . "</td>\n"); $value1 = $value1 + $row->value; $value2 = $value2 + $row->another_value; } you could add the following to the WHILE loop ... print("<td>" . ($row->value + $row->another_value) . "</td>\n"); ... giving you another column with the row total. That way you could avoid adding columns that do not contain numerical values if that is the case in your result set. And I think that you would want to use mysql_fetch_array for your example. Jay

« previous php.general (#115505) next »