HELP - Getting TOTAL from total in Rows

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
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

HELP - Getting TOTAL from total in Rows

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
User avatar
petershaw
Forum Newbie
Posts: 1
Joined: Sat Jul 23, 2005 9:31 am
Location: PhParadise

Post 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>";
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

petershaw - THANKS! PERFECT! :)
Post Reply