• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Outputting Values.

Hi experts, Ive got a table that displays values of currency as in £45.00 now i want these to be added up and totalled at the bottom currnetly i get no output what so ever before i used to get a total after each company but what i actually want is just one total at the end

Could someone help me please??

many thanks

Code is attached and image.

It may be the case of me closeing my while loop to early or in the wrong place ive tried loads of varioations but cant crack it can you please help me.
<?php 
error_reporting(E_ALL);
include 'myphp.php';

  $query = "SELECT customer_name, SUM(Jan09), SUM(Feb09),SUM(Mar09),SUM(Apr09),SUM(May09),SUM(Jun09),SUM(Jul09),SUM(Aug09),SUM(Sep09),SUM(Oct09),SUM(Nov09),SUM(Dec09) FROM bill_detail2009 GROUP BY customer_name"; 
	 
$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Company</th> <th>Jan09</th> <th>Feb09</th> <th>Mar09</th> <th>Apr09</th> <th>May09</th> <th>Jun09</th> <th>Jul09</th> <th>Aug09</th> <th>Sep09</th> <th>Oct09</th> <th>Nov09</th> <th>Dec09</th> </tr>";
// Print out result
while($row = mysql_fetch_array($result)){
	//echo "<tr><td>";
	
	//echo "<br />";
	
	// Print out the contents of each row into a table
echo "<tr><td>"; 
	echo "<a href=\"ARPU2.php?val=".$row['customer_name']."\">".$row['customer_name']."</a>";
	echo "</td><td>"; 
	echo "£".$row['SUM(Jan09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Feb09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Mar09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Apr09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(May09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Jun09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Jul09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Aug09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Sep09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Oct09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Nov09)'];
	echo "</td><td>"; 
	echo "£".$row['SUM(Dec09)'];
	echo "</td><td>";
	
	}
	 while($row = mysql_fetch_array($result)){
echo "<tr><th>Total</th> <th>Jan09</th> <th>Feb09</th> <th>Mar09</th> <th>Apr09</th> <th>May09</th> <th>Jun09</th> <th>Jul09</th> <th>Aug09</th> <th>Sep09</th> <th>Oct09</th> <th>Nov09</th> <th>Dec09</th> </tr>";
echo "<tr><td width=125>";
echo 'Total';
	echo "</td><td>"; 
	echo "£".$row['SUM(Jan09)'];
	$JanTotal = 0;
$JanTotal += $row['SUM(Jan09)'];
      echo "</td><td>"; 
	  echo "£".$row['SUM(Feb09)'];
	$FebTotal = 0;
$FebTotal += $row['SUM(Feb09)'];
	  echo "</td><td>";
	  
	 }
	  
	  echo "</table>";
	 
	  echo "<table>";

$first_number=$row['SUM(Jan09)'];
$second_number=$row['SUM(Feb09)'];
$third_number=$row['SUM(Mar09)'];
$sum_total=$first_number+$second_number+$third_number;
	 
	 

echo "</table>";


   ?>

Open in new window

pivture2.jpg
0
NeoAshura
Asked:
NeoAshura
  • 2
2 Solutions
 
NeoAshuraAuthor Commented:
just to clarify i want to add the total of each month up at the bottom of the list. like

Company  Jan Feb Mar
Mark        £10 £11 £10
Anna       £10 £11  £10
Total       £20  £22  £20
0
 
Slick812Commented:
greetings,  NeoAshura, , I do not see in your code where you selectively store the company's monthly value ($row['SUM(Jan09)) to a $JanTotal value which will add the several row monthly values together

Here is some code that may give you an I dea of how to do it, I do not have your data base, so this is an IDEA, it is Untested code. -

$totals = array(0,0,0,0,0,0,0,0,0,0,0,0);
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
      echo "<a href=\"ARPU2.php?val=".$row['customer_name']."\">".$row['customer_name']."</a>";
      echo "</td><td>";
        $totals[0] +=  $row['SUM(Jan09)'];
      echo "£".$row['SUM(Jan09)'];
        echo "</td><td>";
         $totals[1] +=  $row['SUM(feb09)'];
      echo "£".$row['SUM(Feb09)'];
        echo "</td><td>";
         $totals[2] +=  $row['SUM(mar09)'];
      echo "£".$row['SUM(mar09)'];

- - - - - -
it looks like to have mixed up the proper table structure this the totals - while($row = mysql_fetch_array($result)){ )
loop
you need to take out your code after the first - while($row = mysql_fetch_array($result)){ -


and add something like this -

} // end of the  first while($row   loop
echo "</td></tr><tr><td>Total";
for ($i = 0; $i<12;++$i) {
echo "</td><td>";
echo "£".$totals[$i];
}
echo "</td></tr></table>";


this is untested code, but I hope you get the idea about adding the values into a separate Total value ( I used an array, but other ways are possible).

ask questions, if I did not explain clearly
0
 
Lukasz ChmielewskiCommented:
How about query like
$query = "SELECT customer_name, SUM(Jan09) as jan09, SUM(Feb09) as feb09,SUM(Mar09)as mar09,SUM(Apr09) as apr09 ...

and then displaying

echo "£".$row[jan09];
      echo "</td><td>";
      echo "£".$row['feb09'];
      echo "</td><td>";

.etc.
0
 
NeoAshuraAuthor Commented:
Hi guys thanks for the reply ill test this tomorrow when im in the office appricate the help
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now