Jump to content

Recommended Posts

I have an error in my code, I am trying to display the total amount of each user unsing Total cart price:

 

here is the code :

 

// Total price function

      function total_cart_price(){

        global $con;

        $get_ip_add = getIPAddress();

        $total_price=0;

        $cart_query="select * from `cart_details` where ip_address='

        $get_ip_add'";

        $result=mysqli_query($con,$cart_query);

        while($row=mysqli_fetch_array($result)){

          $product_id=$row['product_id'];

          $select_products="select * from `products` where product_id='$product_id'";

          $result_products=mysqli_query($con,$select_products);

          while($row_product_price=mysqli_fetch_array($result_products)){

            $product_price=array($row_product_price['product_price']);

            $product_values=array_sum($product_price);

            $total_price+=$product_values;

          }

        }

        echo $total_price;

      }

 

And here is the final displayed of the executing code:  Total Price: 0 . Nothing is showing and there is not a error message too. Can someone help me please ?

I mean the is nothing that showed, Total Price: 0.

All you need is one query

$result = $pdo->prepare("SELECT SUM(p.product_price)
                         FROM cart_details c
                             JOIN
                             products p ON c.product_id = p.product_id
                         WHERE c.ip_address = ?
                         ");
$result->execute([ getIPAddress() ]);     

$total_price = $result->fetchColumn();

 

ps, everyone on the same network will have the same ip address. if you are going to use a database based cart, you need to generate a unique value per visitor to use to identify each cart. the simplest way of doing this is to start a session, then use the session id as the identifier.

pps, your cart should have a quantity column, so that someone could have more than one of each item.

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.