PHP - Calculate Total Price In Table
Submitted by razormist on Wednesday, November 20, 2019 - 19:02.
In this tutorial we will create a Calculate Total Price In Table using PHP. This code will automatically calculate the total price of the product when user click the calculate button. The code use MySQLi SUM() query to automatically calculate the price data in the database before displaying it to the HTML page. This a user-friendly program feel free to modify and use it to your system.
We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.
There you have it we successfully created Calculate Total Price In Table using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating Database
Open your database web server then create a database name in it db_sum, after that click Import then locate the database file inside the folder of the application then click ok.Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.- <?php
- if(!$conn){
- }
- ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="containr-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Calculate Total Price In Table</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form method="POST" action="save_product.php">
- <div class="form-group">
- <label>Product</label>
- <input type="text" name="product" class="form-control" required="required"/>
- </div>
- <div class="form-group">
- <label>Price</label>
- <input type="number" min="0" name="price" class="form-control" required="required"/>
- </div>
- <center><button class="btn btn-primary" name="add">Add Product</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Product</th>
- <th>Price</th>
- </tr>
- </thead>
- <tbody>
- <?php
- require_once 'conn.php';
- ?>
- <tr>
- <td><?php echo $fetch['product_name']?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- <?php include 'calculate.php'?>
- </table>
- </div>
- </div>
- </body>
- </html>
Creating PHP Query
This code contains the php query of the application. This code will store the product information to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_product.php.- <?php
- require_once 'conn.php';
- $product = $_POST['product'];
- $price = $_POST['price'];
- mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product', '$price')") or die(mysqli_error());
- }
- ?>
Creating the Main Function
This code contains the main function of the application. This code will automatically calculate total price of product when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as calculate.php.- <tfoot>
- <tr>
- <td align="right">Total</td>
- <td align="right">
- <?php
- ?>
- <form method="POST" action="">
- <button class="btn btn-success" name="sum">Calculate</button>
- </form>
- <?php
- }else{
- ?>
- <?php
- }
- ?>
- </td>
- </tr>
- </tfoot>
Add new comment
- 10550 views