做了一个列表分页的功能模块,主要的文件包括分页类 page.class.php 和 控制 ajax 分页的ajax.js,主要功能有:
1.可以选择 3 种常见的 url 分页格式;
2.可以选择 url 分页 还是 ajax 分页;
3.两种分页方式都可以自定义分页 a 标签的文字;
4.url 分页方式可以自定义分页偏移量;
5.url 分页方式可以选择手动跳转方式:手动输入页码跳转 或 下拉菜单选择页码跳转。
列表分页功能含有但不一定全部包含的元素包括:
首页、下一页、上一页、末页、具体页码、手动输入的跳转至第几页、下拉菜单选择跳转至第几页、信息( 共多少页、共多少条、当前是第几页 )等。
其中必须包含的元素有:上一页、下一页、具体页码。
先看看其他网站是怎么做的( 百度搜索、虎扑、淘宝、虾米、织梦官网 ):
1.百度搜索就是由最简单的"上一页"、"下一页"和具体页码构成。分页偏移量为前5页后4页



2.虎扑话题( upload/2014/12/201412081515104831.jpg" alt="分页样式" >



3.淘宝网宝贝列表页( upload/2014/12/201412081515117745.jpg" alt="分页样式" >


4.虾米列表( upload/2014/12/201412081515132476.jpg" alt="分页样式" >


最后是织梦官网文章列表页( upload/2014/12/201412081515148711.jpg" alt="分页样式" >
浏览至第11页时非常遗憾,宽度过宽导致版式出现问题:


这个分页功能的做法和效果是:
1.url 分页 style1:
①手动输入跳转页码的方式:
始终显示最后一页



②下拉菜单选择跳转的方式:

2.url 分页 style2:
使用"首页"和"末页"代替页码"1"和最后一页页码,使用前n页、后n页代替"..."


同样有下拉菜单跳转方式

3.ajax 分页:
出现的元素只有"首页"、"上一页"、"下一页"和"末页"。
首页时:

中间时:

末页时:

模块的文件结构图:
ROOT:
├─conn
│ └─conn.php
│
├─libs -- smarty库
│
├─templates
│ │
│ ├─demo.html -- 功能页模板文件
│ │
│ ├─css
│ │ ├─common.css
│ │ └─style1.css
│ │
│ ├─images
│ │ └─loading.gif -- ajax分页时请求数据接收到之前的加载图
│ └─js
│ ├─jquery-1.8.3.min.js
│ └─ajax.js -- 当分页方式为ajax时模板demo.html加载的js
│
├─templates_c
│
├─init.inc.php -- smarty配置文件
│
├─page.class.php -- 分页类
│
├─demo.php
│
└─ajaxpage.php -- ajax分页时接受请求的php文件
要注意的地方:
1.偏移量的显示设置,主要是什么时候 url 分页方式1,什么时候显示"..." :当前页码 - 前偏移量 - 1 > 1 时,应当显示前面的"..."; 当前页码 + 后偏移量 + 1 < 总页数时,应当显示后面的"...";
2.选择性加载 js :当使用 ajax 方式进行分页时,才加载 ajax.js
3.外部的 js 无法解析 smarty 的标签,所以在使用外部 js 时的传值要进行处理
4.ajax 分页时,默认是第一页,也就是一定首先会出现 "下一页" 和 "末页",所以 "上一页" 和 "首页" 的添加和点击函数应当包含在"下一页" 和 "末页" 的点击函数中。
主要代码:
page.class.php:
015 | private $firstFonts = "首页" ; |
016 | private $lastFonts = "末页" ; |
017 | private $nextFonts = "下一页 >" ; |
018 | private $preFonts = "< 上一页" ; |
022 | private $pn_fonts = "前10页" ; |
024 | private $fn_fonts = "后10页" ; |
028 | function __construct( $totalNum , $perpageNum , $prePage , $preFonts , $floPage , $nextFonts , $p , $skipStyle , $pageStyle , $page_n , $page_act ){ |
029 | $this ->totalNum = $totalNum ; |
030 | $this ->perpageNum = $perpageNum ; |
031 | $this ->prePage = $prePage ; |
032 | $this ->floPage = $floPage ; |
033 | $this ->skipStyle = $skipStyle ; |
034 | $this ->pageStyle = $pageStyle ; |
035 | $this ->page_n = $page_n ; |
036 | $this ->page_act = $page_act ; |
037 | $this ->getPageNow( $p ); |
038 | $this ->totalPage = $this ->getTotalPage(); |
039 | $this ->pageShow = "" ; |
043 | public function __toString(){ |
044 | return $this ->pageShow; |
047 | public function getPageNow( $p ){ |
053 | die ( "page number error" ); |
055 | return $this ->pageNow; |
058 | public function getUrl(){ |
059 | $url = "http://" . $_SERVER [ 'HTTP_HOST' ]. $_SERVER [ 'REQUEST_URI' ]; |
061 | if ( strpos ( $url , "?" ) === false){ |
062 | return $this ->url = $url . "?" ; |
064 | $url = explode ( "?" , $url ); |
068 | if ( strpos ( $param , "&" ) === false){ |
070 | if ( strpos ( $param , "p=" ) === false){ |
072 | $url = implode( "?" , $url ); |
073 | return $this ->url = $url . "&" ; |
077 | return $this ->url = $url . "?" ; |
080 | $param = explode ( "&" , $param ); |
082 | foreach ( $param as $k => $v ){ |
083 | if ( strpos ( $v , "p=" ) === false){ |
091 | $param = implode( "&" , $param ); |
093 | $url = implode( "?" , $url ); |
094 | return $this ->url = $url . "&" ; |
099 | public function preOffset( $preFonts ){ |
101 | if ( $this ->pageNow!=1 && ( $this ->pageNow - $this ->prePage -1 <= 1)){ |
103 | $this ->pageShow .= "<a id=\"per_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow-1). "\">" .( $preFonts == "" ? $this ->preFonts: $preFonts ). "</a>" ; |
105 | for ( $i =1; $i <= $this ->pageNow-1; $i ++){ |
107 | if ( $this ->page_act != 1){ |
108 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "p=" . $i . "\">" . $i . "</a>" ; |
111 | } else if ( $this ->pageNow - $this ->prePage -1 > 1){ |
113 | if ( $this ->pageStyle == 2 || $this ->page_act == 1){ |
115 | $this ->pageShow .= "<a id=\"first_page\" class=\"pagenum\" href=\"" . $this ->url. "p=1\">" . $this ->firstFonts. "</a>" ; |
117 | if ( $this ->page_n == 1 && $this ->page_act != 1){ |
118 | if ( $this ->pageNow> $this ->pn){ |
119 | $this ->pageShow .= "<a id=\"pre_n_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow- $this ->pn). "\">" . $this ->pn_fonts. "</a>" ; |
124 | $this ->pageShow .= "<a id=\"pre_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow-1). "\">" .( $preFonts == "" ? $this ->preFonts: $preFonts ). "</a>" ; |
126 | if ( $this ->pageStyle == 1){ |
127 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "\">1</a><a id=\"pre_page_2\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow- $this ->prePage-1). " \" title=\"第" .( $this ->pageNow- $this ->prePage-1). "页\">…</a>" ; |
129 | for ( $i = $this ->prePage; $i >=1; $i --){ |
131 | if ( $this ->page_act != 1){ |
132 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow- $i ). "\">" .( $this ->pageNow- $i ). "</a>" ; |
138 | public function floOffset( $nextFonts ){ |
139 | if ( $this ->totalPage > $this ->floPage){ |
140 | for ( $i =0; $i <= $this ->floPage; $i ++){ |
141 | $page = $this ->pageNow+ $i ; |
142 | if ( $page <= $this ->totalPage){ |
144 | if ( $this ->page_act != 1){ |
145 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "p=" . $page . "\">" . $page . "</a>" ; |
149 | if ( $this ->pageNow < $this ->totalPage){ |
151 | if (( $this ->pageNow+ $this ->floPage+1)< $this ->totalPage){ |
153 | if ( $this ->pageStyle == 1){ |
154 | $this ->pageShow .= "<a id=\"flo_page_2\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $page +1). "\" title=\"第" .( $page +1). "页\">…</a>" ; |
158 | if (( $this ->pageNow+ $this ->floPage+1)<= $this ->totalPage){ |
161 | if ( $this ->pageStyle == 1){ |
162 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "p=" . $this ->totalPage. "\" title=\"总共" . $this ->totalPage. "页\">" . $this ->totalPage. "</a>" ; |
165 | $this ->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow+1). "\">" .( $nextFonts == "" ? $this ->nextFonts: $nextFonts ). "</a>" ; |
167 | if ( $this ->pageStyle == 2 && $this ->page_n == 1 && $this ->page_act != 1){ |
168 | if (( $this ->pageNow+10)< $this ->totalPage){ |
169 | $this ->pageShow .= "<a id=\"flo_n_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" .( $this ->pageNow+ $this ->fn). "\">" . $this ->fn_fonts. "</a>" ; |
173 | if ( $this ->pageStyle == 2){ |
174 | if (( $this ->pageNow+ $this ->floPage+1)< $this ->totalPage){ |
175 | $this ->pageShow .= "<a id=\"last_page\" class=\"pagenum\" href=\"" . $this ->url. "p=" . $this ->totalPage. "\">末页</a>" ; |
178 | } else if ( $this ->pageNow > $this ->totalPage){ |
182 | for ( $i =0; $i < $this ->totalPage; $i ++){ |
183 | $page = $this ->pageNow+ $i ; |
185 | $this ->pageShow .= "<a class=\"pagenum\" href=\"" . $this ->url. "p=" . $page . "\">" . $page . "</a>" ; |
190 | public function getOtherInfo(){ |
192 | if ( $this ->page_act != 1){ |
193 | $this ->pageShow .= " 跳转至 " ; |
195 | if ( $this ->skipStyle == "" ){ |
196 | $this ->pageShow .= "" ; |
197 | } else if ( $this ->skipStyle == 1){ |
198 | $this ->pageShow .= "<input id=\"skip\" type=\"text\" value=\"" . $this ->pageNow. "\">" ; |
199 | $this ->pageShow .= "<button id=\"go\">GO</button>" ; |
200 | } else if ( $this ->skipStyle == 2){ |
202 | $this ->pageShow .= "<select id=\"select_page\" onchange=\"location.href=this.options[this.selectedIndex].value;\" >" ; |
203 | for ( $i =1; $i <= $this ->totalPage; $i ++){ |
204 | $this ->pageShow .= "<option value=\"" . $this ->url. "p=" . $i . "\"" ; |
206 | if ( $i == $this ->pageNow){ |
207 | $this ->pageShow .= " selected" ; |
209 | $this ->pageShow .= ">" . $i . "</option>" ; |
211 | $this ->pageShow .= "</select>" ; |
214 | $this ->pageShow .= "<span id=\"pagenow_info\"> 当前第" . $this ->pageNow. "页</span>" ; |
215 | $this ->pageShow .= "/<span id=\"totalpage_info\">共" . $this ->totalPage. "页</span>" ; |
216 | $this ->pageShow .= " <span id=\"totalNum_info\">共" . $this ->totalNum. "条</span>" ; |
219 | public function getFirstRow(){ |
220 | $firstRow = $this ->perpageNum * ( $this ->pageNow-1) + 1; |
224 | public function getTotalPage(){ |
225 | $totalPage = ceil ( $this ->totalNum / $this ->perpageNum); |
229 | public function getPreFonts( $preFonts ){ |
230 | return ( $preFonts == "" )? $this ->preFonts: $preFonts ; |
232 | public function getNextFonts( $nextFonts ){ |
233 | return ( $nextFonts == "" )? $this ->nextFonts: $nextFonts ; |
demo.php:
02 | require 'init.inc.php' ; |
03 | require 'page.class.php' ; |
04 | require 'conn/conn.php' ; |
18 | $p = isset( $_GET [ 'p' ])? $_GET [ 'p' ]:1; |
23 | $sql_all = "select title from ips_archives" ; |
25 | $totalNum = $conne ->getRowsNum( $sql_all ); |
27 | $mypageurl = new MyPageUrl( $totalNum , $perpageNum , $perPage , $preFonts , $floPage , $nextFonts , $p , $skipStyle , $pageStyle , $page_n , $page_act ); |
29 | $firstRow = $mypageurl ->getFirstRow(); |
31 | $totalPage = $mypageurl ->getTotalPage(); |
33 | $sql = "select title from ips_archives order by pubdate desc limit " . $firstRow . "," . $perpageNum ; |
35 | $rowsArray = $conne ->getRowsArray( $sql ); |
37 | $pageShow = $mypageurl ->preOffset( $preFonts ). $mypageurl ->floOffset( $nextFonts ). $mypageurl ->getOtherInfo(); |
38 | $smarty ->assign( "Template_Dir" ,Template_Dir); |
39 | $smarty ->assign( "page_act" , $page_act ); |
40 | $smarty ->assign( "pageNow" , $p ); |
41 | $smarty ->assign( "perpageNum" , $perpageNum ); |
42 | $smarty ->assign( "totalPage" , $totalPage ); |
43 | $smarty ->assign( "preFonts" , $mypageurl ->getPreFonts( $preFonts )); |
44 | $smarty ->assign( "rowsArray" , $rowsArray ); |
45 | $smarty ->assign( "mypage" , $mypageurl ); $smarty ->display( "demo.html" ); |
使用方法在demo.php的注释里
demo.html:
06 | < link href="<{$Template_Dir}>/css/common.css" rel="stylesheet" type="text/css"> |
07 | < link href="<{$Template_Dir}>/css/style1.css" rel="stylesheet" type="text/css"> |
08 | < script id = "jq" src="<{$Template_Dir}>/js/jquery-1.8.3.min.js"></ script > |
13 | <{foreach $rowsArray as $val}> |
14 | < li ><{$val['title']}></ li > |
18 | < div id = "page" ><{$mypage}></ div > |
19 | < input id = "pageNow" type = "hidden" value="<{$pageNow}>"> |
21 | < input id = "page_act" type = "hidden" value="<{$page_act}>"> |
23 | < input id = "perpageNum" type = "hidden" value="<{$perpageNum}>"> |
25 | < input id = "totalPage" type = "hidden" value="<{$totalPage}>"> |
27 | < input id = "Template_Dir" type = "hidden" value="<{$Template_Dir}>"> |
28 | < input id = "preFonts" type = "hidden" value="<{$preFonts}>"> |
33 | $(".pagenum").each(function(){ |
34 | if($(this).text() == $("#pageNow").val()){ |
35 | $(this).addClass("selected"); |
39 | if($("#skip").length>0){ |
40 | $("#skip").keydown(function(){ |
41 | if(event.keyCode == 13){ //回车 |
42 | self.location="demo.php?p="+$(this).val(); |
47 | if($("#go").length>0){ |
48 | $("#go").click(function(){ |
49 | self.location="demo.php?p="+$("#skip").val(); |
52 | //如果分页方式是ajax,则加载外部ajax.js |
53 | if($("#page_act").val() == 1){ |
55 | $Template_Dir = $("#Template_Dir").val(); |
56 | $preFonts = $("#preFonts").val(); |
57 | $insertAjax = $("< script src=\"<{$Template_Dir}>/js/ajax.js\"><\/script>"); |
58 | $insertAjax.insertAfter($("#jq")); |
60 | //最后一行row去掉border-bottom |
61 | $("#list ul").children("li:last").css("border-bottom",0); |
ajaxpage.php:
02 | require 'conn/conn.php' ; |
03 | if (isset( $_POST [ 'pageNow' ]) && ! empty ( $_POST [ 'pageNow' ])){ |
04 | $pageNow = $_POST [ 'pageNow' ]; |
07 | if (isset( $_POST [ 'perpageNum' ]) && ! empty ( $_POST [ 'perpageNum' ])){ |
08 | $perpageNum = $_POST [ 'perpageNum' ]; |
11 | $firstRow = $perpageNum * ( $pageNow -1) + 1; |
12 | $sql = "select title from ips_archives order by pubdate desc limit " . $firstRow . "," . $perpageNum ; |
13 | $rowsArray = $conne ->getRowsArray( $sql ); |
ajax.js:
004 | $( "#newsul li" ).remove(); |
006 | $loading = $( "<img class=\"loading\" src=\"" +$Template_Dir+ "/images/loading.gif\">" ); |
007 | $loading.appendTo($( "#newsul" )); |
012 | if (parseInt($( "#pageNow" ).val()) == parseInt($( "#totalPage" ).val())){ |
013 | $( "#flo_page" ).hide(); |
014 | $( "#last_page" ).hide(); |
015 | $( "#pre_page" ).show(); |
016 | $( "#first_page" ).show(); |
017 | } else if (parseInt($( "#pageNow" ).val()) == 1){ |
018 | $( "#pre_page" ).hide(); |
019 | $( "#first_page" ).hide(); |
020 | $( "#flo_page" ).show(); |
021 | $( "#last_page" ).show(); |
023 | if ($( "#pre_page" ).is( ":hidden" ) || $( "#pre_page" ).length == 0){ |
024 | $( "#pre_page" ).show(); |
026 | if ($( "#first_page" ).is( ":hidden" ) || $( "#first_page" ).length == 0){ |
027 | $( "#first_page" ).show(); |
029 | if ($( "#flo_page" ).is( ":hidden" ) || $( "#flo_page" ) == 0){ |
030 | $( "#flo_page" ).show(); |
032 | if ($( "#last_page" ).is( ":hidden" ) || $( "#last_page" ).length == 0){ |
033 | $( "#last_page" ).show(); |
040 | $firstPage = $( "<a id=\"first_page\" class=\"pagenum\">首页</a>" ); |
041 | if ($( "#first_page" ).length == 0){ |
042 | $firstPage.insertBefore($( "#flo_page" )); |
045 | $pre_page = $( "<a id=\"pre_page\" class=\"pagenum\">" +$preFonts+ "</a>" ); |
046 | if ($( "#pre_page" ).length == 0){ |
047 | $pre_page.insertBefore($( "#flo_page" )); |
052 | $.post( "ajaxpage.php" ,{ |
053 | pageNow : parseInt($( "#pageNow" ).val()), |
054 | perpageNum : parseInt($( "#perpageNum" ).val()) |
055 | }, function (data,textStatus){ |
057 | var dataObj=eval( "(" +data+ ")" ); |
059 | $( ".loading" ).remove(); |
060 | $.each(dataObj, function (idx,item){ |
061 | $li_new = $( "<li>" +item.title+ "</li>" ); |
062 | $li_new.appendTo($( "#newsul" )); |
064 | $( "#list ul" ).children( "li:last" ).css( "border-bottom" ,0); |
068 | apagenow = parseInt($( "#pageNow" ).val()); |
070 | function firstPageAct(){ |
071 | if ($( "#first_page" ).is( ":visible" )){ |
072 | $( "#first_page" ).click( function (){ |
076 | $( "#pageNow" ).val(1); |
077 | apagenow = parseInt($( "#pageNow" ).val()); |
079 | $( "#pagenow_info" ).html( " 当前第1页" ); |
088 | function prePageAct(){ |
089 | if ($( "#pre_page" ).is( ":visible" )){ |
090 | $( "#pre_page" ).click( function (){ |
094 | if (parseInt(apagenow) != 1){ |
095 | apagenow = parseInt(apagenow) - parseInt(1); |
097 | $( "#pageNow" ).val(apagenow); |
099 | if (parseInt($( "#pageNow" ).val()) > parseInt(1)){ |
101 | $( "#pagenow_info" ).html( " 当前第" +$( "#pageNow" ).val()+ "页" ); |
111 | if ($( "#flo_page" ).length>0){ |
113 | $( "#flo_page" ).removeAttr( "href" ); |
114 | $( "#flo_page" ).click( function (){ |
117 | apagenow = parseInt(apagenow) + parseInt(1); |
118 | $( "#pageNow" ).val(apagenow); |
120 | if (parseInt($( "#pageNow" ).val()) <= parseInt($( "#totalPage" ).val())){ |
122 | $( "#pagenow_info" ).html( " 当前第" +$( "#pageNow" ).val()+ "页" ); |
127 | if ($( "#first_page" ).is( ":hidden" ) || $( "#first_page" ).length == 0){ |
139 | if ($( "#last_page" ).length>0){ |
141 | $( "#last_page" ).removeAttr( "href" ); |
142 | $( "#last_page" ).click( function (){ |
145 | apagenow = parseInt($( "#totalPage" ).val()); |
146 | $( "#pageNow" ).val(apagenow); |
148 | $( "#pagenow_info" ).html( " 当前第" +$( "#totalPage" ).val()+ "页" ); |
152 | if ($( "#first_page" ).length == 0){ |
162 | $( "#first_page" ).click( function (){ |
165 | $( "#pre_page" ).click( function (){ |
common.css:
01 | a{ font-size : 14px ; font-family : Tahoma ; color : #444 ; text-decoration : none ; cursor : pointer ;} |
02 | ul{ list-style-type : none ;} |
07 | border : 1px solid #95071b ; |
15 | border-bottom : 1px solid #95071b ; |
style1.css:
05 | border : 1px solid #ccc ; |
08 | background-color : #95071b ; |
12 | background-color : #95071b ; |
init.inc.php:
07 | define( "ROOT" , str_replace ( "\\" , "/" ,dirname( __FILE__ )). "/" ); |
08 | require ROOT. 'libs/Smarty.class.php' ; |
09 | $smarty = new Smarty(); |
11 | $smarty ->setTemplateDir(ROOT. 'templates/' ) |
12 | ->setCompileDir(ROOT. 'templates_c/' ) |
13 | ->setPluginsDir(ROOT. 'plugins/' ) |
14 | ->setCacheDir(ROOT. 'cache/' ) |
15 | ->setConfigDir(ROOT. 'configs' ); |
16 | $smarty ->caching = false; |
17 | $smarty ->cache_lifetime = 60*60*24; |
18 | $smarty ->left_delimiter = '<{' ; |
19 | $smarty ->right_delimiter = '}>' ; |
22 | $PHP_SELF = $_SERVER [ 'PHP_SELF' ]; |
23 | $ROOT_URL = 'http://' . $_SERVER [ 'HTTP_HOST' ]. substr ( $PHP_SELF ,0, strrpos ( $PHP_SELF , '/' )+1); |
24 | define(ROOT_URL, $ROOT_URL ); |
26 | define( "Template_Dir" , $ROOT_URL . 'templates' ); |
代码下载地址:https://github.com/camnpr/listPage