Jump to content

Archived

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

monkeybidz

Adding a variable with the same name

Recommended Posts

I am trying to add variables with the same name, but different row. The number of rows and amount will vary depending on the item i will be looking at.

This is what i have now, which is not correctly adding the $listing_fee

$listing_fee = number_format($listing_fee,2);


$total_due = $listing_fee * mysql_num_rows($result);
$total_due = number_format($total_due, 2);

What im trying to do is add $listing_fee + itself depending on the number of rows result. If i have 1 row, $total due is fine. When a new row with the same variable is auto created it adds incorrectly.

Can someone help? [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

Share this post


Link to post
Share on other sites
You should only use the number_format() function when you want to display a number, not when you're going to be using it in computations.

Ken

Share this post


Link to post
Share on other sites
$total_due is the final display result of the computation.

If mysql_num_rows result in more than one row, say 5. Each result row contains $listing_fee, but with different values. I want to add $listing_fee to itself for each row and get a $total_due.

Share this post


Link to post
Share on other sites
Try something like this:
[code]<?php
$q = "select listing_fee from yourtable";
$rs = mysql_query($q);
$total = 0;
while ($rw = mysql_fetch_assoc($rs))
     $total += $rw['listing_fee'];
echo 'Total Fee: ' . number_format($total,2)."<br>\n";
?>[/code]

The "+=" operator means "take the value on the right and add it to the value on the left" it is equivalent to:
[code]<?php $total = $total + $rw['listing_fee']; ?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.