Jump to content

[SOLVED] Need some help


nathan1

Recommended Posts

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

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

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.