Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have create cart.php page. In this page show product image, quantity, product price, total Product price(*quantity) and grand total of product price. I show product on cart.php page using cookies. I want to calculate product price if quantity is increase or decrease. Also, i want show grand total of product price. Please help to solve this problem. I provide following my code for your reference.

What I have tried:

PHP
<table class="shop_table shop_table_responsive cart">
    <thead>
        <tr>
            <th class="product-thumbnail">Image</th>
            <th class="product-name">Product </th>
            <th class="product-price">Price</th>
            <th class="product-quantity">Quantity</th>
            <th class="product-subtotal">Total</th>
            <th class="product-remove"> </th>
        </tr>
    </thead>
    <tbody>
        <?php
            $pro_id = $_COOKIE['product_id'];   
            $pro_id = explode(',', $pro_id);
            $cnt = count(explode(",", $_COOKIE['product_id']));
            foreach ($pro_id as $val) {
                $ans = "SELECT * FROM wm_products WHERE pro_id='$val'";
                $result = $conn->query($ans);
                $totalprice=0;
                $totalqunty=0;
                while($row = $result->fetch_assoc()){

                    $cnt=$pro_id[$val]['quantity'];
                    $sub_total=$row['pro_price']*$cnt;  
                ?>
                    <tr class="cart_item">
                        <td class="product-thumbnail">
                            <a href="single-product.html"><img src="<?php echo $row['pro_img_path']; ?>" alt="" width="180" height="180"></a>
                        </td>
                        <td data-title="Product" class="product-name">
                            <a href="single-product.html"><?php echo $row['pro_name']; ?></a>
                        </td>
                        <td data-title="Price" class="product-price">
                            <span class="amount">Rs. <?php echo $row['pro_price']; ?></span>
                        </td>
                        <td data-title="Quantity" class="product-quantity">
                            <div class="quantity buttons_added">
                                <input class="minus" value="-" onclick='javascript: subtractQty();' type="button">
                                <label>Quantity:</label>
                                <input id='qty' size="4" class="input-text qty text" title="Qty" value="{$cnt}" name="quantity" max="29" min="0" step="1" type="number">
                                <input class="plus" value="+" onclick='javascript: document.getElementById("qty").value++;' type="button">
                            </div>
                        </td>
                        <td data-title="Total" class="product-subtotal">
                            <span class="amount">Rs. <?php echo $sub_total; ?></span>
                        </td>
                        <td class="product-remove">
                            <a class="remove remove_cart" href="#" dataid="<?php echo $row['pro_id']; ?>">×</a>
                        </td>
                    </tr>
                <?php 
                }                                   
            }
        ?>
        <tr>
            <td class="actions" colspan="6">

            <div class="coupon">
              <label for="coupon_code">Coupon:</label> <input placeholder="Coupon code" value="" id="coupon_code" class="input-text" name="coupon_code" type="text"> <input value="Apply Coupon" name="apply_coupon" class="button" type="submit">
            </div>

            <input value="Update Cart" name="update_cart" class="button" type="button">

            <div class="wc-proceed-to-checkout">
              <a class="checkout-button button alt wc-forward" href="checkout.php">Proceed to Checkout</a>
            </div>

            <input value="1eafc42c5e" name="_wpnonce" id="_wpnonce" type="hidden"><input value="/electro/cart/" name="_wp_http_referer" type="hidden">
            </td>
        </tr>
    </tbody>
</table>
Posted
Updated 12-Jun-18 19:53pm
Comments
Richard Deeming 12-Jun-18 10:10am    
You haven't described a problem in need of solving. Instead, you've given us a list of requirements.

Click the green "Improve question" link and update your question to explain what you have tried, and where you are stuck.

1 solution

PHP
$ans = "SELECT * FROM wm_products WHERE pro_id='$val'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900