Page 1 of 1

HELP - Getting TOTAL from total in Rows

Posted: Sat Jul 23, 2005 11:57 am
by camarosource
I am fairly new to PHP and am having a bit of troubles. Sample Code would be very much appreciated.

I have a PHP script http://camarosource.ca/2006_calendars/list.php loads http://camarosource.ca/2006_calendars/list.htm
This displays new cell rows via PHP's "ROW" command. It displays the results from a database. In the script it then displays the total of each row on the screen and into a variable ($CALENDAR_TOTAL).

At the end of the list it creates, I have a final row where I would like various totals to display. In this case, I am TRYING to figure out how to get the FINAL total of all the subtotals that display on the screen but am having GREAT difficulties. Basically by looking at the totals you would see I am wanting to add 6 + 4 + 5 + 2 + 5 + 1 + 5 ........ and so forth.. you'll understand when you see the script http://camarosource.ca/2006_calendars/list.php

I want to display the COMPLETE total in the (FULL TOTAL) cell.

HELP.

Here is the code http://camarosource.ca/2006_calendars/list.phps

Posted: Sat Jul 23, 2005 12:17 pm
by timvw
If you are using mysql you could:

Code: Select all

select sum(total) AS total
from table
group by (primary key columns)
with rollup

Posted: Sat Jul 23, 2005 12:27 pm
by petershaw
hi!

first of all, you should use a database-class or a function
that prevents you from sql-injection and raiding your host!

maybe like this...

Code: Select all

/* init counter */
$totalcounter = 0;
$newtotalcounter = 0;

/* then like before */
while ($row = mysql_fetch_array($sql_result)) {
$FIRST_GEN_QTY = $row["FIRST_GEN_QTY"];
$SECOND_GEN_QTY = $row["SECOND_GEN_QTY"];
$THIRD_GEN_QTY_VER_ONE = $row["THIRD_GEN_QTY_VER_ONE"];
$THIRD_GEN_QTY_VER_TWO = $row["THIRD_GEN_QTY_VER_TWO"];
$FOURTH_GEN_QTY_VER_ONE = $row["FOURTH_GEN_QTY_VER_ONE"];
$FOURTH_GEN_QTY_VER_TWO = $row["FOURTH_GEN_QTY_VER_TWO"];

$CALENDAR_QUANTITY = $FIRST_GEN_QTY + $SECOND_GEN_QTY + $THIRD_GEN_QTY_VER_ONE + $THIRD_GEN_QTY_VER_TWO + $FOURTH_GEN_QTY_VER_ONE + $FOURTH_GEN_QTY_VER_TWO;

/* add to total-counter */
$newtotalcounter = ($totalcounter + $CALENDAR_QUANTITY);
$totalcounter = $newtotalcounter;

echo "<tr>
          <td align=center height=96 rowspan=2 width=97>
          <b>$CALENDAR_QUANTITY</b></td>
          </tr>
        <tr>
          <td align=center height=11 width=72>
          $FIRST_GEN_QTY</td>
          <td align=center height=11 width=72>
          $SECOND_GEN_QTY</td>
          <td align=center height=11 width=72>
          $THIRD_GEN_QTY_VER_ONE</td>
          <td align=center height=11 width=72>
          $THIRD_GEN_QTY_VER_TWO</td>
          <td align=center height=11 width=72>
          $FOURTH_GEN_QTY_VER_ONE</td>
          <td align=center height=11 width=72>
          $FOURTH_GEN_QTY_VER_TWO</td>
          </tr>";
}
echo "<tr>
          <td align=center height=11 width=97>
          <b>FULL TOTAL:";
echo $totalcounter;
echo "</b></td>
          <td align=center height=12 colspan=6>
          &nbsp;</td>
        </tr>
</TABLE>";

Posted: Sat Jul 23, 2005 12:34 pm
by camarosource
petershaw - THANKS! PERFECT! :)