- 相關(guān)推薦
php+ajax實(shí)現(xiàn)無刷新數(shù)據(jù)分頁例子
無刷新功能我們用到很多很多的,下面我就來給各位介紹一個(gè)實(shí)例,就是實(shí)現(xiàn)php+ajax實(shí)現(xiàn)無刷新數(shù)據(jù)分頁了,例子非常的簡單大家只要按流程來操作就可以了哦.
index.php 文件代碼如下:
<?php
header("Content-type: text/html;charset=GBK");//輸出編碼,避免中文亂碼
?>
<html>
<head>
<title>ajax分頁演示</title>
<script language="javascript" src="ajaxpg.js"></script>
<link rel="stylesheet" type="text/css" href="page.css">
</head>
<body>
<div id="result">
<?php
$page=isset($_GET['page'])?intval($_GET['page']):1; //這句就是獲取page=18中的page的值,假如不存在page,那么頁數(shù)就是1。
$num=3; //每頁顯示10條數(shù)據(jù)
$db=mysql_connect("localhost","root","123456"); //創(chuàng)建數(shù)據(jù)庫連接
mysql_select_db("demo",$db) or die("數(shù)據(jù)庫鏈接錯(cuò)誤"); //選擇要操作的數(shù)據(jù)庫
mysql_query("set names gbk");
/*
首先咱們要獲取數(shù)據(jù)庫中到底有多少數(shù)據(jù),才能判斷具體要分多少頁,具體的公式就是
總數(shù)據(jù)庫除以每頁顯示的條數(shù),有余進(jìn)一。
也就是說10/3=3.3333=4 有余數(shù)就要進(jìn)一。
*/
$result=mysql_query("select * from brand");
$total=mysql_num_rows($result); //查詢所有的數(shù)據(jù)
$url='test.php';//設(shè)置ajax提交頁面地址的URL,這里設(shè)置成test.php通過ajax把參數(shù)傳遞給test.php再把處理過的內(nèi)容賦值到本頁的div id=result。
//頁碼計(jì)算
$pagenum=ceil($total/$num);//獲得總頁數(shù),也是最后一頁
$page=min($pagenum,$page);//獲得首頁
$prepg=$page-1;//上一頁
$nextpg=($page==$pagenum ? 0 : $page+1);//下一頁
$offset=($page-1)*$num; //獲取limit的第一個(gè)參數(shù)的值,假如第一頁則為(1-1)*10=0,第二頁為(2-1)*10=10。
$pagenav="<ul>";
//開始分頁導(dǎo)航條代碼:
$pagenav.="<li>顯示第 <B>".($total?($offset+1):0)."</B>-<B>".min($offset+10,$total)."</B> 條記錄</li><li>共 $total 條記錄 </li>";
//如果只有一頁則跳出函數(shù):
if($pagenum<=1) return false;
$pagenav.="<li> <a href=javascript:dopage('result','$url?page=1');>首頁</a></li> ";
if($prepg) $pagenav.="<li> <a href=javascript:dopage('result','$url?page=$prepg');>前頁</a></li> "; else $pagenav.=" <li>前頁</li> ";
if($nextpg) $pagenav.="<li><a href=javascript:dopage('result','$url?page=$nextpg');>后頁</a> </li>"; else $pagenav.=" <li>后頁</li> ";
$pagenav.="<li> <a href=javascript:dopage('result','$url?page=$pagenum');>尾頁</a></li> ";
$pagenav.="<li>第 $page 頁</li><li>共 $pagenum 頁</li></ul>";
//假如傳入的頁數(shù)參數(shù)大于總頁數(shù),則顯示錯(cuò)誤信息
If($page>$pagenum){
Echo "Error : Can Not Found The page ".$page;
Exit; //開源軟件:phpfensi.com
}
?></div><div id="results">
<?php
echo $pagenav;//輸出分頁導(dǎo)航
?>
</div>
</body>
</html>
css代碼:
/* CSS Document */
/* CSS Document */
#result ul li{
height:20px;
width:auto;
display:block;
color:#999;
border:1px solid #999;
float:left;
list-style:none;
font-size:12px;
margin-left:5px;
line-height:20px;
vertical-align:middle;
text-align:center;
}
#result ul li a:link{
width:50px;
height:20px;
display:block;
line-height:20px;
background:#09C;
border:1px solid #fff;
color:#fff;
text-decoration:none;
}
#result ul li a:hover{
width:50px;
height:20px;
display:block;
line-height:20px;
background:#09C;
border:1px solid #fff;
color:#F60;
text-decoration:none;
}
ajaxpg.js文件如下:
// JavaScript Document
var http_request=false;
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設(shè)置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)建對象實(shí)例失敗
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("results").style.display="none";
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//頁面不正常
alert("您所請求的頁面不正常!");
}
}
}
function dopage(obj,url){
document.getElementById(obj).innerHTML="<font color='green' font-size='12'>正在讀取數(shù)據(jù)...</font>";
send_request(url);
reobj=obj;
}
數(shù)據(jù)庫文件如下:
-- phpMyAdmin SQL Dump
-- version 2.8.1
-- http://www.phpmyadmin.net
--
-- 主機(jī): localhost
-- 生成日期: 2010 年 01 月 22 日 14:41
-- 服務(wù)器版本: 5.0.22
-- PHP 版本: 5.2.12
--
-- 數(shù)據(jù)庫: `demo`
--
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) `brand`
--
CREATE TABLE `brand` (
`id` int(7) NOT NULL auto_increment,
`sp_brand` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `brand`
--
INSERT INTO `brand` (`id`, `sp_brand`) VALUES (1, 'hello world'),
(2, '你好'),
(3, '恩'),
(4, 'fdsafdsafdsa'),
(5, 'fdsafdafdsafdas'),
(6, 'fdsafdsa'),
(7, 'fdsafdsafdas'),
(8, '恩'),
(9, '恩'),
(10, '恩'),
(11, '恩11'),
(12, '恩'),
(13, '恩'),
(14, '恩'),
(15, '恩'),
(16, '恩'),
(17, '恩'),
(18, '恩18');
下面介紹這些文件的功能
ajaxpg.js:ajax無刷新核心文件,一般不要去作修改.
index.php:實(shí)現(xiàn)ajax無刷新的文件了,這里調(diào)用了ajaxpg.js文件,配置了mysql用戶密碼,要和自己本地的一致,以及顯示分頁的效果.
page.css:這是分頁的CSS樣式文件,用來美化的,就不多介紹了.
brand.sql:這是MYSQL數(shù)據(jù)庫的文件了,進(jìn)行導(dǎo)入到MYSQL數(shù)據(jù)庫中,同樣,如果不會(huì)導(dǎo)入,可以參考PHPfensi.com中如何導(dǎo)入.sql文章即可.
【php+ajax實(shí)現(xiàn)無刷新數(shù)據(jù)分頁例子】相關(guān)文章:
php+ajax實(shí)現(xiàn)無刷新的新聞留言系統(tǒng)01-10
html無刷新分頁前端代碼03-14
如何實(shí)現(xiàn)bootstrap jquery dataTable異步ajax刷新表格數(shù)據(jù)02-01
基于PHP+Ajax實(shí)現(xiàn)表單驗(yàn)證的詳解05-19
一個(gè)PHP+MSSQL分頁的例子02-23
PHP如何使用curl實(shí)現(xiàn)數(shù)據(jù)抓取02-05
cakephp的分頁排序05-12