如果& Else坚持回归

时间:2014-05-10 09:53:36

标签: php math if-statement

(抱歉我的英文写得不好)。我遇到了一个问题,我遇到了一个if和else系统,它会返回你所在的级别。

一切都很好,如果你有20次点击,它会告诉你你是2级,如果你有550次点击,它会告诉你是6级。但当你达到1500次点击时,它需要说级别9,但它仍然会说8级。

我的代码:

$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>

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

我从您当前的代码中看到的问题是您有

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

我认为应该是

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

由于$score['clicks']不存在,您将始终以8级

结束

答案 1 :(得分:1)

使它成为一个像这样的开关/案例陈述可能更容易和更具可读性:

$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;
}