1

I have been on this for a few days now and cant quite work out what im doing wrong. If anyone could offer some advice or solution it would be greatly appreciated.

Right.... We have a simple sql script which searches shopping cart for a list of products in the cart based on session.

$sqlCommand = "SELECT * FROM shoppingcart WHERE sessionid ='$sessionid' ORDER BY id DESC"; 
$query = mysqli_query($connection, $sqlCommand) or die (mysqli_error());
$CartList1 = '';
while ($row = mysqli_fetch_array($query)) { 
    $custquantity1 = $row["quantity"];
    $custprice1 = $row["price"];
    $calcustprice1 = $custprice1 * $custquantity1;

    $CartList1 .= '' .$calcustprice1. ',';
}
mysqli_free_result($query);

What the above is doing is gathering the price and times it by the quantity. This bit works great.

What we want to do next is add all those values for a "total cart price".

so then we trim the trailing comma and assemble into an array. This isnt working as expected as its gathering them into a single key?

What we are asking is is there a solution or simpler way to add the output from $CartList1 together?

  $trimmedarray = rtrim($CartList1, ',');

    $a = array($trimmedarray);


    print_r($a);

output result

Array ( [0] => 6.99,41.93 ) 

So essentially we want to add up all the numerical values in $array[0]. Hopefully I have explained this properly and hopefully the above code will not get jumbled into the message just please bear in mind im new to stack overflow.

Many thanks in advance if anyone knows how I can easily combat this.

2
  • loop through the array $a and add all the values is that what you are talking about? Commented Dec 16, 2015 at 11:42
  • sort of... for some reason its placed all the values in a single key so instead of it being $a[0] =>6.99 and $a[1] => 41.99 etc its all in $a[0] else i may have beenable to probably use array_sum($a) or something im a bit confused to be honest Commented Dec 16, 2015 at 11:48

4 Answers 4

1
$sum=0;
foreach($a as $val){
  $sum+=floatval($val);
}

echo $sum;
Sign up to request clarification or add additional context in comments.

Comments

0
while ($row = mysqli_fetch_array($query)) { 
$custquantity1 = $row["quantity"];
$custprice1 = $row["price"];
$calcustprice1[] = $custprice1 * $custquantity1;

$CartList1 .= '' .$calcustprice1. ',';
}
echo array_sum($calcustprice1);
mysqli_free_result($query);

2 Comments

AMAZING!!! thank you so much this worked a treat, I hope you all have a nice time over the holidays, your time and expertise was very much appreciated.
My pleasure Castor , Thanks for the appreciation.
0
$cartList = array();

while ($row = mysqli_fetch_array($query)) {
    $cartList[] = $row["quantity"] * $row["price"];
}

$totalPrice = array_sum($cartList);

Comments

0

You can use array_sum(). For reference php.net - array_sum()

3 Comments

Thanks for the quick responce, Tried that but its only outputting Array ( [0] => 6.99,41.93 ) its not adding them together?
@Amitabh is also using array_sum(), and you marked his answer correct
Hi Vishal, Yes im sorry I think i just needed to see it in context, tried it a few times using different methods before consulting you guys :) Thanks you for your help it is always appreciated.

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.