Jump to content

Getting a sum total


figuringout

Recommended Posts

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

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

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.