2013-12-23 65 views
0

试图添加tablesorter添加到我正在创建的页面。我对jquery知之甚少,所以我猜这就是我的错。我在页面的<head>区域添加了所需的代码,并对我的表进行了必要的更改。我的表格仍然像HTML一样呈现。想法?PHP,MYSQL,带表格的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> 

谢谢!

+3

你添加一个TBODY标签,每行 –

+0

尝试http://datatables.net/很容易集成 –

+0

您可能还需要$('#table')。datasorter()'。不知道datasorter是否想要一个特定的元素,或者将自己附加到'$('table')''返回的所有表中。 –

回答

0

三件事:

  1. 不要直接链接到tablesorter.com的tablesorter - 做一个拷贝到自己的服务器,或者在CDN使用复制(这是我在fork of tablesortercdnjs.com)。
  2. 在您的HTML的顶部添加<!DOCTYPE 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>";