How to calculate total price in php

CJAY

I am getting the price of the product from db using the product id i have stored in php array.

i have a php function where in i want to hold a total of all the products purchased. here i am getting the cart items and i am calling the function which should hold total amount. i am passing the value price each time when i retrieve the product from cart.

for example for first item iteration i send value 300, and for second 400, for third 900.

i want to add up all these and get total price of 1600 in payableAmount() how can i do this?

function getCartitems($product_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_id,product_name,product_price,product_image_url FROM product_list WHERE product_id='$product_id'";
    $result = $conn->query($sql);      
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {              
        echo "<tr><td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td><td>".$row["product_name"]."</td><td><b>&#8377; </b>".floor($row["product_price"])."</td><td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'><span class='glyphicon glyphicon-remove'></span> remove</a></td></tr>";
        $price = $row['product_price'];
        payableAmount($price);      
        }
    } else {
        echo "There are no such items";
    }   
    $conn->close();
}

function payableAmount($totalPrice){    
   // calculate total price here
    $total = $totalPrice;
    echo $total;
}
Narendrasingh Sisodia

You just simply update your query instead like as

$total_price = 0;
while($row = $result->fetch_assoc()) {              
  echo "<tr><td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td><td>".$row["product_name"]."</td><td><b>&#8377; </b>".floor($row["product_price"])."</td><td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'><span class='glyphicon glyphicon-remove'></span> remove</a></td></tr>";
  $total_price += $row['product_price'];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate total price of order?

From Dev

How to calculate total price of an order with Rails?

From Dev

How to calculate total price of an order with Rails?

From Dev

How can I calculate total price of an invoice?

From Dev

Calculate total price with 'WITH RECURSIVE'

From Dev

Calculate total price with 'WITH RECURSIVE'

From Dev

How to get the number of Items in multiple Spinner View and calculate the total price

From Dev

Model rails calculate total price

From Dev

Calculate total price of item list

From Dev

JavaScript Calculate total price of items

From Dev

Javascript calculate tax and total price

From Dev

How to calculate with php the total with a VAT percentage in PHP

From Dev

How can i calculate total positive ,total negative price and sum using Node.js and Mongoose

From Dev

how made total price

From Dev

How to make total price system of suppliers in PHP & MySQL

From Dev

How do I calculate the sub total and total in a table in the PHP?

From Dev

Using Forms in HTML to calculate total price

From Dev

Laravel Eloquent: Best Way to Calculate Total Price

From Dev

Calculate total with price and quantity using Angularjs

From Dev

Calculate Total Price from multiple toggle Buttons

From Dev

Can't calculate total price > 30

From Dev

Calculate order total price in node js and mongodb

From Dev

calculate total price from checkbox + select list

From Dev

MYSQL and PHP calculate total price of orders table every day and every month, populate new summary table

From Dev

PHP: calculate total price according to which options a user chooses from HTML form

From Dev

Count total price of items with php

From Dev

php mysql get total price

From Dev

How can I calculate total price with piece in GridView in ASP.NET

From Dev

How to calculate total sales in MongoDB but the product price data comes from different collection

Related Related

  1. 1

    How to calculate total price of order?

  2. 2

    How to calculate total price of an order with Rails?

  3. 3

    How to calculate total price of an order with Rails?

  4. 4

    How can I calculate total price of an invoice?

  5. 5

    Calculate total price with 'WITH RECURSIVE'

  6. 6

    Calculate total price with 'WITH RECURSIVE'

  7. 7

    How to get the number of Items in multiple Spinner View and calculate the total price

  8. 8

    Model rails calculate total price

  9. 9

    Calculate total price of item list

  10. 10

    JavaScript Calculate total price of items

  11. 11

    Javascript calculate tax and total price

  12. 12

    How to calculate with php the total with a VAT percentage in PHP

  13. 13

    How can i calculate total positive ,total negative price and sum using Node.js and Mongoose

  14. 14

    how made total price

  15. 15

    How to make total price system of suppliers in PHP & MySQL

  16. 16

    How do I calculate the sub total and total in a table in the PHP?

  17. 17

    Using Forms in HTML to calculate total price

  18. 18

    Laravel Eloquent: Best Way to Calculate Total Price

  19. 19

    Calculate total with price and quantity using Angularjs

  20. 20

    Calculate Total Price from multiple toggle Buttons

  21. 21

    Can't calculate total price > 30

  22. 22

    Calculate order total price in node js and mongodb

  23. 23

    calculate total price from checkbox + select list

  24. 24

    MYSQL and PHP calculate total price of orders table every day and every month, populate new summary table

  25. 25

    PHP: calculate total price according to which options a user chooses from HTML form

  26. 26

    Count total price of items with php

  27. 27

    php mysql get total price

  28. 28

    How can I calculate total price with piece in GridView in ASP.NET

  29. 29

    How to calculate total sales in MongoDB but the product price data comes from different collection

HotTag

Archive