Solved

calculate all columns php/mysql

Posted on 2010-09-17
9
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 110

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 110

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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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