monkeybidz Posted May 17, 2006 Share Posted May 17, 2006 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 More sharing options...
kenrbnsn Posted May 17, 2006 Share Posted May 17, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9829-adding-a-variable-with-the-same-name/#findComment-36471 Share on other sites More sharing options...
monkeybidz Posted May 17, 2006 Author Share Posted May 17, 2006 $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. Link to comment https://forums.phpfreaks.com/topic/9829-adding-a-variable-with-the-same-name/#findComment-36508 Share on other sites More sharing options...
kenrbnsn Posted May 17, 2006 Share Posted May 17, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9829-adding-a-variable-with-the-same-name/#findComment-36615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.