Link to home
Start Free TrialLog in
Avatar of cataleptic_state
cataleptic_stateFlag for United Kingdom of Great Britain and Northern Ireland

asked on

calculate all columns php/mysql

Hi,
I have a table of numbers that are loaded from the database and I need to create a totals row to show the totals for each column.

This is the code for the table:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td bgcolor="#000000" class="tableTextHeading">&nbsp;</td>
    <td bgcolor="#000000" class="tableTextHeading">Team</td>
    <td width="40" bgcolor="#000000" class="tableTextHeading"><div align="center">Goals</div></td>
    <td width="30" bgcolor="#000000" class="tableTextHeading"><div align="center">YC</div></td>
    <td width="30" bgcolor="#000000" class="tableTextHeading"><div align="center">RC</div></td>
    <td width="30" bgcolor="#000000" class="tableTextHeading"><div align="center">MOM</div></td>
  </tr>
          <?php do { ?>
            <tr bgcolor="<?php echo ($ac_sw1++%2==0)?"#ffffff":"#dfdfdf"; ?>" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor=''">
              <td width="20">
              0<?php echo $i++;?>              </td>
              <td width="187"><a href="player-profile.php?id=<?php echo $row_squad_members['player_id']; ?>" class="playerStats"><?php //echo $row_squad_members['fname']; ?> <?php //echo $row_squad_members['sname']; ?> <?php echo $row_player_match['team_name']; ?></a></td>
              <td><div align="center" class="playerStats"><?php echo $row_player_match['myGoals']; ?></div></td>
              <td><div align="center" class="playerStats"><?php echo $row_player_match['myYC']; ?></div></td>
              <td><div align="center" class="playerStats"><?php echo $row_player_match['myRC']; ?></div></td>
              <td><div align="center" class="playerStats"><?php echo $row_player_match['myMOM']; ?></div></td>
            </tr>
            <?php } while ($row_player_match = mysql_fetch_assoc($player_match)); ?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason C. Levine
Add a new query that does something like this:

SELECT SUM(myGoals) as SumMyGoals, SUM(myYC) as SumMyYC etc etc FROM tablename WHERE conditions

and then just place SumMyGoals and SumMyYC in a new table row outside of the repeat.
*shakes tiny fist at Ray*
Then after line 28, put in another row to present the totals you have accumulated, something like this code snippet.

HTH, ~Ray
<tr>
<td><div align="center" class="playerStats"><?php echo $total_myGoals; ?></div></td>
<td><div align="center" class="playerStats"><?php echo $total_myYC; ?></div></td>
<td><div align="center" class="playerStats"><?php echo $total_myRC; ?></div></td>
<td><div align="center" class="playerStats"><?php echo $total_myMOM; ?></div></td>
</tr>

Open in new window

SVN-Blame: Distracted by barking dog.  

But seriously, since we already have all the data we don't need another query.  I think either way will work fine.

best to all, ~Ray
Agreed, either method works but Ray's is more elegant and he beat me by nanoseconds to the post (hence the tiny fist shaking)
Avatar of cataleptic_state

ASKER

shooot! something went wrong here. I was trying to give u points (multiple answer)
I dont know why it would not let me give you both points first time around
There's a small bug in a new closing process and that's why you had "trouble"

It would have closed on its own in four days with points to Ray as intended.