Shadowing Posted February 8, 2012 Share Posted February 8, 2012 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 "); Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/ Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315743 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315754 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315760 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315765 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 $total = (200 + (5 * 2 ) + (2000 / 5)) + ((5 * 2 ) + (2000 / 5) * (log(50000,3) / 100)); With already calculating (200 + (5 * 2 ) + (2000 / 5)) on both sides which is 610 im wanting it to read $total = 610 + (610 * .098) Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315779 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2012 Share Posted February 8, 2012 610 + (610 * .098) is the same as 610 * (1 + .098) or 610 * 1.098 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315785 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 php is saying this is 659 log(50000,3) / 100) is 9.8 $total = (200 + (5 * 2 ) + (2000 / 5)) + ((5 * 2 ) + (2000 / 5) * (log(50000,3) / 100)); and php is saying this is 669 $total = 610 + (610 * .098); Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315790 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315791 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 That's the correct value. Do you know how operator precedence works in math operations? You'll need to force it with parentheses. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315794 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315804 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315812 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315817 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315823 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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()); Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315839 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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()); Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315845 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 that would be a question for another thread. Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315847 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315852 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315861 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315873 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315887 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 OMG i didnt even realize there was a create PHP Code button on mysql admin haha. holy crap Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315895 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315898 Share on other sites More sharing options...
Shadowing Posted February 8, 2012 Author Share Posted February 8, 2012 idk been looking for a while now. I dont see any differance in any examples online. so idk what im doing wrong LOG(50000, 3.0) SELECT ( 200 + ( 5 *2 ) + ( 2000 /5 ) + (( 200 + ( 5 *2 ) + ( 2000 /5 ) ) * LOG(50000, 3.0) /100 )) AS result Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315925 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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 <> ' ' "; Quote Link to comment https://forums.phpfreaks.com/topic/256660-little-help-with-this-math-syntax/#findComment-1315926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.