PHP maths help

Permabanned
Joined
22 Apr 2007
Posts
1,805
hi all,

I've got the following code which pulls all the data from the MySQL table and puts it into an array then returns each result.

I'm trying at the bottom to get one large total for all the results added together. Currecntly its displaying the sumation for each result found.

Hope that makes sense. I guess what I'm trying to say is how do I remove it from the while loop?

Code:
<?php

include("connect.php"); 

$sitename=$_GET['q'];

//This is the query for the administration; 
//we are obtaining all articles that are available with all columns.
$query="select * from siteform where sitename ='" .$sitename. "'";


//This runs the query to view
//If we get a positive result in $results, it will process the while loop
If ($results = mysql_query ($query)) {

//Creates the diplay table
}
?>

<?php
if (mysql_num_rows($results)>0){


While ($row = mysql_fetch_array($results)) { 
//inputs the data into the table
		$id = stripslashes($row['id']);
		$price = stripslashes($row['price']);
		$consumption = stripslashes($row['consumption']);
		$hours = stripslashes($row['hours']);
		
		
?>
	
<br />

<font color=#FF0000>kWh * ADU = </font><?php $sum_total = $consumption * $hours;
$kwh = $sum_total * $price;
echo $kwh; ?><br />

</p>


	
	<?php
	}
}
else{
	echo "The site survey for <font color=#FF0000>$sitename</font> has not yet been carried out";
}
	?>
	
   
<?php
mysql_close();
?>

:)
 
PHP:
$total = 0;
while ($row = mysql_fetch_array($results)) { 
  $total = $total + $row['number'];
}
echo $total;

that sort of thing?
 
Yeah, except $price, $consumption and $hours are all listed in a MySQL table.

As you can probably tell, I'm not sure if that makes any difference to what you've kindly posted :)
 
PHP:
$result = mysql_query("SELECT SUM(price) AS total_price, SUM(consumption) AS total_consumption, SUM(hours) as total_hours FROM siteform WHERE sitename = '$sitename'");
while($row = mysql_fetch_assoc($result));
    $total_price = $row['total_price'];
    //etc......
}
 
Thanks Marc,

Is that summing the price though? Price is a constant at 0.15. Say I have 6 results, does the above not sum these to make 0.9?

I have this now, and the result is showing as '0' but I guess i've done something odd.

Code:
<?php

include("connect.php"); 

$sitename=$_GET['q'];

//This is the query for the administration; 
//we are obtaining all articles that are available with all columns.
$result = mysql_query("SELECT SUM(price) AS total_price, SUM(consumption) AS total_consumption, SUM(hours) as total_hours FROM siteform WHERE sitename = 

'$sitename'");

//Creates the diplay table

?>

<?php



while($row = mysql_fetch_assoc($result)); 
	$total_price = $row['total_price']; 
	$total_consumption = $row['total_consumption'];
	$total_hours = $row['total_hours'];

	
$sum_total = $total_consumption * $total_hours;
$kwh = $sum_total * $total_price;
 



	?>
	<br />

	<font color=#FF0000>Total Price To Run These Applications For One Day is = </font><?php $tprice=$kwh; echo $tprice;

	?>
	
   
<?php
mysql_close();
?>
 
you need to format the decimal as well like so...

PHP:
DECIMAL(x, y)

x= total number of digits
y= digits after decimal point

so for example you could use

PHP:
DECIMAL(6,2)

that would allow anything upto 9999.99
 
time for some basic php troubleshooting. echo each value on screen and see where it's going wrong. also you might want to test the query in phpmyadmin to make sure the totals tally.
 
Back
Top Bottom