Search code examples
javascriptphpjquerytablesorter

Tablesorter will not display sortable headers


I've been playing with this code for the past 2 hours. I simply cannot find the error. With the code below, the table header and body display, but the headers are not sortable. I've tried various incarnations of the script code, but to no avail. What am I missing?

    <!DOCTYPE html>
    <head>
    <script type="text/javascript" src="./jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="./Mottie-tablesorter-c1ce076/js/jquery.tablesorter.js"></script>
    <script type="text/javascript">
    $(document).ready(function()
        {
            $("table").tablesorter();
        }
    );
    </script>
    </head>
    <?php
    /* Attempt MySQL server connection. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    $link = mysqli_connect("localhost", "SECRET", "SECRET", "SECRET");

    // Check connection
    if($link === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Attempt select query execution
    $sql = "SELECT * FROM persons";
    if($result = mysqli_query($link, $sql)){
        if(mysqli_num_rows($result) > 0){
            echo "<table class=\"tablesorter\">";
            echo "<thead>";
             echo "<tr>";
             echo "<th>First Name</th>";
             echo "<th>Last Name</th>";
             echo "<th>Email</th>";
             echo "<th>FFID</th>";
             echo "<th>Street</th>";
             echo "<th>City</th>";
             echo "<th>State</th>";
             echo "<th>Zip</th>";
             echo "<th>Home Fire Dept</th>";
             echo "<th>Shirt Size</th>";
             echo "<th>Additional Shirts</th>";
             echo "<th>Friday Class</th>";
             echo "<th>Saturday Class</th>";
             echo "<th>Sunday Class</th>";
             echo "</tr>";
            echo "</thead>";
            while($row = mysqli_fetch_array($result)){
                echo "<tbody><tr>";
                    echo "<td>" . $row['first_name'] . "</td>";
                    echo "<td>" . $row['last_name'] . "</td>";
                    echo "<td>" . $row['email_address'] . "</td>";
                    echo "<td>" . $row['ffid_num'] . "</td>";
                    echo "<td>" . $row['address_street'] . "</td>";
                    echo "<td>" . $row['address_city'] . "</td>";
                    echo "<td>" . $row['address_state'] . "</td>";
                    echo "<td>" . $row['address_zip'] . "</td>";
                    echo "<td>" . $row['fire_dept'] . "</td>";
                    echo "<td>" . $row['wants_tshirt'] . "</td>";
                    echo "<td>" . $row['shirt_size'] . "</td>";
                    echo "<td>" . $row['additional_shirts'] . "</td>";
                    echo "<td>" . $row['friday_class'] . "</td>";
                    echo "<td>" . $row['saturday_class'] . "</td>";
                    echo "<td>" . $row['sunday_class'] . "</td>";
                echo "</tr></tbody>";
            }
            echo "</table>";

            // Close result set
            mysqli_free_result($result);
        } else{
            echo "No records matching your query were found.";
        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    // Close connection
    mysqli_close($link);
    ?>
    </html>

Solution

  • Just add and outside the while loop.

     echo "</thead><tbody>";
                while($row = mysqli_fetch_array($result)){
                    echo "<tr>";
                        echo "<td>" . $row['first_name'] . "</td>";
                        echo "<td>" . $row['last_name'] . "</td>";
                        echo "<td>" . $row['email_address'] . "</td>";
                        echo "<td>" . $row['ffid_num'] . "</td>";
                        echo "<td>" . $row['address_street'] . "</td>";
                        echo "<td>" . $row['address_city'] . "</td>";
                        echo "<td>" . $row['address_state'] . "</td>";
                        echo "<td>" . $row['address_zip'] . "</td>";
                        echo "<td>" . $row['fire_dept'] . "</td>";
                        echo "<td>" . $row['wants_tshirt'] . "</td>";
                        echo "<td>" . $row['shirt_size'] . "</td>";
                        echo "<td>" . $row['additional_shirts'] . "</td>";
                        echo "<td>" . $row['friday_class'] . "</td>";
                        echo "<td>" . $row['saturday_class'] . "</td>";
                        echo "<td>" . $row['sunday_class'] . "</td>";
                    echo "</tr>";
                }
                echo "</tbody></table>";