I would like to add the price values to a total_price and echo it.
My code takes values from session and makes a mysql query to take information from database tables. Once I have it, it shows the prices of each row. Now I'm adding a total price, but instead of taking the values of all prices and add them, it takes the values of the last row only. I would like that total_price to add all the values $price. Here is the code:
<?php
if (empty($_SESSION['cart'])) { echo "Nothing here";} else {
$whereIn = implode(',', $_SESSION['cart']);
$list_query = mysqli_query($con,"SELECT * FROM packages WHERE id IN ($whereIn)");
while($run_list = mysqli_fetch_array($list_query)){
$id = $run_list['id'];
$price = $run_list['package_price'];
$title = $run_list['package_title'];
$total_price = '';
$total_price += $price ;
echo '<p> Product :',$title,' | Price: ',$price; ?> <a href="add_to_cart/remove_from_cart.php?id=<?php echo $id; ?>">Remove item</a></p>
<?php
}?>
<p> Total Price: <?php echo $total_price;?></p>
<p> <button> Checkout </button></p>
<?php } ?>
$total_price = '';outside the loop... OR do$price[] = $run_list['package_price'];then use php.net/manual/en/function.array-sum.php. Also you current approach may be open to SQL injections..remove_from_cart.phpSQL acts like this SQL you are going to run into issues.