figuringout Posted February 15, 2008 Share Posted February 15, 2008 I get a result from a MySQL db, the result has some html tags and a pound sign in. I'd like to add all the results together and get a total, but I'm not sure exactly how I do it. The code here simply adds the last result to itself, I'd like a total of all the results. $query2="SELECT text FROM orders_total where orders_id = " . $orders_id . " and text like '%<b>%'"; $result2=tep_db_query($query2); while($nt=mysql_fetch_array($result2)) { $nt = str_replace("£", "", $nt); echo strip_tags("$nt[text]"); $val1= strip_tags("$nt[text]"); $sum=$val1 + $val1; } print ' order object was created ' . $orders_id . '<br>' ; } } echo $val1; Link to comment https://forums.phpfreaks.com/topic/91253-getting-a-sum-total/ Share on other sites More sharing options...
rhodesa Posted February 15, 2008 Share Posted February 15, 2008 You want to keep adding to $sum with: $sum += $val1, then display $sum at the end <?php $query2="SELECT text FROM orders_total where orders_id = " . $orders_id . " and text like '%<b>%'"; $result2=tep_db_query($query2); while($nt=mysql_fetch_array($result2)) { $nt = str_replace("£", "", $nt); echo strip_tags("$nt[text]"); $val1= strip_tags("$nt[text]"); $sum += $val1; } print ' order object was created ' . $orders_id . '<br>' ; }//Where are these coming from? }//Where are these coming from? echo $sum; ?> Link to comment https://forums.phpfreaks.com/topic/91253-getting-a-sum-total/#findComment-467649 Share on other sites More sharing options...
The Little Guy Posted February 15, 2008 Share Posted February 15, 2008 $query2="SELECT text FROM orders_total where orders_id = " . $orders_id . " and text like '%<b>%'"; $result2=tep_db_query($query2); while($nt=mysql_fetch_array($result2)) { $nt = str_replace("£", "", $nt); echo strip_tags("$nt[text]"); $val1= strip_tags("$nt[text]"); if(!$su){$sum = array_sum($nt);$su=TRUE;} } print ' order object was created ' . $orders_id . '<br>' ; } } echo $val1; Link to comment https://forums.phpfreaks.com/topic/91253-getting-a-sum-total/#findComment-467654 Share on other sites More sharing options...
figuringout Posted February 15, 2008 Author Share Posted February 15, 2008 Thanks you guys - so so simple when you know how! thanks again Link to comment https://forums.phpfreaks.com/topic/91253-getting-a-sum-total/#findComment-467673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.