Total Sum Of Products In PHP/MySQL

Total Sum Of Products System is a simple project created in PHP, MySQL Javascript, and it has a extension of Bootstrap and Modal for the better and responsive design for all the users. This simple system is very useful for the projects and also for the other company that manages products. This system creates a total sum or cost of all products where automatically gives you the amount under the product table.

Sample Code

Home.php - this is where the two forms combined and synchronized to a one form of code.
  1. <?php
  2. include('header.php');
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title>Total Sum Of Products</title>
  8. </head>
  9. <body>
  10. <nav class="navbar navbar-default">
  11. <div class="container-fluid">
  12. <div class="navbar-header">
  13. <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  14. </div>
  15. <ul class="nav navbar-nav">
  16. <li class="active"><a href="home.php">Home</a></li>
  17. <li><a href="#">About</a></li>
  18. <li><a href="#">Contact Us</a></li>
  19. </ul>
  20. </div>
  21. </nav>
  22. <h1 align="center">Products</h1>
  23. <div class="container">
  24. <div class="row-fluid">
  25. <div class="span12">
  26. <ul id="myTab" class="nav nav-tabs">
  27. <li class="active"><a href="#member" data-toggle="tab"><i class="icon-group icon-large"></i>&nbsp;Total Product List</a></li>
  28. <li><a href="#user" data-toggle="tab"><i class="icon-user icon-large"></i>&nbsp;Add Product</a></li>
  29. <li>
  30. <a data-toggle="modal" data-target="#myModal"><i class="icon-signout icon-large"></i>&nbsp;Logout</a>
  31. </li>
  32. </ul>
  33. </div>
  34. </div>
  35. <div class="tab-content">
  36. <?php
  37. include('tab_member.php');
  38. ?>
  39. <?php
  40. include('tab_user.php');
  41. ?>
  42. </div>
  43. <?php
  44. include('modal.php');
  45. ?>
  46. </div>
  47. </body>
  48. </html>
Table_member - And this script is for the table of products where it displays the details.
  1. <div class="tab-pane fade in active" id="member">
  2. <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
  3. <thead>
  4. <tr>
  5. <th>Product Name</th>
  6. <th>Quantity</th>
  7. <th>Price</th>
  8. <th>Amount</th>
  9. <th>Delete</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <?php
  14. $query=mysql_query("select * from product")or die(mysql_error());
  15. while($row=mysql_fetch_array($query)){
  16. $id=$row['product_id'];
  17. ?>
  18. <tr>
  19. <td><?php echo $row['name'] ?></td>
  20. <td><?php echo $row['qty'] ?></td>
  21. <td><?php echo $row['price'] ?></td>
  22. <td><?php echo $row['amount']; ?></td>
  23. <td><a id="<?php echo $id;?>" class="delete_product">&nbsp;Delete</a></td>
  24. </tr>
  25. <?php } ?>
  26. </tbody>
  27. </table>
  28. <?php
  29. $result = mysql_query("SELECT sum(amount) FROM product") or die(mysql_error());
  30. while ($rows = mysql_fetch_array($result)) {
  31. ?>
  32. <div class="pull-right">
  33. <div class="span"><div class="alert alert-success"><i class="icon-credit-card icon-large"></i>&nbsp;Total:&nbsp;<?php echo $rows['sum(amount)']; ?></div></div>
  34. </div>
  35. <?php }
  36. ?>
  37. <?php
  38. $result1 = mysql_query("SELECT sum(qty) FROM product") or die(mysql_error());
  39. while ($rows1 = mysql_fetch_array($result1)) {
  40. ?>
  41. <div class="pull-right">
  42. <div class="span"><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></div>
  43. </div>
  44. <?php }
  45. ?>
  46. <script type="text/javascript">
  47. $(document).ready( function() {
  48. $('.delete_product').click( function() {
  49. var id = $(this).attr("id");
  50.  
  51. if(confirm("Are you sure you want to delete this Product?")){
  52. $.ajax({
  53. type: "POST",
  54. url: "delete.php",
  55. data: ({id: id}),
  56. cache: false,
  57. success: function(html){
  58. $(".del"+id).fadeOut('slow');
  59. }
  60. });
  61. }else{
  62. return false;}
  63. });
  64. });
  65. </script>
resultTable_user.php - This script also is for creating a new product.
  1. <div class="tab-pane fade"id="user">
  2. <div class="containers">
  3. <p align="center" style="color:#000">Fill Up All The Details Below!</p>
  4. <form class="form-horizontal" method="POST">
  5. <div class="form-group">
  6. <label class="control-label col-sm-2" for="name">Product Name:</label>
  7. <div class="span4">
  8. <input type="text" name="name" class="form-control" id="name" placeholder="Product Name" required>
  9. </div>
  10. </div>
  11. <div class="form-group">
  12. <label class="control-label col-sm-2" for="qty">Quantity:</label>
  13. <div class="span4">
  14. <input type="text" name="qty" class="form-control" id="qty" placeholder="Quantity" required>
  15. </div>
  16. </div>
  17. <div class="form-group">
  18. <label class="control-label col-sm-2" for="price">Price:</label>
  19. <div class="span4">
  20. <input type="text" name="price" class="form-control" id="price" placeholder="Price" required>
  21. </div>
  22. </div>
  23. <div class="form-group">
  24. <div class="col-sm-offset-2 col-sm-10">
  25. <button type="submit" name="submit" class="btn btn-primary">Submit</button>
  26. </div>
  27. </div>
  28. </form>
  29. <?php
  30. if (isset($_POST['submit'])){
  31. $name=$_POST['name'];
  32. $qty=$_POST['qty'];
  33. $price=$_POST['price'];
  34. $amount=$qty * $price;
  35. mysql_query ("INSERT INTO product (name, qty, price, amount)
  36. VALUES ('$name','$qty','$price', '$amount')")or die(mysql_error());
  37. ?>
  38. <script type="text/javascript">
  39. alert('Successfully Added a New Product');
  40. window.location="../admin/home.php";
  41. </script>
  42. <?php
  43. }
  44. ?>
  45. </div>
  46. </div>
Hope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Add new comment