-1

我有一个 switch 语句,无论出于何种原因,它都不会更改变量。我默认将其定义为 1,而 $getpage 只会回显为 1,而不是 1,10。我不知道我做错了什么。请有人帮忙。

if (!isset($_SESSION['username'])) {
    echo '<meta http-equiv="refresh" content="0; url=memberarea.php">';
}
else {
    if (!isset($_GET['sex']) &&
       !isset($_GET['age']) &&
       !isset($_GET['page'])) {
        $_GET['age'] = "x";
        $_GET['sex'] = "x";
        $_GET['page'] = 1;
        $getage = mysqli_real_escape_string($connect, $_GET['age']);
        $getsex = mysqli_real_escape_string($connect, $_GET['sex']);
        $getpage = mysqli_real_escape_string($connect, $_GET['page']);
    }
    else {
        if ($_GET['age'] != "12-15" ||
            $_GET['age'] != "16-18" ||
            $_GET['age'] != "19-25" ||
            $_GET['age'] != "26-35" ||
            $_GET['age'] != "36+") {
            $_GET['age'] = "x";
            $getage = mysqli_real_escape_string($connect, $_GET['age']);
        }
        else {
            $getage = mysqli_real_escape_string($connect, $_GET['age']);
        }

        if ($_GET['sex'] != "male" ||
           $_GET['sex'] != "female" ||
           $_GET['sex'] != "x") {
            $_GET['sex'] = "x";
            $getsex = mysqli_real_escape_string($connect, $_GET['sex']);
        }
        else {
            $getsex = mysqli_real_escape_string($connect, $_GET['sex']);
        }

        if (ctype_digit($_GET['page']) === FALSE || $_GET['page'] > 10) {
            $_GET['page'] = 1;
            $getpage = mysqli_real_escape_string($connect, $_GET['page']);
        }
        else {
            $getpage = mysqli_real_escape_string($connect, $_GET['page']);
        }

        if ($getsex === "all") {
            $getsex = "*";
        }
        if ($getage === "all") {
            $getage = "*";
        }

        switch($getpage) {
            case 1:
                $getpage = "1,10";
                break;
            case 2:
                $getpage = "11,20";
                break;
            case 3:
                $getpage = "21,30";
                break;
            case 4:
                $getpage = "31,40";
                break;
            case 5:
                $getpage = "41,50";
                break;
            case 6:
                $getpage = "51,60";
                break;
            case 7:
                $getpage = "61,70";
                break;
            case 8:
                $getpage = "71,80";
                break;
            case 9:
                $getpage = "81,90";
                break;
            case 10:
                $getpage = "91,100";
                break;
        }
    }
4

1 回答 1

3

您应该为此使用一个函数(而不是开关),因为您的输出可以很容易地从您的输入中计算出来。您不想列出所有可能的输入并将其与硬编码的输出相匹配。一旦您点击第 11 页,这就会中断。相反,执行计算以通过一些简单的数学得出每个数字:

function get_range($page) {
    $cap = $page * 10;
    return $cap - 9 . "," . $cap;
}

echo get_range(7);
// prints "61,70"
于 2013-10-04T16:43:31.603 回答