0

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?

4
  • 5
    you are adding tbody to 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. Commented Dec 5, 2013 at 13:55
  • Wow.. it worked actually. I totally forgot that it would give a tdbody to every row... Thanks a lot!!! Saved my day. Commented Dec 5, 2013 at 14:04
  • 2
    Please tell me this is never ever going to be production facing code? The PHP code is riddled with injection attack vectors :| Commented Dec 5, 2013 at 14:35
  • @scriptmonster add your comment as an answer, and I'll upvote it! Commented Mar 8, 2014 at 19:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.