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

PHP、MYSQL、带表排序器的HTML表,php,jquery,mysql,tablesorter,Php,Jquery,Mysql,Tablesorter,试图添加到我正在创建的页面。我对jquery知之甚少,所以我猜这就是我的错所在。我已经在页面的区域添加了所需的代码,并对表进行了必要的更改。我的表仍然像使用HTML一样呈现。想法 <html> <head> <title>Inventory</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jq

试图添加到我正在创建的页面。我对jquery知之甚少,所以我猜这就是我的错所在。我已经在页面的
区域添加了所需的代码,并对表进行了必要的更改。我的表仍然像使用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>

库存
$(document).ready(function(){$(“table”).tablesorter();});
谢谢

三件事:

  • 不要直接链接到tablesorter.com上的tablesorter-复制到您自己的服务器,或者在CDN上使用副本(这是我的at)
  • 在HTML的顶部添加一个
    ,否则IE会变成怪癖模式,让你的网站看起来很糟糕
  • 正如@MikeB所提到的,上面的代码将每一行包装在
    tbody
    中,请按如下方式更正代码(这只是一个片段):

    echo”
    种
    SKU
    鱼苗数
    朱维伯爵
    成虫计数
    笔记
    地方
    物主
    ";
    while($row=mysqli\u fetch\u assoc($result)){
    回声“;
    回显“$row['name']”;
    回显“$row['sku']”;
    回显“$row['quantityfry']”;
    回显“$row['quantityjuv']”;
    回显“$row['Quantity成人”“”;
    回显“$row['notes']”;
    回显“$row['location']”;
    回显“$row['owner']”;
    回声“;
    }
    mysqli_免费_结果($result);
    回声“;
    

  • 您正在为每一行添加一个tbody标记。很容易集成您可能还需要
    $(“#table”).datasorter()
    。不知道datasorter是否需要一个特定的元素,或者将自己附加到由
    $('table')
    返回的所有表。为什么不直接在源代码处对其进行排序呢?将
    echo放置在“;回声“
    echo”“;回声“
    之外,而
    循环。
    
    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>";