2013-12-23 91 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>";