dongyou4411 2013-12-03 19:23
浏览 46
已采纳

试图从php中的mysql表中排序数据表

Im not sure as to why my code wont sort, im pretty new to this and any help would be appreciated.

This is the file that reads in the content and then is suppose to sort it, but when i execute it on the browser the link to sort the data have no affect.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>Movie Reviews Display</title>

    </head>

    <body>

<?php
    $host="XXXXXXX"; 
    $username="XXXXXX"; // Mysql username 
    $password="XXXXXXX"; // Mysql password 
    $db_name="XXXXXXXX"; // Database name 
    $tbl_name="movies"; // Table name

    $con=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
    mysqli_select_db($con,"XXXXXX") or die ("no database");

    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

        $result = mysqli_query($con,"SELECT * FROM movies");
        ?>
        <h1>Movie Reviews Database</h1>
        <hr />
        <p />
        <?php
        echo "<table border='1'>
            <tr>
            <th>Review Date</th>
            <th>Title</th>
            <th>Rating</th>
            <th>Genre</th>
            <th>Release Date</th>
            <th>Reviewer Name</th>
            <th>Review Rateing</th>
            <th>Review Text</th>
            </tr>";

            while($row = mysqli_fetch_array($result))
              {
              echo "<tr>";
              echo "<td>" . $row['reviewDate'] . "</td>";
              echo "<td>" . $row['title'] . "</td>";
              echo "<td>" . $row['rating'] . "</td>";
              echo "<td>" . $row['genre'] . "</td>";
              echo "<td>" . $row['releaseDate'] . "</td>";
              echo "<td>" . $row['ReviewerName'] . "</td>";
              echo "<td>" . $row['reviewRating'] . "</td>";
              echo "<td>" . $row['reviewText'] . "</td>";
              echo "</tr>";
              }
        echo "</table>"; 


    if(isset($_GET['sort'])){   
        switch ($_GET['sort'] ){
        case 0: 
                    $query = "SELECT * FROM movies ORDER BY reviewDate DESC"; 
                    mysqli_query($con,$query);
                    break;
        case 1:
                    $query = 'SELECT * FROM movies ORDER BY title DESC';
                    mysqli_query($con,$query);
                    break;
        case 2:
                    $query = 'SELECT * FROM movies ORDER BY rating DESC';
                    mysqli_query($con,$query);
                    break;
        case 3:
                    $query = 'SELECT * FROM movies ORDER BY genre DESC'; 
                    mysqli_query($con,$query);
                    break;
        case 4: 
                    $query = 'SELECT * FROM movies ORDER BY releaseDate DESC';
                    mysqli_query($con,$query); 
                    break;
        case 5:
                    $query = 'SELECT * FROM movies ORDER BY ReviewerName DESC';
                    mysqli_query($con,$query); 
                    break;
        case 6:
                    $query = 'SELECT * FROM movies ORDER BY reviewRating DESC';
                    mysqli_query($con,$query);
                    break;           
        }
    }


mysqli_close($con);
?>
<p />
   Sort by...

      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>">Review Date</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>">Title</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>">Rating</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>">Genre</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>">Release Date</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>">Reviewer</a>
      <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>">Reviewer Rating</a>


    </body>

</html>
  • 写回答

2条回答 默认 最新

  • doushenjia8514 2013-12-03 19:41
    关注

    I would have made it a lot more complex, but I kept your general idea and improved upon.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Movie Reviews Display</title>
        </head>
        <body>
    <?php
    
        $host = "XXXXXXX"; 
        $username = "XXXXXX";
        $password = "XXXXXXX";
        $db_name = "XXXXXXXX";
        $tbl_name = "movies";
    
    
        $con = mysqli_connect($host, $username, $password);
    
        if(mysqli_connect_errno())
            $con = false;
        else
            $cdb = mysqli_select_db($con, $db_name);
    
    
        if(!$con) {
            echo "Failed to connect to database server!";
    
        else if(!$cdb) {
            echo "Failed to select database!";
    
        else {
            echo "        <h1>Movie Reviews Database</h1>
            <hr />
            <p />
            <table border='1'>
                <tr>
                    <th>Review Date</th>
                    <th>Title</th>
                    <th>Rating</th>
                    <th>Genre</th>
                    <th>Release Date</th>
                    <th>Reviewer Name</th>
                    <th>Review Rateing</th>
                    <th>Review Text</th>
                </tr>
    ";
    
            $sort = "";
            if(isset($_GET['sort'])) {
                switch ($_GET['sort'] ) {
                case 0: 
                            $sort = " ORDER BY reviewDate DESC"; 
                            break;
                case 1:
                            $sort = ' ORDER BY title DESC';
                            break;
                case 2:
                            $sort = ' ORDER BY rating DESC';
                            break;
                case 3:
                            $sort = ' ORDER BY genre DESC'; 
                            break;
                case 4: 
                            $sort = ' ORDER BY releaseDate DESC';
                            break;
                case 5:
                            $sort = ' ORDER BY ReviewerName DESC';
                            break;
                case 6:
                            $sort = ' ORDER BY reviewRating DESC';
                            break;           
                }
            }
            $result = mysqli_query($con, "SELECT * FROM `movies`" . $sort);
            while($row = mysqli_fetch_array($result)) {
                echo "            <tr>
    ";
                echo "                <td>" . $row['reviewDate'] . "</td>
    ";
                echo "                <td>" . $row['title'] . "</td>
    ";
                echo "                <td>" . $row['rating'] . "</td>
    ";
                echo "                <td>" . $row['genre'] . "</td>
    ";
                echo "                <td>" . $row['releaseDate'] . "</td>
    ";
                echo "                <td>" . $row['ReviewerName'] . "</td>
    ";
                echo "                <td>" . $row['reviewRating'] . "</td>
    ";
                echo "                <td>" . $row['reviewText'] . "</td>
    ";
                echo "            </tr>
    ";
            }
            echo "        </table>
    "; 
        }
        mysqli_close($con);
    
    ?>
            <p />Sort by...
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>">Review Date</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>">Title</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>">Rating</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>">Genre</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>">Release Date</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>">Reviewer</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>">Reviewer Rating</a>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题