php+ajax實現(xiàn)無刷新分頁的方法
本文實例講述了php+ajax實現(xiàn)無刷新分頁的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
這是一款基于原生態(tài)的php +js +ajax 的分頁程序?qū)嵗?,我們詳細的從?shù)據(jù)庫創(chuàng)建到js,php,html頁面的創(chuàng)建來告訴你如何實現(xiàn)ajax分頁調(diào)用數(shù)據(jù)的方法。
具體步驟如下:
一、創(chuàng)建數(shù)據(jù)庫
SQL語句如下:
`id` int(10) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
INSERT INTO `tb_user` VALUES (1, 'aaa');
INSERT INTO `tb_user` VALUES (2, 'bbb');
INSERT INTO `tb_user` VALUES (3, 'ccc');
INSERT INTO `tb_user` VALUES (4, 'ddd');
INSERT INTO `tb_user` VALUES (5, 'eee');
INSERT INTO `tb_user` VALUES (6, 'fff');
INSERT INTO `tb_user` VALUES (7, 'ggg');
INSERT INTO `tb_user` VALUES (8, 'hhh');
INSERT INTO `tb_user` VALUES (9, '����');
二、ajaxpage.js文件代碼如下:
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設置MIME類別
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE瀏覽器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//異常,創(chuàng)建對象實例失敗
window.alert("創(chuàng)建XMLHttp對象失??!");
return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請求方式,URL,及是否同步執(zhí)行下段代碼
http_request.open("GET",url,true);
http_request.send(null);
}
//處理返回信息的函數(shù)
function processrequest(){
if(http_request.readyState==4){//判斷對象狀態(tài)
if(http_request.status==200){//信息已成功返回,開始處理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//頁面不正常
alert("您所請求的頁面不正常!");
}
}
}
function dopage(obj,url){
document.getElementById(obj).innerHTML="正在讀取數(shù)據(jù)...";
reobj = obj;
send_request(url);
}
三、php調(diào)用代碼如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="javascript" src="ajaxpage.js"></script>
<div id="result">
<?php
$terry=mysql_connect("localhost","root","")or die("連接數(shù)據(jù)庫失敗:".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("set NAMES 'utf8'");
$result=mysql_query("select * from tb_user");
$total=mysql_num_rows($result) or die(mysql_error());
$page=isset($_GET['page'])?intval($_GET['page']):1;
$page_size=3;
$url='index.php';
$pagenum=ceil($total/$page_size);
$page=min($pagenum,$page);
$prepage=$page-1;
$nextpage=($page==$pagenum?0:$page+1);
$pageset=($page-1)*$page_size;
$pagenav='';
$pagenav.="顯示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>記錄 共<b><font color='yellow'>".$total."</font></b>條記錄 現(xiàn)在是第 <b><font color='blue'>".$page."</font></b> 頁 ";
if($page<=1)
$pagenav.="<a style=cursor:not-allowed;>首頁</a> ";
else
$pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首頁</a> ";
if($prepage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一頁</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>上一頁</a> ";
if($nextpage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一頁</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>下一頁</a> ";
if($pagenum)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾頁</a> ";
else
$pagenav.="<a style=cursor:not-allowed;>尾頁</a> ";
$pagenav.="共".$pagenum."頁";
if($page>$pagenum){
echo "error:沒有此頁".$page;
exit();
}
?>
<table align="center" border="2" width="300">
<tr bgcolor="#cccccc" align="center">
<td>用戶名</td>
<td>用戶密碼</td>
</tr>
<?php
$info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
<tr align="center">
<td><?php echo $array['id'];?></td>
<td><?php echo $array['username'];?></td>
</tr>
<?php
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>";
?>
</div>
希望本文所述對大家的PHP程序設計有所幫助。
相關文章
php讀取csv文件后,uft8 bom導致在頁面上顯示出現(xiàn)問題的解決方法
以下是對php讀取csv文件后,uft8 bom導致在頁面上顯示出現(xiàn)問題的解決方法進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08PHP4 與 MySQL 數(shù)據(jù)庫操作函數(shù)詳解
PHP4 與 MySQL 數(shù)據(jù)庫操作函數(shù)詳解...2006-12-12