Calculate Total Price In Table Using PHP | Free Source Code

Calculate Total Price In Table using PHP

June 5, 2020
PHP

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 the user clicks 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 interprets in the webserver such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.

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 a Database

Open your database web server then create a database name in it db_sum, after that click Import then locates the database file inside the folder of the application then click ok.

calculate total price in table using php

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
    $conn=mysqli_connect("localhost", "root", "", "db_price");
 
    if(!$conn){
        die("Error: Faile to connect to database!");
    }
?>

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';
                        $query = mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
                        while($fetch = mysqli_fetch_array($query)){
                    ?>
                        <tr>
                            <td><?php echo $fetch['product_name']?></td>
                            <td align="right"><?php echo number_format($fetch['price'])?></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';
 
    if(ISSET($_POST['add'])){
        $product = $_POST['product'];
        $price = $_POST['price'];
 
        mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product', '$price')") or die(mysqli_error());
 
        header("location: index.php");
    }
?>

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
                if(!ISSET($_POST['sum'])){
            ?>
                <form method="POST" action="">
                    <button class="btn btn-success" name="sum">Calculate</button>
                </form>
            <?php
                }else{
                    $query = mysqli_query($conn, "SELECT SUM(price) AS total FROM `product`") or die(mysqli_error());
                    $fetch = mysqli_fetch_array($query);
            ?>
                <label class="text-danger"><?php echo number_format($fetch['total'])?></label>
            <?php
                }
            ?>
        </td>
    </tr>
</tfoot>

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!

Facebook Comments

Leave a Reply

Your email address will not be published.