Solved

calculate all columns php/mysql

Posted on 2010-09-17
9
274 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:cataleptic_state
  • 4
  • 3
  • 2
9 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33702771
Use this kind of pattern.  Where you currently have this...

<?php echo $row_player_match['myGoals']; ?>

... replace it with the code snippet.
<?php 
echo $row_player_match['myGoals']; 
$total_myGoals = $total_myGoals + $row_player_match['myGoals']; 
?>

Open in new window

0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 33702772
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.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 33702779
*shakes tiny fist at Ray*
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33702797
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

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33702816
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
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 33702855
Agreed, either method works but Ray's is more elegant and he beat me by nanoseconds to the post (hence the tiny fist shaking)
0
 
LVL 4

Author Comment

by:cataleptic_state
ID: 33716062
shooot! something went wrong here. I was trying to give u points (multiple answer)
0
 
LVL 4

Author Closing Comment

by:cataleptic_state
ID: 33716069
I dont know why it would not let me give you both points first time around
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 33719856
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.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question