gum1982 Posted February 1, 2010 Share Posted February 1, 2010 Hello can someone please help me im running a foreach loop that is pulling the prices value out off my database as follows. this is the function function one_life() { db_connect(); $query = "SELECT * FROM paypal_payment_info order by paypal_payment_info.mc_gross DESC"; $result = mysql_query($query); if(!$result) { echo mysql_error(); } $result = db_result_to_array($result); return $result; } $one_lifes = one_life(); This is my foreach loop <?php foreach($one_lifes as $one_life) { ?> <?php echo $one_life['mc_gross']; ?> <?php } ?> So this gives me the values i have a list like this. 200 10 30 200 350 What i need to do is add all these values together and echo one overall amount which would be say 790 for the example. Any Help Please? Link to comment https://forums.phpfreaks.com/topic/190525-foreach-adding-up/ Share on other sites More sharing options...
jl5501 Posted February 1, 2010 Share Posted February 1, 2010 <?php $total = 0; foreach($one_lifes as $one_life) { echo $one_life['mc_gross']; $total += $one_life['mc_gross']; } echo 'Total: '.$total; ?> Link to comment https://forums.phpfreaks.com/topic/190525-foreach-adding-up/#findComment-1004946 Share on other sites More sharing options...
gum1982 Posted February 1, 2010 Author Share Posted February 1, 2010 Legend Link to comment https://forums.phpfreaks.com/topic/190525-foreach-adding-up/#findComment-1004949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.