Reputation: 341
I am trying to work with Tablesorter but it doesn't seem to work when I output the table using PHP.
My PHP code:
function displayHours() {
include 'dbinfo.php';
global $name;
$date = $_SESSION["selectedDate"];
$result = mysqli_query($con, "SELECT * FROM hours WHERE name='$name' AND date='$date'");
echo '<thead><tr>
<th><center>Project name</th>
<th><center>Hour(s)</th>
</tr></thead>';
$color = FALSE;
while($row = mysqli_fetch_array($result))
{
if (!$color) {
echo "<tbody><tr>";
$color = TRUE;
} else if ($color) {
echo "<tbody><tr class=\"alt\">";
$color = FALSE;
}
echo "<td> " . $row['projectName'] . "</td>";
echo "<td> " . $row['description'] . "</td>";
echo '</tr> ';
echo '</tbody>';
}
}
My html code:
<div class="datagrid">
<table id="myTable" class="tablesorter">
<?php displayHours();?>
</tbody>
</table>
</div>
My jQuery code:
$(document).ready(function () {
$("#myTable").tablesorter();
});
If I output two in one loop it at least sorts them, but once I only make it one it wont do anything.
I tried everything, any help please?
Upvotes: 0
Views: 321
Reputation: 2769
There is no problem with the tablesorter
, but your table html. You PHP code generates thead
and several tbody
tags. Normally there should be one tbody
tag.
To fix that in your PHP before your while loop in put tbody
opening and after your loop close the tag.
//open tbody tag
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
//print rows
}
//close tbody tag
echo "</tbody>";
There is also an unnecessary tbody
closing in your "html code", it's better to remove it.
<div class="datagrid">
<table id="myTable" class="tablesorter">
<?php displayHours();?>
</table>
</div>
Then your tablesorter would be working smoothly.
Upvotes: 1
Reputation: 90
I know while using tablesorter myself. I had to use
$(table).trigger("update")
to get a dynamically added table to update, however this wasn't with php.
Upvotes: 0