【问题标题】:Sorting html table with a href and php from sql database使用 sql 数据库中的 href 和 php 对 html 表进行排序
【发布时间】:2013-05-18 07:36:51
【问题描述】:

我有一个 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

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

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

【问题讨论】:

  • 你只需要调试你的代码就能看到结果sql。

标签: php html sql sorting html-table


【解决方案1】:

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

我不是 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); ?>

【讨论】:

    【解决方案2】:

    至于你的 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>
    

    奖金:

    您可以使用this jquery 插件在客户端进行排序。

    【讨论】:

    • 谢谢,我尝试了上面的方法,但是页面刷新了,什么也没做。在尝试 jquery 插件之前,我想让它与 php 一起工作:P
    • @isJustMe - 我只是得到错误,目前正在处理语法
    • 是的尝试添加括号,它仍然什么都不做。我已经将我的代码剥离为原始 sql 查询的表输出和排序代码,所以我认为没有任何冲突。
    • @Bjorn 好的,我又更新了一次,你能试试新代码吗
    • 抱歉还是不行。我回应了$order,它肯定得到了正确的变量。我在想 jquery 插件可能很快就会被关注:D
    【解决方案3】:

    这里有多个问题:

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

    【讨论】:

    • 1.我的预期结果是“通过单击表格列的标题对数据进行排序”。例如。点击 ID 按 ID 列的数字顺序排序,点击 Price 按价格列的数字顺序排序等。 2. $$Price 从 $price 变量中输出一个数值,我在前面添加了一个 $。它有效,不知道它的语法不正确。 3.不知道需要说明的页面,这是我第一次做这种事情并假设它会在当前页面上查找查询。
    • 4.您会建议进行哪些检查? 5.这两个不同的变量是因为底部的代码是我在网上找到的一个例子,我定制了适合我网站上的代码。我确实提到它来自一个例子。 6. 单引号有效,当我从 php 吐出 html 数据时,我遇到了将单引号与双引号混合的问题。 7. 将进行调试,但看起来我的问题是没有在查询中提及页面作为开始,所以基本上没有发生任何事情。
    • @Bjorn 1. 我不是来破译你的想法的。你的问题是因为某个问题。我不知道您的确切问题,因此我无法为您提供该问题的确切解决方案。 2. 如果$price 为5,$$price 返回$5,可能为0 或未定义。这也是不好的做法。 3. 现在你知道了。 4. 目前超出你学习范围的检查。现在只是为了好玩而编写代码。 5. 即使是经验丰富的开发人员,也会经常犯小错误和语法错误。 6. 适合你的和适合所有人的不同,有时也不同于 W3C 标准。
    • 我相信你明白我不是一个熟练的 php 编码器,这就是为什么我问其他人将如何实现这一点。我不一定要分析我的代码并破译我的问题,我更多的是寻找一种方法来真正实现我的目标。感谢您的意见,我会看我下次问什么:P
    猜你喜欢
    • 2012-01-22
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多