Jump to content

PHP and codeigniter script with if else statements


tomtom989898

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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 />";
}
?>
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.