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.