gildas-Milandou Posted June 3, 2023 Share Posted June 3, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316934-hello-every-body-i-hope-you-are-all-fine/ Share on other sites More sharing options...
ginerjm Posted June 3, 2023 Share Posted June 3, 2023 Do you error checking enabled? Why are you doing a query inside that loop? You should do your data collection with a single query. Quote Link to comment https://forums.phpfreaks.com/topic/316934-hello-every-body-i-hope-you-are-all-fine/#findComment-1609364 Share on other sites More sharing options...
Barand Posted June 3, 2023 Share Posted June 3, 2023 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(); Quote Link to comment https://forums.phpfreaks.com/topic/316934-hello-every-body-i-hope-you-are-all-fine/#findComment-1609365 Share on other sites More sharing options...
mac_gyver Posted June 3, 2023 Share Posted June 3, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316934-hello-every-body-i-hope-you-are-all-fine/#findComment-1609367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.