Solved

calculate all columns php/mysql

Posted on 2010-09-17
9
272 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 108

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
 
LVL 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now