Jump to content

Add total price of items in a shopping Cart


katta44

Recommended Posts

Hi, 

 

I have the code to add items to a shopping cart. When one item added to the shopping cart and change the quality then the total shown is accurate by price * quantity.

 

I can add multiple items into the cart but the total price is not accurate if quantity is changed. The total price is accurate only for one item.

 

Please let me know how to loop all the items in the cart and show the total for all the items as price * quantity

 

Below is the code, please help i am new to PHP.

<?php
$total=0;
global $con;
$ip = getIp();
$sel_price = "select * from cart where ip_add='$ip'";
$run_price = mysqli_query($con,$sel_price);
while($p_price=mysqli_fetch_array($run_price)){
$pro_id= $p_price['p_id'];
$pro_price = "select * from products where product_id='$pro_id'";
$run_pro_price=mysqli_query($con,$pro_price);
while($pp_price = mysqli_fetch_array($run_pro_price)){
$product_price= array($pp_price['product_price']);
$product_title = $pp_price['product_title'];
$product_image = $pp_price['product_image'];
$single_price = $pp_price['product_price'];
$values=array_sum($product_price);
$total += $values;
 
 
 
          
   ?>
      
      <tr align="center">
      <td><input type="checkbox" name="remove[]" value="<?php echo $pro_id;?>"/></td>
      <td><?php echo $product_title; ?><br>
      <img src="admin_area/product_images/<?php echo $product_image; ?>" width="60" height="60"/>
      </td>
      <td><input type="text" size="4" name="qty"/></td>
     <?php
     
     if(isset($_POST['update_cart']))
     {
     
      $qty=$_POST['qty'];
      $update_qty="update cart set qty='$qty'";
      $run_qty=mysqli_query($con, $update_qty);
      
      $_SESSION['qty']=$qty;
      
      $total =$total*$qty;
      
     }
 
     
     
     
     ?>
    <td><?php echo "$". $single_price; ?></td>
      </tr>
      
      
      <?php }} ?>
      <tr align="right">
      <td colspan="4"><b> Sub Total:</b>
      <td><?php echo"$" . $total; ?></td>
      </tr>
      
      <tr align="center">
      <td colspan="2"><input type="submit" name="update_cart" value="Update Cart"/></td>
      <td><input type="submit" name="continue" value="Continue Shopping"/></td>
      <td><button><a href="checkout.php" style="text-decoration:none; color:black;">Checkout</a></button></td>
      </tr>
      
      
      </table>
   
   
   </form>
   
   <?php
   function updatecart(){
            global $con;
            
   $ip = getIp();
  
    if(isset($_POST['update_cart'])){
     foreach($_POST['remove'] as $remove_id){
     
     $delete_product = "delete from cart where p_id='$remove_id' AND ip_add='$ip'";
     $run_delete = mysqli_query($con,$delete_product);
     if($run_delete){
     echo "<script>window.open('cart.php','_self')</script>";
     }
     
     }
     }
     
    }
   if(isset($_POST['continue'])){
   
    echo "<script>window.open('index.php','_self')</script>";
   }
   echo @$up_cart = updatecart();
   
   ?>

 

Link to comment
Share on other sites

Don't run queries from inside a loop. Use a join to get all the data with a single query.

 

data

mysql> select * from cart;
+---------+---------+---------+------+
| cart_id | ip      | prod_id | qty  |
+---------+---------+---------+------+
|       1 | 123.321 |       1 |    2 |
|       2 | 123.321 |       2 |    5 |
|       3 | 123.321 |       3 |    2 |
|       4 | 321.333 |       1 |    1 |
|       5 | 321.333 |       3 |    3 |
+---------+---------+---------+------+

mysql> select * from product;
+---------+-------------+------------+
| prod_id | prod_title  | prod_price |
+---------+-------------+------------+
|       1 | Widget      |       5.99 |
|       2 | Gizmo       |      10.99 |
|       3 | Thingumajig |      19.99 |
+---------+-------------+------------+
 

query

SELECT prod_title
  , qty as quantity
  , prod_price as unitprice
  , prod_price * qty as price
FROM product 
    INNER JOIN cart USING (prod_id)
WHERE ip = '123.321';

+-------------+----------+-----------+-------+
| prod_title  | quantity | unitprice | price |
+-------------+----------+-----------+-------+
| Widget      |        2 |      5.99 | 11.98 |
| Gizmo       |        5 |     10.99 | 54.95 |
| Thingumajig |        2 |     19.99 | 39.98 |
+-------------+----------+-----------+-------+


Now you can loop through the single result set and accumulate the total with

 

$totalPrice += $row['price'];
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.