seany123 Posted December 1, 2008 Share Posted December 1, 2008 Okay well im creating a game and its now starting to get to the hard bits of coding. ill explain exactly what im trying to do then maybe somone can point me into the right direction. First, Im trying to create a Crime mod which based on different values in the database, the player will succeed, fail or fail & prison. So what im needing first is statement which will check what percentage of "maxnerve", "nerve" equals. if ($player->nerve < $player->maxnerve * 0.25) { } else if ($player->nerve >= $player->maxnerve * 0.25 || $player->nerve < $player->maxnerve * 0.50) { } else if ($player->nerve >= $player->maxnerve * 0.50 || $player->nerve < $player->maxnerve * 0.75) { } else if ($player->nerve >= $player->maxnerve * 0.75 || $player->nerve < $player->maxnerve * 1) { } else if ($player->nerve >= $player->maxnerve * 1){ } So maybe the code above works maybe it dont. Second, now i need each if statement to get 2 random numbers 1-100. then ill need to do something like this... If(randomnumber1 <= 10){crime sucess } else if(randomnumber1 >= 11 && randomnumber2 >= 10{ Crime Fail } else if(randomnumber2 >= 11 && randomnumber2 < 10 { Crime Fail and Prison } does anyone see what im getting at? i know what needs to be done i just dont know how to do it, all help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/ Share on other sites More sharing options...
dimond345 Posted December 1, 2008 Share Posted December 1, 2008 Hello. 1. code look like is not working. You must use && and not || Then $player->nerve = 65 will be working $player->nerve >= $player->maxnerve * 0.25 || $player->nerve < $player->maxnerve * 0.50 and not else if ($player->nerve >= $player->maxnerve * 0.50 && $player->nerve < $player->maxnerve * 0.75) { } First, Im trying to create a Crime mod which based on different values in the database, the player will succeed, fail or fail & prison. So what im needing first is statement which will check what percentage of "maxnerve", "nerve" equals. if ($player->nerve < $player->maxnerve * 0.25) { } else if ($player->nerve >= $player->maxnerve * 0.25 && $player->nerve < $player->maxnerve * 0.50) { } else if ($player->nerve >= $player->maxnerve * 0.50 && $player->nerve < $player->maxnerve * 0.75) { } else if ($player->nerve >= $player->maxnerve * 0.75 && $player->nerve < $player->maxnerve * 1) { } else if ($player->nerve >= $player->maxnerve * 1){ } So maybe the code above works maybe it dont. Second looks like is working fine. If you will be have problem, describe it. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702904 Share on other sites More sharing options...
seany123 Posted December 1, 2008 Author Share Posted December 1, 2008 well to be honest im looking for some general guidence... i literally wrote this code on the message page here on phpfreaks.. 5 mins ago. i havn't tested it. But a problem i do have, how do i get 2 random numbers 1-100?? Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702910 Share on other sites More sharing options...
dimond345 Posted December 1, 2008 Share Posted December 1, 2008 for random numbers you may use 1. standart rand() function: $random1 = rand(1,100); $random2 = rand(1,100); 2. finde class for it at www.phpclasses.org/ 3. write you own randnom function. rand() function is not very good, like in any other language standart random function. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702915 Share on other sites More sharing options...
seany123 Posted December 1, 2008 Author Share Posted December 1, 2008 i wouldnt think... the random function would have to be very good. as long as it does what it says it does give me a completely random number from 1-100. maybe this isnt as hard as i first thought it would be.... im starting to understand how to do the seperate bits... but putting them all together is something completely different. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702917 Share on other sites More sharing options...
seany123 Posted December 1, 2008 Author Share Posted December 1, 2008 ive come up with this but when i run the page... it just goes all white... so there must be errors. Just to make it less confusing... after the first }} all the code pretty much repeats itself just with different values. i copy and pasted. <?php if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){ $random1 = rand(1,100); $random2 = rand(1,100); If($random1 <= 10){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if (randomnumber1 >= 11 && randomnumber2 >= 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 11 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) { $random1 = rand(1,100); $random2 = rand(1,100); If($random1 <= 30){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 31 && randomnumber2 >= 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 31 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) { $random1 = rand(1,100); $random2 = rand(1,100); If($random1 <= 60){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 61 && randomnumber2 >= 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 61 && randomnumber2 < 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) { $random1 = rand(1,100); $random2 = rand(1,100); If($random1 <= 80){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 81 && randomnumber2 >= 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 81 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1){ $random1 = rand(1,100); $random2 = rand(1,100); If($random1 <= 90){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 91 && randomnumber2 >= 10){ $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 91 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-703354 Share on other sites More sharing options...
seany123 Posted December 2, 2008 Author Share Posted December 2, 2008 Can anyone see any problems with the above code? Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-704381 Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 First, turn on error reporting. <?php // add this at the top of your script error_reporting(E_ALL); ini_set('display_errors', TRUE); ?> Second, pretty code is happy code: <?php if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25) { $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 10) { $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if (randomnumber1 >= 11 && randomnumber2 >= 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 11 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) { $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 30) { $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 31 && randomnumber2 >= 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 31 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) { $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 60) { $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 61 && randomnumber2 >= 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 61 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) { $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 80){ $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 81 && randomnumber2 >= 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 81 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1) { $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 90) { $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); echo "You Suceeded in your crime."; echo "You gained $10 & 25 EXP."; } else if(randomnumber1 >= 91 && randomnumber2 >= 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You Failed your crime."; } else if(randomnumber2 >= 91 && randomnumber2 < 10) { $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); echo "You failed your crime & you want sent to prison"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-704400 Share on other sites More sharing options...
seany123 Posted December 2, 2008 Author Share Posted December 2, 2008 Okay i put that error reporting code in and got these errors: Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 7 Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 27 Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 48 Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 69 Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 90 Line 7 if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){ Line 27 else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) { Line 48 else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) { Line 69 else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) { Line 90 else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1){ Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-704454 Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 Well there you go $player is undefined. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705021 Share on other sites More sharing options...
seany123 Posted December 3, 2008 Author Share Posted December 3, 2008 right i have actually fixed the issue with the errors... now im getting a different problem.. if the code determines a sucess then it works great.. but if its a fail or fail & jail... then the page just goes white.. i was thinking maybe it was the "{ and }"s i have in place a distrupting the other codes. here is the part of code that are being used. if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){ $random1 = rand(1,100); $random2 = rand(1,100); if($random1 <= 10){ header("location: ../crime.php"); $query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id )); } else if (randomnumber1 >= 11 && randomnumber2 >= 10){ header("location: ../crime.php"); $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); } else if(randomnumber2 >= 11 && randomnumber2 < 10) { header("location: ../crime.php"); $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); } else { header("location: ../crime.php"); } } EDIT: i just ran error test and got these errors: Notice: Use of undefined constant randomnumber1 - assumed 'randomnumber1' on line 43 Notice: Use of undefined constant randomnumber2 - assumed 'randomnumber2' on line 49 Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705240 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 header("location: ../crime.php"); $query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id )); That query will never get executed. Put it above the header line if you want it to actually be executed. Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705246 Share on other sites More sharing options...
seany123 Posted December 3, 2008 Author Share Posted December 3, 2008 i fixed the : Notice: Use of undefined constant randomnumber1 - assumed 'randomnumber1' on line 43 Notice: Use of undefined constant randomnumber2 - assumed 'randomnumber2' on line 49 errors.. how stupid duh lol now gonna test Quote Link to comment https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705262 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.