简体   繁体   中英

How to calculate total in cart for each item selected from php?

I'm a beginner seeking to solve this problem!

how to calculate the total of each item in the cart and how to calculate grand total?

this is my code

<td class="p-price first-row"><?php echo 'LBP '.number_format($r['product_price'],0)?>/KG</td>
    <input type="hidden"  class="unitprice" id="unitprice" value="<?php echo $r['product_price']?>">
<input type="text"  id="quantity" class="quantity" value="<?php echo $weight ?>" readonly>
 <td class="total-price first-row"><input class="total" id="total" readonly></td>

<input type="int" value="" id="grandtotal" class="grandtotal" readonly>

The variables you want arent quite clear. That said, it seems you're looking for something like:

$grandTotal = 0;
foreach ($_SESSION as $key => $values) {
    if (strpos($key, "pid") !== false) {
        $sql = "select * from products LEFT JOIN category on products.category_id = category.category_id where product_id = $_SESSION[$key]";
        $result = $connect->query($sql);
        $r = mysqli_fetch_assoc($result);
        $w = "weight";
        $w .= (string)$r['product_id'];
        $weight = $_SESSION[$w];
        $total = $r['product_price'] * $weight;
        $grandTotal += $total;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM