2

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

我像这样输出我的表(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>

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

这是我测试过的东西

$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

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

任何建议您将如何解决此问题将不胜感激。

4

3 回答 3

4

至于你的 PHP,你可以使用这样的东西:

$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,请确保包含接收参数的页面名称:

<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 插件在客户端进行排序。

于 2013-05-23T04:37:23.113 回答
2

对于那些希望通过切换升序或降序来查看每个列排序的最终结果的人。

我不是 php 大师,所以我相信你会找到一些更好的方法来做到这一点,但它对我有用。

感谢所有在上面提供帮助的人,尤其是 isJustMe。

PHP

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

$ascdesc = isset($_GET['ascdesc'])?$_GET['ascdesc']:'DESC';
switch(strtoupper($ascdesc))
  {
  case 'DESC': $ascdesc = 'ASC'; break;
  case 'ASC': $ascdesc = 'DESC'; break;
  default: $ascdesc = 'DESC'; break;
  }

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

$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0){
while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $product_name = $row["product_name"];
    $price = $row["price"];
    $category = $row["category"];
    $subcategory = $row["subcategory"];
    $date_added = strftime("%b %d, %y",strtotime($row["date_added"]));    

$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>";
} else {
  $product_list = "You have no products listed in your store";
}

HTML

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

于 2013-05-24T05:22:00.003 回答
0

这里有多个问题:

  1. 您没有说明您的确切问题是什么以及您的预期结果是什么。
  2. $$price的代码中有
  3. 我从未见过没有随附页面的查询字符串显示为链接(即<a href="page.php?id=value">Text</a>
  4. 如果您以任何方式接受来自客户端的值,通常需要进行更多的有效性和完整性检查,尤其是当它们将成为数据库查询的一部分时。
  5. $sql您在顶部和$query底部向我们展示了您的查询的两个不同变量。确保使用正确的变量
  6. 据我所知,HTML 要求在值周围使用双引号,而不是单引号。
  7. 调试直到您可以找到问题所在或问题所在的区域。 var_dump(), print_r(), 和echo是你的朋友。
于 2013-05-23T04:39:37.420 回答