Forum Moderators: coopster
The only way I can get it to work is to do an additional query for the SUM and as I need to total a few columns this is not an ideal situation.
Any help is gratefully appreciated.
echo $total;
that 'should' give you the total of the columns you specify as $column.
This is another. I have no idea if this will work but it might also be useful:
$query="SELECT *, sum(column_to_be_summed) as sum_column FROM Scoring_Track ORDER BY id";
Does that help?
$query="SELECT *, sum(column_to_be_summed) as sum_column FROM Scoring_Track ORDER BY id";
I thought there might be a way of stripping out the total of a column from the general query using php?
I could do it this way but I have 15 columns that need SUMing separately so that's going to be one v.long query.
And why is a long line of SQL a problem?
I thought there might be a way of stripping out the total of a column from the general query using php?
As was mentioned above you could just sum the columns you want in php and not even change the SQL.
<head>
<style type="text/css">
* {
margin:0;
padding:0;
}
#scoretable {
width: 400px;
padding: 0;
margin:0 0 20px 0;
}
#totaltable {
float:right;
width: 400px;
padding: 0;
margin:0 5px 20px 0;
}
.clear {clear:right;}
th {
font: bold 10px Verdana, Arial, Times, Georgia, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
text-align: left;
padding: 6px 6px 6px 12px;
background-color:#CAE8EA;
}
th.center {
text-align:center;
}
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}
td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
padding: 5px 5px 5px 5px;
color: #4f6b72;
}
td.alt {
background: #F5FAFA;
color: #797268;
}
th.spec {
margin:0;
padding:7px;
border-left: 1px solid #C1DAD7;
border-top: 0;
background-color:#f5fafa;
font: bold 8px Verdana, Arial, Times, Georgia, sans-serif;
text-align:center;
}
th.specalt {
margin:0;
padding:5px;
border-left: 1px solid #C1DAD7;
border-top: 0;
background-color:#fff;
font: bold 8px Verdana, Arial, Times, Georgia, sans-serif;
text-align:center;
color: #797268;
}
th.specbgtxt {
margin:0;
padding:5px;
border-left: 1px solid #C1DAD7;
border-top: 0;
background-color:#FFF;
font: bold 9px Verdana, Arial, Times, Georgia, sans-serif;
text-align:center;
color: #555;
}
</style>
</head>
<body>
<?include '../connect.php';
mysql_pconnect(localhost,$username,$password);
@mysql_select_db($database) or die(mysql_error());$query="SELECT * FROM Scoring_Track ORDER BY id";
$result=mysql_query($query)or die(mysql_error());
$dpquery="SELECT SUM(dylanplayerp) AS dpp FROM Scoring_Track";
$dpresult=mysql_query($dpquery) or die(mysql_error());
$dvquery="SELECT SUM(dylanvictoryp) AS dvp FROM Scoring_Track";
$dvresult=mysql_query($dvquery) or die(mysql_error());
$dtquery="SELECT SUM(dylantributep) AS dtp FROM Scoring_Track";
$dtresult=mysql_query($dtquery) or die(mysql_error());$jpquery="SELECT SUM(jamesplayerp) AS jpp FROM Scoring_Track";
$jpresult=mysql_query($jpquery) or die(mysql_error());
$jvquery="SELECT SUM(jamesvictoryp) AS jvp FROM Scoring_Track";
$jvresult=mysql_query($jvquery) or die(mysql_error());
$jtquery="SELECT SUM(jamestributep) AS jtp FROM Scoring_Track";
$jtresult=mysql_query($jtquery) or die(mysql_error());$mpquery="SELECT SUM(markplayerp) AS mpp FROM Scoring_Track";
$mpresult=mysql_query($mpquery) or die(mysql_error());
$mvquery="SELECT SUM(markvictoryp) AS mvp FROM Scoring_Track";
$mvresult=mysql_query($mvquery) or die(mysql_error());
$mtquery="SELECT SUM(marktributep) AS mtp FROM Scoring_Track";
$mtresult=mysql_query($mtquery) or die(mysql_error());$shpquery="SELECT SUM(shaunplayerp) AS shpp FROM Scoring_Track";
$shpresult=mysql_query($shpquery) or die(mysql_error());
$shvquery="SELECT SUM(shaunvictoryp) AS shvp FROM Scoring_Track";
$shvresult=mysql_query($shvquery) or die(mysql_error());
$shtquery="SELECT SUM(shauntributep) AS shtp FROM Scoring_Track";
$shtresult=mysql_query($shtquery) or die(mysql_error());$sipquery="SELECT SUM(simonplayerp) AS sipp FROM Scoring_Track";
$sipresult=mysql_query($sipquery) or die(mysql_error());
$sivquery="SELECT SUM(simonvictoryp) AS sivp FROM Scoring_Track";
$sivresult=mysql_query($sivquery) or die(mysql_error());
$sitquery="SELECT SUM(simontributep) AS sitp FROM Scoring_Track";
$sitresult=mysql_query($sitquery) or die(mysql_error());$dpptotal=mysql_result($dpresult,"dpp");
$dvptotal=mysql_result($dvresult,"dvp");
$dtptotal=mysql_result($dtresult,"dtp");
$jpptotal=mysql_result($jpresult,"jpp");
$jvptotal=mysql_result($jvresult,"jvp");
$jtptotal=mysql_result($jtresult,"jtp");
$mpptotal=mysql_result($mpresult,"mpp");
$mvptotal=mysql_result($mvresult,"mvp");
$mtptotal=mysql_result($mtresult,"mtp");
$shpptotal=mysql_result($shpresult,"shpp");
$shvptotal=mysql_result($shvresult,"shvp");
$shtptotal=mysql_result($shtresult,"shtp");
$sipptotal=mysql_result($sipresult,"sipp");
$sivptotal=mysql_result($sivresult,"sivp");
$sitptotal=mysql_result($sitresult,"sitp");$num=mysql_numrows($result);
mysql_close();
?><table id="scoretable" cellspacing="0" summary="The Scoring Track">
<tr>
<th colspan="2" abbr="Gaming Scores" class="nobg">Gaming Scores</th>
<th colspan="2" abbr="Dylan" class="center">Dylan</th>
<th colspan="2" abbr="James" class="center">James</th>
<th colspan="2" abbr="Mark" class="center">Mark</th>
<th colspan="2" abbr="Shaun" class="center">Shaun</th>
<th colspan="2" abbr="Simon" class="center">Simon</th>
</tr>
<tr>
<th class="spec">Date</th>
<th class="spec">Game</th>
<th class="spec">PPs</th>
<th class="spec">VPs<br />and TPs</th>
<th class="spec">PPs</th>
<th class="spec">VPs<br />and TPs</th>
<th class="spec">PPs</th>
<th class="spec">VPs<br />and TPs</th>
<th class="spec">PPs</th>
<th class="spec">VPs<br />and TPs</th>
<th class="spec">PPs</th>
<th class="spec">VPs<br />and TPs</th>
</tr><?
$i=0;
while ($i < $num)
while($row = mysql_fetch_array($result)) {
?><tr>
<th scope="row" class="specalt"><? echo $row['date']; ?></th>
<th scope="row" class="specalt"><? echo $row['game']; ?></th>
<th scope="row" class="specalt"><? echo $row['dylanplayerp']; ?></th>
<th scope="row" class="specalt"><? echo $row['dylanvictoryp']; ?></th>
<th scope="row" class="specalt"><? echo $row['jamesplayerp']; ?></th>
<th scope="row" class="specalt"><? echo $row['jamesvictoryp']; ?></th>
<th scope="row" class="specalt"><? echo $row['markplayerp']; ?></th>
<th scope="row" class="specalt"><? echo $row['markvictoryp']; ?></th>
<th scope="row" class="specalt"><? echo $row['shaunplayerp']; ?></th>
<th scope="row" class="specalt"><? echo $row['shaunvictoryp']; ?></th>
<th scope="row" class="specalt"><? echo $row['simonplayerp']; ?></th>
<th scope="row" class="specalt"><? echo $row['simonvictoryp']; ?></th>
</tr><?
$i++;
}
echo "</table>";
?><table id="totaltable" cellspacing="0" summary="The Total Points">
<tr>
<th abbr="The Showdown" class="nobg">The Showdown</th>
<th colspan="2" abbr="Dylan">Dylan</th>
<th colspan="2" abbr="James">James</th>
<th colspan="2" abbr="Mark">Mark</th>
<th colspan="2" abbr="Shaun">Shaun</th>
<th colspan="2" abbr="Simon">Simon</th>
</tr>
<tr>
<th class="specalt"> </th>
<th class="specalt"><? echo $dpptotal; ?></th>
<th class="specalt"><? echo $dvptotal+$dtptotal; ?></th>
<th class="specalt"><? echo $jpptotal; ?></th>
<th class="specalt"><? echo $jvptotal+$jtptotal; ?></th>
<th class="specalt"><? echo $mpptotal; ?></th>
<th class="specalt"><? echo $mvptotal+$mtptotal; ?></th>
<th class="specalt"><? echo $shpptotal; ?></th>
<th class="specalt"><? echo $shvptotal+$shtptotal; ?></th>
<th class="specalt"><? echo $sipptotal; ?></th>
<th class="specalt"><? echo $sivptotal+$sitptotal; ?></th>
</tr>
<tr>
<th class="specbgtxt">Total Points</th>
<th colspan="2" class="specbgtxt"><? echo $dpptotal+$dvptotal+$dtptotal; ?></th>
<th colspan="2" class="specbgtxt"><? echo $jpptotal+$jvptotal+$jtptotal; ?></th>
<th colspan="2" class="specbgtxt"><? echo $mpptotal+$mvptotal+$mtptotal; ?></th>
<th colspan="2" class="specbgtxt"><? echo $shpptotal+$shvptotal+$shtptotal; ?></th>
<th colspan="2" class="specbgtxt"><? echo $sipptotal+$sivptotal+$sitptotal; ?></th>
</tr>
</table>
<div class="clear"></div>
<?
mysql_free_result($result);
?></body>
$query="SELECT *,
SUM(dylanplayerp) AS dpp,
SUM(dylanvictoryp) AS dvp,
SUM(dylantributep) AS dtp,
SUM(jamesplayerp) AS jpp,
SUM(jamesvictoryp) AS jvp,
SUM(jamestributep) AS jtp,
SUM(markplayerp) AS mpp,
SUM(markvictoryp) AS mvp,
SUM(marktributep) AS mtp,
SUM(shaunplayerp) AS shpp,
SUM(shaunvictoryp) AS shvp,
SUM(shauntributep) AS shtp,
SUM(simonplayerp) AS sipp,
SUM(simonvictoryp) AS sivp,
SUM(simontributep) AS sitp
FROM Scoring_Track
GROUP BY id
ORDER BY id";$result=mysql_query($query)or die(mysql_error());
$dpptotal=mysql_result($result,"dpp");
$dvptotal=mysql_result($result,"dvp");
$dtptotal=mysql_result($result,"dtp");
$jpptotal=mysql_result($result,"jpp");
$jvptotal=mysql_result($result,"jvp");
$jtptotal=mysql_result($result,"jtp");
$mpptotal=mysql_result($result,"mpp");
$mvptotal=mysql_result($result,"mvp");
$mtptotal=mysql_result($result,"mtp");
$shpptotal=mysql_result($result,"shpp");
$shvptotal=mysql_result($result,"shvp");
$shtptotal=mysql_result($result,"shtp");
$sipptotal=mysql_result($result,"sipp");
$sivptotal=mysql_result($result,"sivp");
$sitptotal=mysql_result($result,"sitp");
$total = 0;
foreach($row['dylanplayerp'] as $c){
$dpptotal = $total + $c}
<tr>
<th class="specalt"><? echo $dpptotal; ?></th>
</tr>
Going to simplify things to make it easier for me to see what's going on. Let's say I have a table called Scores and it looks like this:
id¦ A ¦ B
1 ¦ 1 ¦ 2
2 ¦ 3 ¦ 2
3 ¦ 1 ¦ 1
4 ¦ 2 ¦ 3
Okay so the simplified code below displays a table showing those columns and the scores in them. The second table then needs to add the scores in column A and column B and display them individually so in this case totals would be 7 and 8 respectively. Then these totals are added together to show a full total which would be 15.
<?
$query="SELECT * FROM Scores ORDER BY id";
$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);
mysql_close();
?>
<table>First Table Here
<?
$i=0;
while ($i < $num)
while($row = mysql_fetch_array($result)) {
?>
<tr>
<th>echo $row['A'];</th>
<th>echo $row['B'];</th>
</tr>
<?
$i++;
}
echo "</table>";
?>
<?
$total = 0;
foreach($array['A'] as $c)
{
$colAtotal = $total + $c;
}
foreach($array['B'] as $c)
{
$colBtotal = $total + $c;
}
?>
<table>Second Table With Totals
<tr>
<th>echo $colAtotal;</th>
<th>echo $colBtotal;</th>
</tr>
<tr>
<th>echo $colAtotal+$colBtotal;</th>
</tr>
</table>
<?
mysql_free_result($result);
?>
So the bit in red is the non-working area. Using this example code can some kind soul please change this to show me how it should work then I can adapt it for my full code. Thanks.
I've tried that statement in the loop and out of it and either way leaves me with an invalid argument error because I'm assuming it considers $row['whatever'] as not an array.
This is the point I'm stuck with at the moment.
As I can't go back and edit the dodgy post I'll repeat it here corrected.
<?
$query="SELECT * FROM Scores ORDER BY id";
$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);
mysql_close();
?>
<table>First Table Here
<?
$i=0;
while ($i < $num)
while($row = mysql_fetch_array($result)) {
?>
<tr>
<th>echo $row['A'];</th>
<th>echo $row['B'];</th>
</tr>
<?
$i++;
}
echo "</table>";
?>
<?
$total = 0;
foreach($row['A'] as $c)
{
$colAtotal = $total + $c;
}
foreach($row['B'] as $d)
{
$colBtotal = $total + $d;
}
?>
<table>Second Table With Totals
<tr>
<th>echo $colAtotal;</th>
<th>echo $colBtotal;</th>
</tr>
<tr>
<th>echo $colAtotal+$colBtotal;</th>
</tr>
</table>
<?
mysql_free_result($result);
?>
<?
$query="SELECT * FROM Scores ORDER BY id";
$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);
$colAtotal = 0;
$colBtotal = 0;
?>
<table>First Table Here
<?
for($i=0; $i<$num; $i++) {
$row = mysql_fetch_array($result);
?>
<tr>
<th><?= echo $row['A']; ?></th>
<th><?= echo $row['B']; ?></th>
</tr>
<?
$colAtotal += $row['A'];
$colBtotal += $row['B'];
}
?>
</table>
<table>Second Table With Totals
<tr>
<th><?= echo $colAtotal;?></th>
<th><?= echo $colBtotal; ?></th>
</tr>
<tr>
<th><?= echo $colAtotal+$colBtotal; ?></th>
</tr>
</table>
<? mysql_close();
mysql_free_result($result);
?>