我有一个switch语句,它不会因任何原因改变变量。我默认将它定义为一个,而$ 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;
}
}
答案 0 :(得分:3)
您应该使用此功能(而不是开关),因为您可以根据输入轻松计算输出。您不希望列出所有可能的输入并将其与硬编码输出相匹配。一击到第11页就会中断。相反,执行计算以通过一些简单的数学计算得出每个数字:
function get_range($page) {
$cap = $page * 10;
return $cap - 9 . "," . $cap;
}
echo get_range(7);
// prints "61,70"