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?
tbodyto all of your rows, this could be the problem. Instead of adding every row you should open the tag before loop and close it after loop. Besides you have closed tbody where you run displayHours fonction.