2017-06-16 134 views
-2

我試圖理清但說排序是不確定的:未定義指數:排序

注意:未定義指數:排序在C:\ XAMPP \ htdocs中\的index.php上線23

注意:未定義的索引:排序在C:\ XAMPP \ htdocs中\的index.php上線27

說明:未定義指數:排序在C:\ XAMPP \ htdocs中\的index.php上線31

說明:未定義指數:在35行排序C:\ xampp \ htdocs \ index.php

任何人都可以提供幫助嗎?

  <tbody> 
        <?php 
          $sortby = isset($_GET['sort']) ? $_GET['sort'] : 'id'; 
          if ($_GET['sort'] == 'id') 
          { 
           $sql .= " ORDER BY id"; 
          } 
          elseif ($_GET['sort'] == 'nome') 
          { 
           $sql .= " ORDER BY nome"; 
          } 
          elseif ($_GET['sort'] == 'idade') 
          { 
           $sql .= " ORDER BY idade"; 
          } 
          elseif($_GET['sort'] == 'peso') 
          { 
           $sql .= " ORDER BY peso"; 
          } 
           $serverName = "asdaf-DEMO"; 
           $options = array( "UID" => "sa", "PWD" => "fasdasd", "Database" => "demo"); 
           $conn = sqlsrv_connect($serverName, $options); 
            if($conn) { 
            $sql="SELECT * FROM cliente"; 
            $aResult=sqlsrv_query($conn, $sql); 
            while($rows = sqlsrv_fetch_array($aResult)) 
            { 
            $id = $rows['id']; 
            $nome = $rows['nome']; 
            $idade = $rows['idade']; 
            $peso = $rows['peso']; 


        ?> 
+1

[PHP: 「注意:未定義變量」, 「注意:未定義指數」 和 「通知:未定義偏移」]的可能的複製:(https://stackoverflow.com/後if/else條件此外覆蓋$sql問題/ 4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – schellingerht

+0

你上面代碼中的哪一行是行號23? –

+0

if($ _GET ['sort'] =='id') –

回答

1

你填寫$sortby變量,所以在if條件下用它代替$_GET['sort']

<tbody> 
    <?php 
      $sortby = isset($_GET['sort']) ? $_GET['sort'] : 'id'; 
      $sortOrder = isset($_GET['order']) ? $_GET['order'] : 'ASC'; 
      $orderBy = ''; 
      if ($sortby== 'id') 
      { 
       $orderBy .= " ORDER BY id"; 
      } 
      elseif ($sortby== 'nome') 
      { 
       $orderBy .= " ORDER BY nome"; 
      } 
      elseif ($sortby== 'idade') 
      { 
       $orderBy .= " ORDER BY idade"; 
      } 
      elseif($sortby== 'peso') 
      { 
       $orderBy .= " ORDER BY peso"; 
      } 
       $serverName = "asdaf-DEMO"; 
       $options = array( "UID" => "sa", "PWD" => "fasdasd", "Database" => "demo"); 
       $conn = sqlsrv_connect($serverName, $options); 
        if($conn) { 
        $sql="SELECT * FROM cliente" . $orderBy . ' ' . $sortOrder; 
        $aResult=sqlsrv_query($conn, $sql); 
        while($rows = sqlsrv_fetch_array($aResult)) 
        { 
        $id = $rows['id']; 
        $nome = $rows['nome']; 
        $idade = $rows['idade']; 
        $peso = $rows['peso']; 


    ?> 
+0

我現在沒有錯誤,但排序不工作 –

+0

我更新了我的答案。請檢查一下! –

+0

謝謝,順便說一句,它只對ASC進行排序,如果我再次點擊排序DESC,我該如何做? –

0

替換所有if ($_GET['sort']if($sortBy

你得到這個錯誤的原因是因爲你只有一個isset檢查。這很好,但稍後在代碼中,您可以返回使用$_GET['sort']而不檢查它是否已設置。

+0

注意:未定義變量:sortBy在C:\ xampp \ htdocs \ index.php上第23行 –

+0

對不起''sortby'(小寫)但你可以親眼看到。 –