用sql数据库中的href和php对html表进行排序

问题描述:

我有一个html表,其中包含来自spql out with php的sql表的产品数据。我想通过单击表格列的标题对数据进行排序。

I have a html table that contains product data from an sql table that is spit out with php. I'd like to sort the data by clicking the heading of the table column.

我正在输出我的表格(php)

I'm outputting my table like so (php)

$product_list = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");

$product_list .= "<tr>
      <td>$id</td>
      <td><strong>$product_name</strong></td>
      <td>$$price</td>
      <td>$category</td>
      <td>$subcategory</td>
      <td>$date_added</td>
    </tr>";

Html

<table class="product-list">
  <tr>
  <th><a href='?sort=id'>ID</a></th>
  <th><a href='?sort=product_name'>Name</a></th>
  <th><a href='?sort=price'>Price</a></th>
  <th><a href='?sort=category'>Category</a></th>
  <th><a href='?sort=subcategory'>Subcategory</a></th>
  <th><a href='?sort=date_added'>Added</a></th>
  </tr>
  <?php echo stripslashes($product_list); ?>
</table>

我尝试了几种方法从在线示例中做到这一点但是当我点击标题时没有任何反应。

I've tried a few ways to do this from examples online but when I click the header nothing happens.

这是我测试的东西

$sort = array('id', 'product_name', 'price', 'category', 'subcategory', 'date_added');

$order = '';
  if (isset($_GET['sort']) && in_array($_GET['sort'], $sort)) {
  $order .= $_GET['sort'];
  }

$query = 'SELECT * FROM products ORDER BY '.$order;

// Then Output the table as above

发生了很多事情在页面上,我只包含了生成并显示表格的代码,所以它可能会阻止这一点,但我想确保在深入研究其余代码之前我会以正确的方式处理事情。

There is a lot happening on the page, I've only included the code that generates and displays the table so it could be something else stopping this but I'd like to make sure I'm going about things the right way before delving into the rest of the code.

如果您对此有任何建议,将不胜感激。

Any suggestions how you would go about this would be greatly appreciated.

至于您的PHP,您可以使用以下内容:

As for your PHP you can use something like this:

$product_list = "";    
$order = isset($_GET['sort'])?$_GET['sort']:'date_added';

$query = "SELECT * FROM products ORDER BY $order ";
$sql = mysql_query($query);

while($row = mysql_fetch_array($sql)){

$product_list .= "<tr>
      <td>".$row['id']."</td>
      <td>".$row['product_name']."</td>
      <td>".$row['price']."</td>
      <td>".$row['category']."</td>
      <td>".$row['subcategory']."</td>
      <td>".$row['date_added']."</td>         
        </tr>"; 

}

对于HTML,请确保包含您的名称接收参数的页面:

For your HTML make sure you include your the name of the page receiving the parameters:

<table class="product-list">
  <tr>
  <th><a href='page.php?sort=id'>ID</a></th>
  <th><a href='page.php?sort=product_name'>Name</a></th>
  <th><a href='page.php?sort=price'>Price</a></th>
  <th><a href='page.php?sort=category'>Category</a></th>
  <th><a href='page.php?sort=subcategory'>Subcategory</a></th>
  <th><a href='page.php?sort=date_added'>Added</a></th>
  </tr>
  <?php echo stripslashes($product_list); ?>
</table>

奖金:

你可以做使用这个 jquery插件在客户端进行排序。

You can do the sorting on the client side using this jquery plugin.