ajax完美實(shí)現(xiàn)兩個(gè)網(wǎng)頁(yè) 分頁(yè)功能的實(shí)例代碼
分頁(yè)的首頁(yè)
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<h1 align="center">武俠小說(shuō)分頁(yè)</h>
<br/>
<script src="jquery-1.4.2.min.js"></script>
<script>
var page='';
function init(page){
document.getElementById("tables").innerHTML='';
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr =new ActiveXObject("Microsoft.XMLHTTP")
}
var url="fenye.php";
xhr.open("POST",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(page){
xhr.send("page="+page);
}
else {xhr.send("page=1");}
var content;
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
var json =eval('('+xhr.responseText+')');
//alert(xhr.responseText);
var fenye=json.str;
// alert(fenye);
document.getElementById('div').innerHTML=fenye;
content="<th>ID</th><th>名稱</th><th>作者</th><th>出版社</th><th>ISBN號(hào)</th><th>類型</th><th>價(jià)格</th>";
for(var i=0;i<json.info.length;i++){
content+="<tr><td>"+json.info[i].id+"</td><td>"+json.info[i].name+"</td><td>"+json.info[i].author+"</td><td>"+json.info[i].publisher+"</td><td>"+json.info[i].isbn+"</td><td>"+json.info[i].type+"</td><td>"+json.info[i].price+"</tr>";
document.getElementById("tables").innerHTML=content;
}
// alert(fenye);
}
}
}
}
</script>
<body onLoad="init()">
<h3 align="center">jquery實(shí)現(xiàn)$.ajax的分頁(yè)</h3>
<table id="tables" style=" width:500px; height:100px; text-align:center" align="center" border="5" bordercolor="#FF6600">
<th>ID</th><th>名稱</th><th>作者</th><th>出版社</th><th>ISBN號(hào)</th><th>類型</th><th>價(jià)格</th>
</table>
<table align="center" style="margin-top:15px">
<tr><td>
<td style="colspan:3;height:20">
<div id="div" style="position:absolute; left: 447px; top: 218px; width: 411px; height: 22px;"></div>
</td>
</tr>
</table>
<br />
</body>
分頁(yè)的php精華代碼
<?php
//命令模型層處理數(shù)據(jù)
$link=mysql_connect('localhost','root','') or die("失敗");
mysql_select_db('books',$link) or die("連接數(shù)據(jù)庫(kù)出錯(cuò)了!");
//每頁(yè)顯示記錄數(shù)
$pagesize = 2;
//求出總的記錄數(shù)
$sql = "select count(*) as total_rows from books";
$result = mysql_query($sql);
$total_rows = mysql_fetch_array($result);
//求總共的頁(yè)碼數(shù)
$pages = ceil($total_rows[0]/$pagesize);
//當(dāng)前第幾頁(yè)
$page = $_POST['page'];
$strtext = "當(dāng)前第".$page."頁(yè)"."總共".$pages."頁(yè)"."共".$total_rows[0]."記錄";
//var_dump($str);
//接下來(lái),我要根據(jù)當(dāng)前點(diǎn)擊的頁(yè)碼求出對(duì)應(yīng)的數(shù)據(jù)
$offset = $pagesize*($page-1);
$sql = "select * from books limit $offset,$pagesize";
mysql_query("set names utf8");
$res=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_assoc($res)){
$rows[]=$row;
}
$pageInfo = $rows;
//echo json_encode($pageInfo);
//var_dump($pageInfo);
//將獲得數(shù)據(jù)鏈接,后返回
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
//命令視圖層顯示數(shù)據(jù)
$first_a = "<a onclick='init(".$first.")' href='#'><img src='3.jpg' width='50px;' height='20px;'/></a>";
if($page>1){
$prev_a = "<a onclick='init(".$prev.")' href='#'><img src='1.jpg' width='50px;' height='20px;'/></a>";
}
if($page<$pages){
$next_a = "<a onclick='init(".$next.")' href='#'><img src='2.jpg' width='50px;' height='20px;'/></a>";
}
$last_a = "<a onclick='init(".$last.")' href='#'><img src='4.jpg' width='50px;' height='20px;'/></a>";
@$str = $strtext.$first_a.$prev_a.$next_a.$last_a;
//var_dump($str);
$info = array('str'=>$str,'info'=>$pageInfo);
echo json_encode($info);
相關(guān)文章
php設(shè)計(jì)模式之命令模式的應(yīng)用詳解
本篇文章是對(duì)php的命令模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
laravel使用Redis實(shí)現(xiàn)網(wǎng)站緩存讀取的方法詳解
這篇文章主要給大家介紹了關(guān)于laravel使用Redis實(shí)現(xiàn)網(wǎng)站緩存讀取的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
thinkPHP5框架整合plupload實(shí)現(xiàn)圖片批量上傳功能的方法
這篇文章主要介紹了thinkPHP5框架整合plupload實(shí)現(xiàn)圖片批量上傳功能的方法,結(jié)合實(shí)例形式分析了thinkPHP結(jié)合pluploadQueue實(shí)現(xiàn)上傳功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
thinkPHP5分頁(yè)功能實(shí)現(xiàn)方法分析
這篇文章主要介紹了thinkPHP5分頁(yè)功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了thinkPHP5實(shí)現(xiàn)分頁(yè)功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
php將服務(wù)端的文件讀出來(lái)顯示在web頁(yè)面實(shí)例
本篇文章主要介紹了php將服務(wù)端的文件讀出來(lái)顯示在web頁(yè)面實(shí)例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-10-10

