您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)利用ajax怎么實現(xiàn)一個網(wǎng)頁分頁功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
分頁的首頁
代碼如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<h2 align="center">武俠小說分頁</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號</th><th>類型</th><th>價格</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()">
<h4 align="center">jquery實現(xiàn)$.ajax的分頁</h4>
<table id="tables" align="center" border="5" bordercolor="#FF6600">
<th>ID</th><th>名稱</th><th>作者</th><th>出版社</th><th>ISBN號</th><th>類型</th><th>價格</th>
</table>
<table align="center" >
<tr><td>
<td >
<div id="div" ></div>
</td>
</tr>
</table>
<br />
</body>
分頁的php精華代碼
復(fù)制代碼 代碼如下:
<?php
//命令模型層處理數(shù)據(jù)
$link=mysql_connect('localhost','root','') or die("失敗");
mysql_select_db('books',$link) or die("連接數(shù)據(jù)庫出錯了!");
//每頁顯示記錄數(shù)
$pagesize = 2;
//求出總的記錄數(shù)
$sql = "select count(*) as total_rows from books";
$result = mysql_query($sql);
$total_rows = mysql_fetch_array($result);
//求總共的頁碼數(shù)
$pages = ceil($total_rows[0]/$pagesize);
//當(dāng)前第幾頁
$page = $_POST['page'];
$strtext = "當(dāng)前第".$page."頁"."總共".$pages."頁"."共".$total_rows[0]."記錄";
//var_dump($str);
//接下來,我要根據(jù)當(dāng)前點擊的頁碼求出對應(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)于利用ajax怎么實現(xiàn)一個網(wǎng)頁分頁功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。