newstudents Posted January 16, 2017 Share Posted January 16, 2017 (edited) I'm trying to get total price from cart table. The product_price in other table which is product. I'm only getting the latest price not the total price. Thanks function total_price () { $total = 0; global $db; $ip = getIp(); $sql = $db->query("SELECT * from cart WHERE ip_add='$ip'"); $row = $query->fetch(PDO::FETCH_ASSOC); if ($row) { } $no=$sql->rowCount(); // number of rows affected by the last SQL statement if ($no == 0) { echo ""; } else { foreach($sql as $row) $product_id = $row["p_id"]; $sql = $db->query("SELECT product_price from product WHERE product_id='$product_id'"); $no=$sql->rowCount(); // number of rows affected by the last SQL statement if ($no == 0) { echo ""; } else { foreach($sql as $row) $product_price = array($row["product_price"]); $values = array_sum($product_price ); $total += $values; } } echo "RM" . $total; } } I'm trying to get total price from cart table. The product_price in other table which is product. I'm only getting the latest price not the total price. Thanks Edited January 16, 2017 by newstudents Link to comment Share on other sites More sharing options...
Ingolme Posted January 16, 2017 Share Posted January 16, 2017 Your issue is that you keep overwriting the array here: $product_price = array($row["product_price"]); You should be adding the value to the array: $product_price = array(); foreach($sql as $row) { $product_price[] = $row["product_price"]; } But you can get the total straight from SQL: $get_sum = $db->prepare('SELECT SUM(product_price) AS total FROM product WHERE product_id = ?'); $get_sum->execute(array($product_id)); if($data = $get_sum->fetch(PDO::FETCH_ASSOC)) { $total = $data['total']; } Your code is vulnerable to hacking, you should research prepared statements. Link to comment Share on other sites More sharing options...
newstudents Posted January 16, 2017 Author Share Posted January 16, 2017 i have try the code..but the output still the same. only get latest price.hope you can help me Link to comment Share on other sites More sharing options...
Ingolme Posted January 16, 2017 Share Posted January 16, 2017 What code did you try? Link to comment Share on other sites More sharing options...
newstudents Posted January 16, 2017 Author Share Posted January 16, 2017 this code  $get_sum = $db->prepare('SELECT SUM(product_price) AS total FROM product WHERE product_id = ?');$get_sum->execute(array($product_id));if($data = $get_sum->fetch(PDO::FETCH_ASSOC)) { $total = $data['total'];} Link to comment Share on other sites More sharing options...
Ingolme Posted January 16, 2017 Share Posted January 16, 2017 I would assume product_id is unique, so there's only going to be one record in the database with that ID. That means that the sum of all the query results is only going to be the price of that product. Â I don't know the structure of your database and I don't know what sum you're trying to get, so I can't help any further until you can clarify. Link to comment Share on other sites More sharing options...
newstudents Posted January 16, 2017 Author Share Posted January 16, 2017 (edited) ya...product_id is unique. currently i have develop e-commerce website for my uni project. this code are for total price in cart part. in cart table there is 3 column which is p _id, ip_add and qty. in table product there is 6 column and on of that are product_price. so i want to get sum for total price for the item that user want to buy. the total price should get based on the total product that user add to cart. so the price should be get by product_id Edited January 16, 2017 by newstudents Link to comment Share on other sites More sharing options...
Ingolme Posted January 16, 2017 Share Posted January 16, 2017 There are several issues with your code. You're overwriting the variable $sql and $row inside the loop that's using those variables for iteration, you're also overwriting the value of $total on each iteration. Â Your code looks much more complicated than it needs to be. You should read the SQL tutorial. Assuming you don't have a "quantity" field, the function can be as simple as one SQL query with a JOIN and using the SUM() function. function total_price() { global $db; $ip = getIp(); $query = $db->prepare(' SELECT SUM(p.product_price) AS total FROM cart AS c JOIN product AS p ON p.product_id = c.p_id WHERE c.ip_add = :ip '); $query->bindParam(':ip', $ip); $query->execute(); $row = $query->fetch(PDO::FETCH_ASSOC); if($row) { $total = (float) $row['total']; } else { $total = 0; } return $total; } In the case that there's a quantity field, just change the SUM() function to multiply price by quantity: SELECT SUM(p.product_price * c.quantity) AS total FROM cart AS c JOIN product AS p ON p.product_id = c.p_id WHERE c.ip_add = :ip Link to comment Share on other sites More sharing options...
newstudents Posted January 17, 2017 Author Share Posted January 17, 2017 there is no output for that code....the code not capture the price Link to comment Share on other sites More sharing options...
Ingolme Posted January 17, 2017 Share Posted January 17, 2017 Do you understand what the code is doing? Link to comment Share on other sites More sharing options...
newstudents Posted January 17, 2017 Author Share Posted January 17, 2017 sorry i'm new to this. as i know the code is get the sum for product_price based on product_id.correct me if i'm wrong Link to comment Share on other sites More sharing options...
Ingolme Posted January 17, 2017 Share Posted January 17, 2017 Try to debug it. Find out why it's not giving the right value. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now