!DOCTYPE html>
html>
head>
title>分頁(yè)/title>
/head>
script type="text/javascript">
/**
* AJAX
* 1.創(chuàng)建ajax對(duì)象
* 2.建立連接
* 3.判斷ajax準(zhǔn)備狀態(tài)及狀態(tài)碼
* 4.發(fā)送請(qǐng)求
*/
function showList(pageNow){
//創(chuàng)建ajax對(duì)象
var xmlHttp = new XMLHttpRequest();
//建立連接
xmlHttp.open('get','./AJAX_Fenye.php?pageNow='+pageNow);
//判斷ajax準(zhǔn)備狀態(tài)及狀態(tài)碼
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 xmlHttp.status == 200) {
//alert(xmlHttp.readyState);
document.getElementById('result').innerHTML = xmlHttp.responseText;
}
}
//發(fā)送請(qǐng)求
xmlHttp.send(null);
}
//默認(rèn)顯示第1頁(yè)
window.onload = function(){
showList(1);
}
/script>
body>
input type="text">
div id="result">
!-- 此處顯示信息! -->
/div>
/body>
/html>
!DOCTYPE html>
html>
head>
meta charset="UTF-8">
title>AJAX分頁(yè)/title>
/head>
body>
?php
//以下php代碼可封裝調(diào)用,參數(shù)為pageNow
//鏈接數(shù)據(jù)庫(kù)
@mysql_connect('localhost','root','');
@mysql_select_db('empmanage');
@mysql_query('set name utf8');
//獲取總記錄數(shù)
$rs = mysql_query('select count(*) from emp');
$rows = mysql_fetch_row($rs);
$recordCount = $rows[0];
//每頁(yè)顯示多少條pageSize
$pageSize = 5;
//總頁(yè)數(shù) = 總記錄/每頁(yè)顯示多少
$pageCount=ceil($recordCount/$pageSize);
//獲取當(dāng)前頁(yè) 三元運(yùn)算 若不存在pageNow則默認(rèn)顯示第1頁(yè)
$pageNow = isset($_GET['pageNow'])? $_GET['pageNow']:1;
if ($pageNow 1) {
$pageNow = 1;
}elseif ($pageNow > $pageCount) {
$pageNow = $pageCount;
}
//起始位置 每頁(yè)從第幾條數(shù)據(jù)顯示
$pageStart = ($pageNow-1)*$pageSize;
//從哪條開始顯示,限制每頁(yè)顯示幾條
$sql = "select * from emp limit $pageStart,$pageSize";
//鏈接數(shù)據(jù)庫(kù)
$rs = mysql_query($sql);
//以上php代碼可封裝調(diào)用,參數(shù)為pageNow 返回查詢到的數(shù)據(jù)$rs
?>
table bordercolor="green" border="1">
tr>
th>ID/th>
th>姓名/th>
th>郵箱/th>
th>等級(jí)/th>
/tr>
?php
//循環(huán)取出數(shù)據(jù)
while ($rows = mysql_fetch_assoc($rs)) {
echo "
tr>
td>{$rows['id']}/td>
td>{$rows['name']}/td>
td>{$rows['email']}/td>
td>{$rows['level']}/td>
/tr>
";
}
?>
tr>
td colspan='4'>
?php
//分頁(yè)頁(yè)碼 調(diào)用js中的showList()方法 此處$i=$pageNow
for ($i=1; $i = $pageCount; $i++) {
echo "a href = 'javascript:void(0)' onclick = 'showList($i)'>{$i}/a> nbsp;";
}
?>
/td>
/tr>
/table>
/body>
/html>