Solved

Outputting Values.

Posted on 2011-03-22
4
205 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
  • 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 33

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses four methods for overlaying images in a container on a web page
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 …

776 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