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
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.
Link to comment
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
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.