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.

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.