Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get the total sum of rows by grouping values and output on html form in a table

What I have tried:

PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "satapp";

$conn = new mysqli($servername, $username, $password, $dbName);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT indiname,  baseline from indicators GROUP BY indiname";
$result = $conn->query($query);
?>
<!DOCTYPE html>
	<title>get Records
<div style="width: 500px">
	<!--<a href="one.php"<h3 align="center">get Records</h3>-->
<?php
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
?>
<?php
}

}	
$conn->close();?>
<table><tbody><tr><th>Intervention Area</th><th>Target</th><th>Progress</th></tr><tr><td><?php echo $row["Interventionarea"];?></td><td><?php echo $row["target"];?></td><td><?php $number=array("progress"); echo array_sum($number);?></td></tr></tbody></table>

</div>
Posted
Updated 24-Jul-17 19:43pm
v2
Comments
Mohibur Rashid 25-Jul-17 1:23am    
your $number contains only one string, how do you expect to have sum? and why did you do that?

1 solution

1. Your SQL query will not even run, GROUP BY[^] must have some aggregate functions in the SELECT list...
2. Do not run sum in code - do it in SQL!
 
Share this answer
 
Comments
CPallini 25-Jul-17 3:03am    
5.
Kornfeld Eliyahu Peter 25-Jul-17 3:37am    
Thank you...
ezrasoft 26-Jul-17 5:52am    
thank you for your response to my question.
how would you suggest i do it. i have a table in a database i want to sum up a specific field when data is printed on html .
Kornfeld Eliyahu Peter 26-Jul-17 6:10am    
select a, b, c from table where...
union
select null, null, sum(c) from table where...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900