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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.