带表格排序器的 PHP、MYSQL、HTML 表格

标签 php jquery mysql tablesorter

尝试添加 tablesorter添加到我正在创建的页面。我对 jquery 知之甚少,所以我猜这就是我的错。我已在<head>中添加了所需的代码我的页面区域,并对我的表格进行了必要的更改。我的表格仍然像仅使用 HTML 一样呈现。有想法吗?

<html>
 <head>
  <title>Inventory</title>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
   <script type="text/javascript" src="http://tablesorter.com/__jquery.tablesorter.min.js"></script>
   <script type="text/javascript">
     $(document).ready(function(){ $("table").tablesorter(); });
   </script>
 </head>
 <body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$query = "SELECT 
            products.name,
            products.sku,
            inventory.quantityfry,
            inventory.quantityjuv,
            inventory.quantityadult,
            inventory.notes,
            inventory.location,
            inventory.owner
          FROM 
            products
          INNER JOIN 
            inventory
          ON
            products.sku=inventory.sku";

$result = mysqli_query($con,$query) or die(mysqli_error($con));
echo "<table border='1' id='table' class='tablesorter'>
<thead>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>

</tr>
</thead>";

while ($row = mysqli_fetch_assoc($result)) {
    echo "<tbody>";
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['sku'] . "</td>";
    echo "<td>" . $row['quantityfry'] . "</td>";
    echo "<td>" . $row['quantityjuv'] . "</td>";
    echo "<td>" . $row['quantityadult'] . "</td>";
    echo "<td>" . $row['notes'] . "</td>";
    echo "<td>" . $row['location'] . "</td>";
    echo "<td>" . $row['owner'] . "</td>";
    echo "</tr>";
    echo "</tbody>";    
}

mysqli_free_result($result);

echo "</table>";

mysqli_close($con);
?>     
 </body>



</html>

谢谢!

最佳答案

三件事:

  1. 不要直接链接到tablesorter.com 上的tablesorter - 复制到您自己的服务器,或使用CDN 上的副本(这是我的fork of tablesorter,位于cdnjs.com)。
  2. 包括 <!DOCTYPE html>位于 HTML 的顶部,否则 IE 将更改为怪异模式,从而使您的网站看起来很糟糕。
  3. 正如 @MikeB 提到的,上面的代码将每一行包装在 tbody 中。 ,更正代码如下(这只是一个片段):

    echo "<table border='1' id='table' class='tablesorter'>
    <thead>
    <tr>
    <th>Species</th>
    <th>SKU</th>
    <th>Fry Count</th>
    <th>Juvie Count</th>
    <th>Adult Count</th>
    <th>Notes</th>
    <th>Location</th>
    <th>Owner</th>
    
    </tr>
    </thead><tbody>";
    
    while ($row = mysqli_fetch_assoc($result)) {
    
        echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['sku'] . "</td>";
        echo "<td>" . $row['quantityfry'] . "</td>";
        echo "<td>" . $row['quantityjuv'] . "</td>";
        echo "<td>" . $row['quantityadult'] . "</td>";
        echo "<td>" . $row['notes'] . "</td>";
        echo "<td>" . $row['location'] . "</td>";
        echo "<td>" . $row['owner'] . "</td>";
        echo "</tr>";
    
    }
    
    mysqli_free_result($result);
    
    echo "</tbody></table>";
    

关于带表格排序器的 PHP、MYSQL、HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747897/

相关文章:

mysql外键未违反

php - 在复选框、单选按钮或下拉菜单上执行 mysql_real_escape_string?

php - 使用POST php ios上传图像数据

php - Zend_Form_Element_Captcha 异常问题...?

php - 从 Symfony 中的存储库内部记录

javascript - 将 .txt 而不是图像文件加载到 jquery 中

javascript - 切换显示/隐藏 (jQuery)

mysql - 是MYSQL的表关系错误吗?

php - Drupal 在查询时取出 $ 符号

jquery - IE 上的主要 JQuery 错误无法重现 - 在这种情况下我可以做什么来解决这个错误?