duankuang7928 2014-05-27 13:03
浏览 84
已采纳

根据html <select>选项重新排序mysql结果?

another possibly dumb question... I have a database for reviews on items from a webpage. I also have the reviews populating a table on my page based off of the mysql tables data.

What i want to do, and I've searched online but not managed to find a solution, is to reorder the code based upon the selection made on my html dropdown menu.

Any advice?

The code as it stands. If any more is required I can provide it.

    <p>Order By: <select>
  <option value="newest" id="newest">Newest First</option>
  <option value="oldest" id="oldest">Oldest First</option>
  <option value="highest" id="highest">Highest Rating</option>
  <option value="lowest" id="lowest">Lowest Rating</option>
</select></p>

<?php
include "common.php";

mysql_connect("HOST", "USER", "PASS") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());
 $sql = "SELECT email, review, rating FROM reviews ORDER BY review_id DESC ";
  $result = mysql_query($sql)or die(mysql_error());

  echo "<table>";
  echo "<tr><th>User</th><th>&nbsp&nbsp&nbsp</th><th>Review</th><th>&nbsp&nbsp&nbsp</th><th>Rating</th></tr>";

  while($row = mysql_fetch_array($result)){
    $email     = $row['email'];
    $review    = $row['review'];
    $rating    = $row['rating'];

echo "<tr>
  <td>".$email."</td>
  <td></td>
  <td>".$review."</td>
  <td></td>
  <td>".$rating."</td>
  </tr>";
}
?>  
  • 写回答

2条回答 默认 最新

  • donglefu6195 2014-05-27 13:15
    关注
    <p>Order By: 
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="GET">
        <select name="ordering">
          <option value="pk DESC" id="newest">Newest First</option>
          <option value="pk ASC" id="oldest">Oldest First</option>
          <option value="rating DESC" id="highest">Highest Rating</option>
          <option value="rating ASC" id="lowest">Lowest Rating</option>
        </select>
    </form>
    </p>
    
    <?php
    include "common.php";
    
    mysql_connect("HOST", "USER", "PASS") or die(mysql_error());
    mysql_select_db("DB") or die(mysql_error());
    if(isset($POST['ordering'])) { 
        $sql = "SELECT email, review, rating FROM reviews ORDER BY" . $POST['ordering'];
    } else {
         $sql = "SELECT email, review, rating FROM reviews ORDER BY review_id DESC ";
    }
     $result = mysql_query($sql)or die(mysql_error());
    
      echo "<table>";
      echo "<tr><th>User</th><th>&nbsp&nbsp&nbsp</th><th>Review</th><th>&nbsp&nbsp&nbsp</th><th>Rating</th></tr>";
    
      while($row = mysql_fetch_array($result)){
        $email     = $row['email'];
        $review    = $row['review'];
        $rating    = $row['rating'];
    
    echo "<tr>
      <td>".$email."</td>
      <td></td>
      <td>".$review."</td>
      <td></td>
      <td>".$rating."</td>
      </tr>";
    }
    ?>  
    

    All you need to do is wrap the select in a form that submits to itself, change the option values to partial SQL queries, and finally make a conditional SQL statement based off of the form input.

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

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算