PHP通过表单排序

时间:2017-05-27 18:03:56

标签: php mysql

我有一个页面显示来自MYSQL数据库的信息。

数据库表 enter image description here

我制作了一个包含排序选项的表单

表格

<form action= "properties.php" method="post" name="SortingForm">

            <label for="SortBy">Sort by</label>
            <select name="SortBy">
                  <option value="Added">Date Added</option>
                  <option value="Location">Location</option>
                  <option value="Type">Type</option>
                  <option value="Price">Price</option>
            </select>

             <input type="radio" name="SortingOrder" value="Ascending" checked> Ascending
             <input type="radio" name="SortingOrder" value="Descending"> Descending<br>

        </form>

这是PHP默认显示信息的方式

输出

<?php
        require("connect.php");

        //Linking
        $link = connectToDB();

        //SQL Query
        $sql = "SELECT tbl_property.propertyid,tbl_property.imagename,tbl_property.locationid,tbl_property.typeid,tbl_property.price,
                             tbl_town.townname,
                             tbl_type.typename
                  FROM tbl_property,tbl_town,tbl_type
                  WHERE tbl_town.townid = tbl_property.locationid AND tbl_type.typeid = tbl_property.typeid";

        //Execute
        $result = $link->query($sql);

            //Table Start
            echo "<table class='table'>";
                echo "<tr>";
                    echo "<th>Picture</th>";
                    echo "<th>Location</th>";
                    echo "<th>Type</th>";
                    echo "<th>Price</th>";
                    echo "<th>More Information</th>";
                        if(isset($_SESSION['id'])){     
                        echo "<th>Edit Property</th>";
                        echo "<th>Delete Property</th>";
                        }
                echo "</tr>";

                    while ($row = $result->fetch_assoc()) {
                        //Image Name
                        $ImageGetter = "images/".$row['imagename'];
                        //Information

                            echo "<tr>";
                                echo '<td><img height="150" width="200"" src="'.$ImageGetter.'" /></td>';
                                echo "<td>".$row['townname']."</td>";
                                echo "<td>".$row['typename']."</td>";
                                echo "<td>&euro;".$row['price']."</td>";
                                echo "<td>"."<a href=\"profile.php?id=".$row['propertyid']."\"> <button type='button' class='btn btn-default'>More Info</button></a>"."</td>";

                                        //If user is logged in, give extra buttons
                                        if(isset($_SESSION['id'])){     
                                            echo "<td>"."<button type='button' class='btn btn-info'>Edit</button>"."</td>";
                                            echo "<td>"."<a href=\"delete.php?id=".$row['propertyid']."\"> <button type='button' class='btn btn-danger'>Delete</button></a>"."</td>";
                                        }

                            echo "</tr>";

                    }
            echo "</table>";
    ?>

我的问题是,是否有可能使数据库中显示的信息从表单中选择的排序选项发生变化,如果是,如何变更?因为我无法弄清楚。请记住,显示确实有效,页面上的所有内容都能正确显示,但按最旧到最新排序。

1 个答案:

答案 0 :(得分:1)

您可以在语句末尾使用ORDER BY以某种方式对行进行排序。

例如:select ... FROM ... WHERE ... ORDER BY propertyid DESC会将您的行从最高的propertyid排序到最低的propertyid。