未定义的索引:排序

时间:2017-06-16 09:01:47

标签: php

我正在尝试排序,但是说排序是未定义的:

注意:未定义的索引:在第23行的C:\ xampp \ htdocs \ index.php中排序

注意:未定义的索引:在第27行的C:\ xampp \ htdocs \ index.php中排序

注意:未定义的索引:在第31行的C:\ xampp \ htdocs \ index.php中排序

注意:未定义的索引:在第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'];


                    ?>

2 个答案:

答案 0 :(得分:1)

您填写$sortby变量,因此如果条件允许,请使用它而不是$_GET['sort']。在$sql条件之后另外覆盖if/else

<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'];


    ?>

答案 1 :(得分:0)

将所有if ($_GET['sort']替换为if($sortBy

您收到此错误的原因是您只有一个isset检查。这很好但后来在代码中你回到使用$_GET['sort']而不检查它是否已设置。