nathan1 Posted January 28, 2008 Share Posted January 28, 2008 Hi people, wondering if someone would be able to help me out here... $c = $ro['client_number']; //total payments $p_result = mysql_query("SELECT * FROM payment WHERE client_number='$c'") or die(mysql_error()); $p_ro = mysql_fetch_array($p_result); $ptotal = 0; while ($p_ro = mysql_fetch_assoc($p_result)) $ptotal += $p_ro['payment']; I am trying to get the total payments, but its not getting all the payments, it only gets all but the first, and i have no idea. Is this the best way of doing it? I was thinking about doing a foreach but im not sure how to go about it..... thanks for any help Link to comment https://forums.phpfreaks.com/topic/88198-solved-need-some-help/ Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 SELECT SUM(payment) as tpayments FROM payment WHERE client_number=$c just migt be easier to let Mysql do the summation of that field That is if yer not displaying the history of payments Link to comment https://forums.phpfreaks.com/topic/88198-solved-need-some-help/#findComment-451274 Share on other sites More sharing options...
nathan1 Posted January 28, 2008 Author Share Posted January 28, 2008 thanks but yes i am displaying a history of the payments, so i need to echo it all out with php, no other ideas ? ??? Link to comment https://forums.phpfreaks.com/topic/88198-solved-need-some-help/#findComment-451525 Share on other sites More sharing options...
hitman6003 Posted January 28, 2008 Share Posted January 28, 2008 That's because when you call mysql_fetch_array outside of your loop it's retrieving the first row... $p_result = mysql_query("SELECT * FROM payment WHERE client_number='" . $ro['client_number'] . "'") or die(mysql_error()); $ptotal = 0; while ($p_ro = mysql_fetch_assoc($p_result)) { echo 'Payment on ' . $p_ro['date'] . ' of ' . $p_ro['payment'] . '<br />'; $ptotal += $p_ro['payment']; } echo 'Total payments: ' . $ptotal; Link to comment https://forums.phpfreaks.com/topic/88198-solved-need-some-help/#findComment-451533 Share on other sites More sharing options...
nathan1 Posted January 29, 2008 Author Share Posted January 29, 2008 thanks heaps, problem solved appreciate the help Link to comment https://forums.phpfreaks.com/topic/88198-solved-need-some-help/#findComment-452063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.