麻烦使用tablesorter jquery插件与php生成的表

时间:2014-07-07 19:26:50

标签: php jquery-plugins tablesorter

我要求用户填写表单,我想以表格格式显示该信息。然后,我希望用户能够从每列的标题行对表进行排序。我试图使用jquery tablesorter插件,但似乎无法让它工作。插件是否不适用于PHP生成的表?

<!DOCTYPE HTML>
<html>
<head>
<title>Dashboard</title>
<link href ="table.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-latest.js"></script> 
<script type="text/javascript" src="jquery.tablesorter.js"></script> 

<script type = "text/javascript">
$(document).ready(function() {
     $("#sortedtable").tablesorter();
});
</script>


</head>
<body>
<?php
unset($_SESSION['errors_record']);
define("WEB_DB", "server_db");
define("DB_USER", "root");              
define("DB_PASS", "prog");      
$db_host = "localhost";
MYSQL_CONNECT($db_host,DB_USER,DB_PASS);
mysql_select_db(WEB_DB);

?>

<p><h1>SRG TDE Technical Review Dashboard</h1></p>
<p>
<a href = "http://localhost/record_form.php"><button>Create a New Review Record</button></a>
<a href="login.php" style = "float:right;">Logout</a>
</p>
<div class="CSSTableGenerator">
<table id = "sortedtable" class = "tablesorter">
<thead>
                        <tr>
                            <th>Review Record ID</th>
                            <th>Project</th>
                            <th>Date</th>
                            <th>Author</th>
                            <th>Moderator</th>
                            <th>Portfolio Lead</th>
                            <th>Review Artifact Type</th>
                            <th>Review Artifact Name</th>
                            <th>Version</th>
                        </tr>

    $sql = "select record_id,project,date,author,moderator,portlead,rtype,rname,version from dashboard_table ORDER BY date DESC";
    $result = mysql_query($sql);
    $num = mysql_num_rows($result);


    if ($num)
    {

        while(list($record_id,$project,$date,$author,$moderator,$portlead,$rtype,$rname,$version) = mysql_fetch_row($result))
        {
?>
<?php $url="http://localhost/main_tab.php?record=" . $record_id ?>
<tbody>
        <tr>
        <td><?php echo "<a href = '$url'>$record_id</a>";?></td>
        <td><?php echo $project?></td>
        <td><?php echo $date?></td>
        <td><?php echo $author?></td>
        <td><?php echo $moderator?></td>
        <td><?php echo $portlead?></td>
        <td><?php echo $rtype?></td>
        <td><?php echo $rname?></td>
        <td><?php echo $version?></td>
        </tr>

        <?php



        }
    }

?>
</tbody>
</table>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

我对PHP并不是那么好,但似乎正在构建的表将每行包裹在一个新的tbody中。将初始<tbody>移到while循环之外:

    if ($num)
    {

        echo "<tbody>";  
        while(list($record_id,$project,$date,$author,$moderator,$portlead,$rtype,$rname,$version) = mysql_fetch_row($result))
        {
?>
<?php $url="http://localhost/main_tab.php?record=" . $record_id ?>
        <tr>
        <td><?php echo "<a href = '$url'>$record_id</a>";?></td>
        <td><?php echo $project?></td>
        <td><?php echo $date?></td>
        <td><?php echo $author?></td>
        <td><?php echo $moderator?></td>
        <td><?php echo $portlead?></td>
        <td><?php echo $rtype?></td>
        <td><?php echo $rname?></td>
        <td><?php echo $version?></td>
        </tr>

        <?php

        }
    }

?>
</tbody>