Calculate Sum (Total) of Column in PHP

Submitted by: 
Language: 
Visitors have accessed this post 81804 times.

Hi guys,

The purpose of this code is to teach you on how to calculate the values of a column using the Sum function.

This is very useful if you want to create a poin of sale system in your PHP project.

Hope you will like this source code, for more question about this code email me at [email protected] thank you.

databasename: db folder sum.sql

Sample code:

  1. <?php
  2. include('header.php');
  3. ?>
  4.  
  5. <body>
  6. <br>
  7. <br>
  8. <div class="container">
  9. <div class="alert alert-success">Sum the Numbers in a Column</div>
  10. <br />
  11. <table class="table table-striped table-bordered">
  12. <thead>
  13. <tr>
  14. <th>Product Name</th>
  15. <th>Quantity</th>
  16. <th>Price</th>
  17. <th>Amount</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <?php
  22. $query=mysql_query("select * from product")or die(mysql_error());
  23. while($row=mysql_fetch_array($query)){
  24. $id=$row['product_id'];
  25. ?>
  26. <tr>
  27. <td><?php echo $row['name'] ?></td>
  28. <td><?php echo $row['qty'] ?></td>
  29. <td><?php echo $row['Price'] ?></td>
  30. <td><?php echo $row['amount'] ?></td>
  31. </tr>
  32. <?php } ?>
  33. </tbody>
  34. </table>
  35. <?php
  36. $result = mysql_query("SELECT sum(amount) FROM product") or die(mysql_error());
  37. while ($rows = mysql_fetch_array($result)) {
  38. ?>
  39. <div class="pull-right">
  40. <div class="span">
  41. <div class="alert alert-success"><i class="icon-credit-card icon-large"></i>&nbsp;Total:&nbsp;<?php echo $rows['sum(amount)']; ?></div>
  42. </div>
  43. </div>
  44. <?php }
  45.  
  46. $result1 = mysql_query("SELECT sum(qty) FROM product") or die(mysql_error());
  47. while ($rows1 = mysql_fetch_array($result1)) {
  48. ?>
  49. <div class="pull-right">
  50. <div class="span">
  51. <div class="alert alert-info"><i class="icon-credit-card icon-large"></i>&nbsp;Total number of Items:&nbsp;<?php echo $rows1['sum(qty)']; ?></div>
  52. </div>
  53. </div>
  54. <?php } ?>
  55. </div>
  56. </body>
  57. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Thank you friend for this php code. It help me.

program for sql sum...

Hello Sir,
in above php code, what is in header.php file....?
its a just connection file ?
or anything
can u send me code of this file?

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.