0

When i call this function on my webpage it should echo out the total value of the products in your shopping cart, But it only echo's out the value of the product with highest product id. Instead of the sum of these values combined.

function total_price() {

    $total = 0;
    global $link;
    $ip = getIp();
    $sel_price= "select * from cart where ip_add='$ip'";
    $run_price= mysqli_query($link, $sel_price);

    while($p_price=mysqli_fetch_array($run_price))  {
        $pro_id = $p_price['id'];
        $pro_price = "select * from products where id='$pro_id'";
        $run_pro_price = mysqli_query($link, $pro_price);
        while ($pp_price = mysqli_fetch_array($run_pro_price)){
            $product_price = array($pp_price['prijs']);
            $values = array_sum($product_price);
            $total = number_format((float)$values, 2, ',', '');  
        }
    }
    echo "€ " .$total;

}
2
  • You're creating an array with one price in it, then using array_sum() on it. So the sum is just the value of that one element. You're not adding to the previous rows. Commented Jan 3, 2015 at 0:59
  • 1
    Why don't you just use SELECT SUM(prijs) AS total in the query? Commented Jan 3, 2015 at 1:03

3 Answers 3

1

There's no reason to use array_sum. The sum of an array with one element is just the value of that element. You would use array_sum if you were pushing each price onto the array, and then calculating the sum at the end of the loop. But you can just add to the total value each time through the loop, so there's no need to do that.

    while ($pp_price = mysqli_fetch_array($run_pro_price)){
        $values += $pp_price['prijs'];
    }
}
$total = number_format((float)$values, 2, ',', '');  
echo "€" . $total;

You could also do everything in a single query:

$sel_total = "SELECT SUM(prijs) AS total
                FROM cart 
                JOIN products ON cart.id = product.id
                WHERE ip_add = '$ip'";
$res = mysqli_query($sel_total);
$row = mysqli_fetch_assoc($res);
$total = number_format($row['total'], 2, ',', '');
echo "€" . $total;
Sign up to request clarification or add additional context in comments.

Comments

0
$total += number_format((float)$values, 2, ',', '');  

should work

1 Comment

You should use number_format AFTER calculating the total.
0
<?php
function total_price() {
    $values = 0;
    $total = 0;
    global $link;
    $ip = getIp();
    $sel_price= "select * from cart where ip_add='$ip'";
    $run_price= mysqli_query($link, $sel_price);

    while($p_price=mysqli_fetch_array($run_price))  {
        $pro_id = $p_price['id'];
        $pro_price = "select * from products where id='$pro_id'";
        $run_pro_price = mysqli_query($link, $pro_price);
        while ($pp_price = mysqli_fetch_array($run_pro_price)){
            //$product_price = array($pp_price['prijs']);
            //this will cumulate sum 
            $values += $pp_price['prijs'];
            //$total = number_format((float)$values, 2, ',', '');  
        }
    }
    //this will format result
    $total = number_format((float)$values, 2, ',', '');
    echo "&euro; " .$total;

}

btw why dont you try using JOIN statement???

1 Comment

Get rid of array_sum. It should just be $values += $pp_price['prijs'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.