Question

(sorry for my bad English writing). I've got a problem where I'm stuck with an if and else system that returns the level that you're.

Everything is good, if you have 20 clicks it will tell you that you're level 2, if you have 550 clicks it will tell you're level 6. But when you go up to 1500 clicks it needs to say level 9, but it will still say level 8.

My code:

$levelown = 'Level 1';

function ifElse() {

    global $levelown;
    global $arrayIP;

    if($arrayIP['clicks'] >= 0 && $arrayIP['clicks'] <= 49)
        {
            $levelown = 'Level 2';
        }
    /* .... More if and elses with levels */

    // This is the problem, this will keep telling me that I'm level 8.
    elseif($arrayIP['clicks']>=3000)
        {
            $levelown = 'Level ' . floor(($score['clicks']/1000)+8);
        }



and you are <strong><?php echo $levelown; ?></strong>

Thank you for helping!

Was it helpful?

Solution

The problem that i saw from your current code is that you have

 elseif($arrayIP['clicks']>=3000)
        {
            $levelown = 'Level ' . floor(($score['clicks']/1000)+8);
        }

which i think should be

 elseif($arrayIP['clicks']>=3000)
        {
            $levelown = 'Level ' . floor(($arrayIP['clicks']/1000)+8);
        }

since $score['clicks'] isn't present you will always end up with level 8

OTHER TIPS

It's probably easier and more readable to make it a switch/case statement like so:

$highscore   = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore ORDER BY clicks DESC LIMIT 0,50 ");
$ipquery     = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore WHERE ip = '".$_SERVER['REMOTE_ADDR']."'");
$arrayIP     = $ipquery->fetch_array();

$levelown = 1;

function ifElse() {
global $levelown;
global $arrayIP;

    switch (true) {
        case $arrayIP['click'] > 3000:
            $levelown = floor(($arrayIP['click']/1000)+8);
            break;
        case $arrayIP['click']== 3000:
            $levelown = 11;
            break;
        case $arrayIP['click'] >= 2000:
            $levelown = 10;
            break;
        case $arrayIP['click'] >= 1500:
            $levelown = 9;
            break;
        case $arrayIP['click'] >= 1000:
            $levelown = 8;
            break;
        case $arrayIP['click'] >= 750:
            $levelown = 7;
            break;
        case $arrayIP['click'] >= 500:
            $levelown = 6;
            break;
        case $arrayIP['click'] >= 350:
            $levelown = 5;
            break;
        case $arrayIP['click'] >= 200:
            $levelown = 4;
            break;
        case $arrayIP['click'] >= 50:
            $levelown = 3;
            break;
        case $arrayIP['click'] >= 0:
            $levelown = 2;
            break;
    }
    $levelown = "Level " . $levelown;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top