duankuang7928
duankuang7928
2014-05-27 13:03

根据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 donglefu6195 7年前
    <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.

    点赞 评论 复制链接分享
  • dongpao2871 dongpao2871 7年前

    You must pass the selected value to the php in some way: - by POST or GET, submitting a form or trough ajax

    Then you need to order by according to that value, and return the result to the screen.

    I assume you are new to PHP, so I'd recommend you to read about PDO, mysqli, or some other framework to connect to database, because mysql_* functions are deprecated, and to properly sanitize your string before putting it on the query.

    点赞 评论 复制链接分享

相关推荐