Solved

Outputting Values.

Posted on 2011-03-22
4
203 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi guys thanks for the reply ill test this tomorrow when im in the office appricate the help
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 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

13 Experts available now in Live!

Get 1:1 Help Now