Solved

Outputting Values.

Posted on 2011-03-22
4
207 Views
Last Modified: 2012-05-11
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
Comment
Question by:NeoAshura
[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
  • 2
4 Comments
 
LVL 6

Author Comment

by:NeoAshura
ID: 35191459
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
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 250 total points
ID: 35192136
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
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 250 total points
ID: 35192552
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
 
LVL 6

Author Comment

by:NeoAshura
ID: 35201099
Hi guys thanks for the reply ill test this tomorrow when im in the office appricate the help
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

695 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