Solved

calculate all columns php/mysql

Posted on 2010-09-17
9
275 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

837 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