Jump to content

little help with this math syntax


Shadowing

Recommended Posts

Hey guys im trying to do this math and im not sure how to write it in one line.

 

 resources + (5 * 2 ) + (2000 /  5 ) = $e + ($e * .05); 

my problem is how do I make it all equal $e so i can take $e + the 5 percent with the 5 percent being based off of  the amount of $e

 mysql_query("UPDATE planets SET resources= resources + (slave_camps * $slave_camps)
+ FLOOR(slaves / $resources_slave) + (* .05) WHERE id = 3112 "); 

Link to comment
Share on other sites

why do you need to write it on one line?

 

$e = ($resources + (5 * 2) + (2000 / 5)) + (($resources + (5 * 2) + (2000 / 5)) * .05);

 

take the entire value that would be set to $e and multiply it by .05 to get 5% of the value, then set it to $e.

Link to comment
Share on other sites

thanks for the reply AyKay47

 

I'm adding up fields on all rows in a table

Every hour it does a mass update updating all rows.

 

so i cant select first then update.

 

thanks so much. I dont know why i didnt think about just repeating the first equation with the percent then adding the two together.

cause thats obviously how it would be done. :) thanks alot

Link to comment
Share on other sites

for some reason im off by 10 on this

 

log(50000,3) is 9.8

 

 $total = (200 + (5 * 2 ) + (2000 / 5)) + ((5 * 2 ) + (2000 / 5) * (log(50000,3) / 100)); 

 

this is giving me 659 when it should be giving me 669

 

after I log it i am turning it into a percent by  deviding it by 100 to turn the 9.8 into .098

Link to comment
Share on other sites

for some reason im off by 10 on this

 

log(50000,3) is 9.8

 

 $total = (200 + (5 * 2 ) + (2000 / 5)) + ((5 * 2 ) + (2000 / 5) * (log(50000,3) / 100)); 

 

this is giving me 659 when it should be giving me 669

 

after I log it i am turning it into a percent by  deviding it by 100 to turn the 9.8 into .098

 

the way your order of operations is set up, this is of course the answer you should be receiving. I am not sure of your logic, so I can't really help.

Link to comment
Share on other sites

i took the log out and just replaced it with the value and it still gives me 659

 

 $total = (200 + (5 * 2 ) + (2000 / 5)) + ((5 * 2 ) + (2000 / 5) * (9.8 / 100)); 

                                  (610) + (59)

 

 

so the problem is

 

this equals 49

 

$total = ((5 * 2 ) + (2000 / 5) * (9.8 / 100)); 

 

and I need it to read 59 like this below

 

$total = 610 + (610 * .098);

Link to comment
Share on other sites

Wait, what? It isn't precedence at all, even though it appeared to be at first. The values in the second part of your operation come out to 49.2. That's the right answer. Where do you expect the extra 10 to come from?

 

((5 * 2 ) + (2000 / 5) * (9.8 / 100))
= 10 + 400 * .098
= 10 + 39.2
=49.2

Link to comment
Share on other sites

from the context of your original post, and the response to my reply, you want two of the same condition (equating to 610), with the second one being multiplied by .098. If so, both of your conditions are not mirrors as you have written it. You are not adding 200 to the second condition... e.g:

 

$total = (200 + (5 * 2 ) + (2000 / 5)) + ((200 + (5 * 2 ) + (2000 / 5)) * (9.8 / 100)); 
echo $total;

 

this will give you 669 and change

Link to comment
Share on other sites

lol i found my problem haha

 

I forgot to add the 200 on the 2nd half

 

$total = (200 + (5 * 2 ) + (2000 / 5) + ((5 * 2 ) + (2000 / 5) * (log(50000,3)) / 100));

 

it should be

 

$total = (200 + (5 * 2 ) + (2000 / 5) + ((200 + (5 * 2 ) + (2000 / 5)) * log(50000,3) / 100));

 

thanks guys.

Link to comment
Share on other sites

lol AyKay47 found my problem haha

 

I forgot to add the 200 on the 2nd half

 

$total = (200 + (5 * 2 ) + (2000 / 5) + ((5 * 2 ) + (2000 / 5) * (log(50000,3)) / 100));

 

it should be

 

$total = (200 + (5 * 2 ) + (2000 / 5) + ((200 + (5 * 2 ) + (2000 / 5)) * log(50000,3) / 100));

 

thanks guys.

 

no problem.

Link to comment
Share on other sites

yah major help guys.

 

Now that i have that all done

 

this is giving me 0 with no syntax errors

 

$update_res = "UPDATE planets SET resources= FLOOR(200 + (5 * 2) + (2000 / 5) + 
((200 + (5 * 2) + (2000 / 5)) * log(50000,3) / 100)) WHERE id <> ' ' ";		
mysql_query($update_res) or die(mysql_error()); 

Link to comment
Share on other sites

lol AyKay47 i just notice you added your name in the reply :)

acctualy i did notice the problem before i read your reply. that was funny though and awesome that you noticed it.

 

I turned all my values into numbers so it wasnt a value issue

 

not sure why im getting a 0 when its going into the data base

$update_res = "UPDATE planets SET resources= (200 + (5 * 2) + (2000 / 5) + 
((200 + (5 * 2) + (2000 / 5)) * log(50000,3) / 100)) WHERE id <> ' ' ";		

mysql_query($update_res) or die(mysql_error()); 

Link to comment
Share on other sites

If your intent is to do all the math in the query string, why not just test the calculations in a query to begin with instead of messing around with it in php?

 

SELECT ( 200 + ( 5 *2 ) + ( 2000 /5 ) + (( 200 + ( 5 *2 ) + ( 2000 /5 ) ) * log( 50000, 3 ) /100 )) AS result

result
610.619378215035

Link to comment
Share on other sites

Thanks Pikachu2000

 

I didnt know how to do a result like that. Thanks for that. I'll be using that alot

 

So i have two issues then

 

My knowledge of SQL is really hairy.

is it actually possible to type SQL in my script? where i dont have to have it in php?

for my understanding i can only type SQL into the SQL page on mysql admin

 

either way that shouldn't be equaling 610  then.

that should equal the 669 like it did with it in php

 

So i guess since its equaling 610 then that means SQL is confused so that is why php is even more confused and making it equal 0

sigh

this is a rare situation where i have to do the math in the query string

Link to comment
Share on other sites

There's nothing wrong with doing math in a query string, and it often is better to do it that way. Especially true in the case of an UPDATE query. Imagine the mess it would be to SELECT every record, perform a math operation on each one in php, then have to update all those records.

 

The query you've written should UPDATE all records in which the `id` field does NOT consist of merely a single space. Is `id` the PK index field for that table?

Link to comment
Share on other sites

You are absolutely right Pikachu!

 

thank you for noticing that I forgot that i actually recently learn the differance from blank and ' '. Which means space in the field.  but i think space and blank equals the same on default setting doesnt it?

 

either way i solved my problem lol. Something is really wrong with me some times

even though i was saying to my self and reading the statement exactly how it was saying. update all fields that isnt empty. I was looking at rows that were empty and wondering it wasnt being updated haha.

 

So that problem is gone. So im getting 610 now. "well i always was getting 610, just wasting looking at the right rows"

so now i need to figure out why its not doing the entire math equation to equal 669

 

 

Link to comment
Share on other sites

alright im getting closer

 

This gives me 669

SELECT ( 200 + ( 5 *2 ) + ( 2000 /5 ) + (( 200 + ( 5 *2 ) + ( 2000 /5 ) ) * .098)) AS result

 

and this gives me 669

SELECT ( 200 + ( 5 *2 ) + ( 2000 /5 ) + (( 200 + ( 5 *2 ) + ( 2000 /5 ) ) * (9.8 / 100))) AS result

 

so my problem is with using log. so going to go look that up

Link to comment
Share on other sites

what you are doing wrong is attempting to use a php function inside of an SQL string without concatenating it.

 

$update_res = "UPDATE planets SET resources= (200 + (5 * 2) + (2000 / 5) + 
((200 + (5 * 2) + (2000 / 5)) * " . log(50000,3) . " / 100)) WHERE id <> ' ' ";

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.