php+ajax实现无刷新分页
这篇文章主要介绍了php+ajax实现无刷新分页的方法,详细讲述了Ajax文件的实现及PHP调用方法,需要的朋友可以参考下
本文实例讲述了php+ajax实现无刷新分页实现方法。分享给大家供大家参考。具体如下:
limit偏移量,长度;limit0,7;第一页limit7,7;第二页limit14,7;第三页每页信息条数:7信息总条数:selectcount()fromtable信息总页数:ceil向上取整(总条数/每页条数)1、分页类具体使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 classPagination{
private$total;//数据表中总记录数
private$listRows;//每页显示行数
private$limit;//mysql数据库的limit
private$uri;//分页信息前面的uri地址
private$pageNum;//页数
private$config=array(''header''=>"个记录","prev"=>"【上一页】","next"=>"【下一页】","first"=>"【首页】","last"=>"【尾页】");
private$listNum=8;
/
$total当前信息总条数
$listRows每页显示的条数
$pa下面的page
http://网址/index.php?page=5
/
publicfunction__construct($total,$listRows=10,$pa=""){
$this->total=$total;
$this->listRows=$listRows;
$this->uri=$this->getUri($pa);
$this->page=!empty($_GET["page"])?$_GET["page"]:1;//不传入page,则默认显示首页
$this->pageNum=ceil($this->total/$this->listRows);
$this->limit=$this->setLimit();
}
//设置每页显示的条数
privatefunctionsetLimit(){
return"Limit".($this->page-1)$this->listRows.",{$this->listRows}";
}
//获得URL地址
privatefunctiongetUri($pa){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],''?'')?'''':"?").$pa;
$parse=parse_url($url);
if(isset($parse["query"])){
parse_str($parse[''query''],$params);
unset($params["page"]);
$url=$parse[''path''].''?''.http_build_query($params);
}
return$url;
}
//魔术方法,
publicfunction__get($args){
if($args=="limit")
return$this->limit;
else
returnnull;
}
//页面开始的条数
privatefunctionstart(){
if($this->total==0)
return0;
else
return($this->page-1)$this->listRows+1;
}
//页面结束的条数
privatefunctionend(){
returnmin($this->page$this->listRows,$this->total);
}
/设置首页/
privatefunctionfirst(){
$html="";
if($this->page==1)
$html.=''''.$this->config["first"].'''';
else
$html.="uri}&page=1\")''>{$this->config["first"]}";
//$html.="uri}&page=1''>{$this->config["first"]}";
return$html;
}
/设置上一页/
privatefunctionprev(){
$html="";
if($this->page==1)
$html.=''''.$this->config["prev"].'''';
else
$html.="uri}&page=".($this->page-1)."\")''>{$this->config["prev"]}";
//$html.="uri}&page=".($this->page-1)."''>{$this->config["prev"]}";
return$html;
}
//页码列表【首页】【2】【3】…………【尾页】
privatefunctionpageList(){
$linkPage="";
$inum=floor($this->listNum/2);
for($i=$inum;$i>=1;$i--){
$page=$this->page-$i;
if($page<1)
continue;
$linkPage.="uri}&page={$page}\")''>{$page}";
}
$linkPage.="{$this->page}";
for($i=1;$i<=$inum;$i++){
$page=$this->page+$i;
if($page<=$this->pageNum)
$linkPage.="uri}&page={$page}\")''>{$page}";
else
break;
}
return$linkPage;
}
/设置下一页/
privatefunctionnext(){
$html="";
if($this->page==$this->pageNum)
$html.=''''.$this->config["next"].'''';
else
$html.="uri}&page=".($this->page+1)."\")''>{$this->config["next"]}";
//$html.="uri}&page=".($this->page+1)."''>{$this->config["next"]}";
return$html;
}
/设置尾页/
privatefunctionlast(){
$html="";
if($this->page==$this->pageNum)
$html.=''''.$this->config["last"].'''';
else
$html.="uri}&page=".($this->pageNum)."\")''>{$this->config["last"]}";
//$html.="uri}&page=.(this->pageNum).''>{$this->config["last"]}";
return$html;
}
/设置页面跳转/
privatefunctiongoPage(){
return
''''.$this->pageNum.'')?''.$this->pageNum.'':this.value;showPage(\''''.$this->uri.''&page=\''+page+\''\'')}"value="''.$this->page.www.sm136.com''"style="width:25px">
''.$this->pageNum.'')?''.$this->pageNum.'':this.previousSibling.value;showPage(\''''.$this->uri.''&page=\''+page+\''\'')">'';
}
//页面列表配置选项
functionfpage($display=array(0,1,2,3,4,5,6,7,8)){
$html[0]="共有{$this->total}{$this->config["header"]}";
$html[1]="每页显示".($this->end()-$this->start()+1)."条,本页{$this->start()}-{$this->end()}条";
$html[2]="{$this->page}/{$this->pageNum}页";
$html[3]=$this->first();
$html[4]=$this->prev();
$html[5]=$this->pageList();
$html[6]=$this->next();
$html[7]=$this->last();
$html[8]=$this->goPage();
$fpage='''';
foreach($displayas$index){
$fpage.=$html[$index];
}
return$fpage;
}
} 2数据显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 //链接数据库
//获得具体信息
//分页显示
header("content-type:text/html;charset=utf-8");
$link=mysql_connect(''localhost'',''root'',''111111'');
mysql_select_db(''shop'',$link);
mysql_query("setnamesutf8");
$css=<<
table{border:1pxsolidblack;width:700px;margin:auto;border-collapse:collapse;}
td{border:1pxsolidblack;}
eof;
echo$css;
echo"
序号 | 名称 | 数量 | 价格 | 时间 |
";
//1引入分页类
include"./Pagination.php";
//2.获得信息总条数
$sql="selectfromsw_goods";
$qry=mysql_query($sql);
$total=mysql_num_rows($qry);
$per=7;
//3.实例化分页类对象
$page_obj=newPagination($total,$per);
//4.拼装sql语句,获得每页信息
//利用page_obj实现limit的灵活设置
//$page_obj->limit;
$sqla="selectfromsw_goods".$page_obj->limit;
$qrya=mysql_query($sqla);
//5.获得页面列表
$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
$i=1;
while($rsta=mysql_fetch_assoc($qrya)){
echo"";
echo"".$i++." | ";
echo"".$rsta[''goods_name'']." | ";
echo"".$rsta[''goods_number'']." | ";
echo"".$rsta[''goods_price'']." | ";
echo"".date("Y-m-dH:i:s",$rsta[''goods_create_time''])." | ";
echo" ";
}
echo"".$pagelist." ";
echo" "; 3ajax无刷新分页实现
open(‘get'',''www.visa158.com网址/index.php?page=2'')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
新建网页
//获得分页信息ajax函数
functionshowPage(myurl){
varxhr=newXMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
varrst=document.getElementById("result");
rst.innerHTML=xhr.responseText;
}
}
xhr.open("get",myurl);
xhr.send(null);
}
window.onload=function(){
showPage("./data1.php");//获得分页信息
//showPage("./data.php?page=2");
}
ajax无刷新分页效果
document.write(newDate()+" ");
document.write(newDate()+" ");
document.write(newDate()+" ");
document.write(newDate()+" ");
|
|