2013-10-04 44 views
-1

我有一個switch語句,並且因爲任何原因它不改變變量。我將它默認定義爲一個,$ getpage只會回顯爲1,而不是1,10。我不知道我做錯了什麼。有人請幫忙。PHP開關語句無法正常工作

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

使用不同的變量。它應該工作 – bksi

+1

適合我... ... [演示](http://phpfiddle.org/lite/code/ejq-xd2) – Neal

+0

默認不是強制性的 –

回答

3

您應該使用此功能(不是開關),因爲您的輸出可以很容易地從您的輸入計算得出。您不想列出每個可能的輸入並將其與硬編碼輸出相匹配。只要你點擊第11頁就會中斷。相反,通過一些簡單的數學運算來計算每個數字:

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

echo get_range(7); 
// prints "61,70" 
+0

這就解釋了,但是你能告訴我們交換機出了什麼問題嗎?> –

+0

我發現交換機出了什麼問題,條件語句並沒有一路開啓,但是這遠遠更聰明。 +1,非常感謝。只要我可以,我會選擇這個作爲答案。 – George