Jump to content


Photo

Calculating or adding a variable to itself


  • Please log in to reply
4 replies to this topic

#1 monkeybidz

monkeybidz
  • Members
  • PipPipPip
  • Advanced Member
  • 330 posts

Posted 24 May 2006 - 02:06 AM

I have a variable $listing_fee. When $items_found = mysql_num_rows($result) give a result of more than one item, it creates a new row. Each row generates a $listing_fee for each item like it should. Each $listing_fee may be different. The problem here, is that the $listing_fee has the same name on each row, but i want to add(calculate the amounts. I cannot rename $listing_fee to $listing_fee1, $listing_fee2 and so on since the amount of rows may differ each time. does anyone know how i can make add $listing_fee + whatever other $listing_fee that may result?

Thanks in advance.

Hope you can understand what im trying to do. Oh!, almost forgot the amount that defines the $listing_fee does not come from mysql, it is passed from another page through include.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 02:20 AM

why not do like select count(listing_fee) as total where ... ?

if not, then just use a 2nd variable

$x = 0;
while (fetch array)
$x = $x + $listing_fee;
}
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 monkeybidz

monkeybidz
  • Members
  • PipPipPip
  • Advanced Member
  • 330 posts

Posted 27 May 2006 - 02:38 AM

I have a query that selects users info and items purchased from mysql database and
creates a different row for each result. I added a variable $listing_fee to
the result row. So now each row "mysql_num_rows($result)" prints out something like this:

item number -- description -- fee
_____________________________________
123456 -- standard plan -- 1.00
1245 -- standard plan -- 1.00
321321 -- beyond plan -- 3.00
45646 -- second plan -- 2.50
458778 -- standard plan -- 1.00
----------------------------------------------------
The following variables represent the above each row.
$plan = description
$listing_fee = fee
----------------------------------------------------
Everything works fine and looks good. I get the number result
on each row like it should according to the plan and fee.
Below is some info that each row uses for each item. I am trying
to add(+) the rersults for each $listing_fee on each row to get a
$total_due. This is where the problem is. Each row variable is named
$listing_fee as it is created as many time of row result. So if the
customer purchased 20 plans, i will have 20 rows with different
price depending on the plan purchased. I need to add these together.

$standard_fee = 1.00;
$second_fee = 2.50;
$beyond_fee = 3.00;

if($plan == '1') {
$listing_fee = $standard_fee;
}

if($plan == '2') {
$listing_fee = $second_fee;
}

if($plan == '3') {
$listing_fee = $beyond_fee;
}

This is what i have done so far and gets me close to the total.


$total_due = mysql_num_rows($result)* $listing_fee;


This would work if all where priced the same.

"$listing _fee" is not mysql. Info is gathered from the same page or script.

Can someone help? The entire code is too long to post.

#4 jajtiii

jajtiii
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 27 May 2006 - 03:27 AM

Along the lines of the original reply:

<php>
if($plan == '1') {
$listing_fee = $standard_fee;
}

if($plan == '2') {
$listing_fee = $second_fee;
}

if($plan == '3') {
$listing_fee = $beyond_fee;
}

$total += $listing_fee;
</php>

#5 monkeybidz

monkeybidz
  • Members
  • PipPipPip
  • Advanced Member
  • 330 posts

Posted 27 May 2006 - 03:39 AM

[img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] jajtiii
You got it!

I knew it could be done, i was just not explaining myself good enough.

Thanks so much.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users