tomtom989898 Posted February 24, 2015 Share Posted February 24, 2015 I am working on a simple code for a scoreboard and need a little help. I have the scoreboard performing the right score calculation, however when I try to come up with level system with the number above, I am not able to get it to execute right. "script_fields": { "PlayerScore.Level": { "script": "if (doc['PlayerScore.score'].value <= 100) { doc['PlayerScore.Level'].value == 0} elseif (doc['PlayerScore.score'].value >= 100) { doc['PlayerScore.Level'].value == 1 }" } }, So bacially I want this to say, If playerScore.score is less than or = to 100, then your PlayerScore.Level is 1. Else if playerscore.score is greater than or = 101 but less than 200, then Playerscore.level is 2. I am actually doing about 10 levels, but if I can just get the first one to go, I should be good to get the other. any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/294847-php-and-codeigniter-script-with-if-else-statements/ Share on other sites More sharing options...
blacknight Posted February 24, 2015 Share Posted February 24, 2015 (edited) this is not php looks like js... but make sure your value is an init and not a string Edited February 24, 2015 by blacknight Quote Link to comment https://forums.phpfreaks.com/topic/294847-php-and-codeigniter-script-with-if-else-statements/#findComment-1506563 Share on other sites More sharing options...
QuickOldCar Posted February 24, 2015 Share Posted February 24, 2015 Here is a little function to make it easier for you <?php function scoreLevel($score){ if(!$score || $score <= 0){ return 1; } $score = $score -1; $level = (int) floor($score / 100) + 1; return $level; } //usage //echo scoreLevel('201'); //test in a loop foreach (range(0, 500) as $number) { echo "score:".$number." level:".scoreLevel($number)."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/294847-php-and-codeigniter-script-with-if-else-statements/#findComment-1506566 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.