【问题标题】:TableSorter + AJAX to get data from MySQLTableSorter + AJAX 从 MySQL 获取数据
【发布时间】:2014-03-11 13:18:09
【问题描述】:
【问题讨论】:
标签:
jquery
mysql
ajax
twitter-bootstrap
tablesorter
【解决方案1】:
您必须创建自己的导航分页并从数据库中逐页加载数据,使用 SQL 查询 LIMIT 和 OFFSET 进行控制,并使用 AJAX 按需加载。
//This is PHP code:
$totalRecord = ??? //get this using SELECT COUNT
$totalRecordPerPage = 50; //this is up to you!!!
//get total pages
$totalPage = floor($totalRecord / $totalRecordPerPage); //floor;
$modResult = $totalRecord % $totalRecordPerPage;
if ($modResult <> 0)
$totalPage++;
$pageNo = $_GET["pageno"]; //FROM AJAX GET
//set the SQL limit and offset
$limit = $totalRecordPerPage;
$offset = (($pageNo - 1) * $totalRecordPerPage);
$sql = "SELECT bla bla bla
FROM bla1, bla2
WHERE bla bla bla
ORDER BY bla blabla ASC
limit $limit
offset $offset";
$sqlResult = mysql_query($sql, $con); //this is only an example, use PDO for security
//create JSON from result
//use that JSON to update tablesorter
//JAVASCRIPT AJAX
//tblHTML is a tr and td that contain JSON data from the AJAX result
$("#tblbody").html(tblHTML);
$(".tablesorter").trigger("update");
永远不要一次加载所有数据