问题
Trying to add tablesorter added to a page I am creating. I know very little of jquery, so I'm guessing that's where my fault is. I've added the required code in the <head> area of my page, and made the necessary changes to my table. My table still renders as it would with just HTML. Ideas?
<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>
Thanks!
回答1:
Three things:
- Don't link directly to tablesorter at tablesorter.com - make a copy to your own server, or use a copy at a CDN (this is of my fork of tablesorter at cdnjs.com).
- Include a
<!DOCTYPE html>at the top of your HTML otherwise IE will change into quirks mode and pretty much make your site look bad. As @MikeB mentioned, the above code wraps every row in a
tbody, correct the code as follows (this is just a snippet):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>";
来源:https://stackoverflow.com/questions/20747897/php-mysql-html-table-with-tablesorter