Jump to content

Adding a variable with the same name


monkeybidz

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\" /]
Link to comment
https://forums.phpfreaks.com/topic/9829-adding-a-variable-with-the-same-name/
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

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.