Loop Problem,plz help

A

Anonymous

Guest
$totalCost=0;
while($row = $result->fetch_assoc())
{

$totalCost+=$row["price"];
---------------------------------
why $totalcost=0?what happen?
 
Try this way:
Code:
<?php

$totalCost = 0;
while($row = $result->fetch_assoc()) {
     $totalCost += (int) $row["price"]; 
     // Or
     // $totalCost += intval($row["price"]); 
}

?>
Both ways will force/get the integer value inside your $row["price"].
Also make sure you give integer type (if this is the case) to your price table field.
 
Are u sure that $row["price"] contains the value of the new book?I htink it is not getting the proper value from the query.So please check it once more.
 
Are u sure that $row["price"] contains the value of the new book?I htink it is not getting the proper value from the query.So please check it once more.
 
Back
Top