duanjuda5789 2012-02-21 23:46
浏览 209
已采纳

PHP - 使用下拉菜单对列表进行排序

I am stuck with this, I have been putting myself through a crash course in PHP all week making a fake amazon like site! I want to order my table using a drop down menu but cant work out how! I know the SQL thats the easy bit but the way I have tried to do it is follows:

Set up variables for example

$query = "SELECT bookname, bookauthor, bookpub, bookisbn
                FROM booktable
                ORDER BY bookname ASC";

$query2 = "SELECT bookname, bookauthor, bookpub, bookisbn
            FROM booktable
            ORDER BY bookauthor ASC";

Then I have my form=

<form name="sort" action="products.php" method="post">
<select name="order">
   <option value="choose">Make A Selection</option>
   <option value="title">Title</option>
   <option value="author">Author</option>
   <option value="publisher">Publisher</option>
   <option value="isbn">Book ISBN-10</option>
</select>
<input type="submit" value=" - Sort - " />
</form>

Now for the bit I cant do,

I set up the variable:

$sort = $_POST['order']; 

so that whatever the value of the dropdown menu is, will become assigned to the variable $sort..... (is that right?)

then I want to say IF the value of $sort is the same as lets say 'author' than $query = $query2 but I dont know how to say it without huge error courtesy of XAMPP!

I hope this all makes sense and that my method to try and achieve it is close to correct

Thank you

  • 写回答

3条回答 默认 最新

  • dongwei3120 2012-02-21 23:51
    关注

    To sort I would do the following:

    Change the option values to match the column names from your table Set your sort option including a default if they haven't selected anything:

    $sort=$_POST["order"]!="choose"?$_POST["order"]:"bookname";
    

    then simply set your query to use:

    ORDER BY ".mysql_real_escape_string($sort)." ASC
    

    This way you only need one query, rather than a query for each case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包