javascript - Tablesorter 将不会显示可排序的标题

标签 javascript php jquery tablesorter

过去 2 个小时我一直在研究这段代码。我根本找不到错误。使用下面的代码,表头和正文显示,但标题不可排序。我尝试了脚本代码的各种变体,但无济于事。我错过了什么?

    <!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>

最佳答案

只需在 while 循环之外添加 和 即可。

 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>";

关于javascript - Tablesorter 将不会显示可排序的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278155/

相关文章:

javascript - 如何在单击按钮并按 Enter 键时启动 jquery 函数

php - php脚本是如何执行的

php - 如何获取表2中没有的值

javascript - YQL中可以添加UserAgent吗?

javascript - 使用 CSS 和 jQuery 构建金字塔

php - 将 Session 变量传递给称为 PHP 脚本的 ajax

jQuery.addClass 不工作

javascript - 在 JavaScript 中导入二进制日志文件

javascript - 发布到后端后如何获取数据?

javascript - 在 jQuery 中使用按钮或 anchor 重定向到另一个页面