哪位帮忙看下这个分页类怎么调用,我说的是在查询语句下
网上找到一个比较好看的分页类,尽管作者已经说明怎么调用了,但是对我来说还是不太明白,主要是在sql语句里面怎么调用,下面是分页类代码:
1 | <br /><?php<br /> <br /> class Page<br />{<br /> private $pageSize ; //您的网站每一页显示的列表条数<br /> private $totalRows ; //通过数据库查询返回的总的记录条数<br /> private $url ; //基准URL<br /> private $pageAmount ; //页码的总数<br /> private $currentPage ; //当前的页码<br /> private $offset = 4; //页码偏移量<br /> private $pageString = 'p' ; //页码在URL中的名字<br /> private $classHere = 'class="here"' ; //当前页链接的 class 样式类名,默认为here<br /> <br /> //初始化当前页码,记录总条数,每页多少条记录<br /> public function __construct( $param )<br /> {<br /> $this ->pageSize = $param [ 'pageSize' ];<br /> $this ->totalRows = $param [ 'totalRows' ];<br /> $this ->url = $param [ 'baseUrl' ];<br /> $this ->offset = ! empty ( $param [ 'offset' ])? $param [ 'offset' ]: $this ->offset;<br /> $this ->pageString = ! empty ( $param [ 'pageString' ])? $param [ 'pageString' ]: $this ->pageString;<br /> $this ->classHere = ! empty ( $param [ 'className' ])? $param [ 'className' ]: $this ->classHere;<br /> $this ->currentPage = (int) $param [ 'currentPage' ];<br /> }<br /> <br /> <br /> public function pagination( $style = '1' , $output =TRUE)<br /> {<br /> $this ->baseUrl();<br /> $this ->pageAmount();<br /> $this ->currentPage();<br /> <br /> //获取全部组件<br /> if ( $style == '1' )<br /> {<br /> $page = $this ->indexPage(). $this ->prevPage(). $this ->pageNumber(). $this ->nextPage(). $this ->endPage();<br /> }<br /> else if ( $style == '2' )<br /> {<br /> //获取纯数字链接<br /> $page = $this ->pageNumber();<br /> }<br /> else if ( $style == '3' )<br /> {<br /> //只获取上一页下一页<br /> $page = $this ->prevPage(). $this ->nextPage();<br /> }<br /> else if ( $style == '4' )<br /> {<br /> //上一页、下一页、数字链接<br /> $page = $this ->prevPage(). $this ->pageNumber(). $this ->nextPage();<br /> }<br /> <br /> if ( $output )<br /> {<br /> return $page ;<br /> }<br /> else <br /> {<br /> echo $page ;<br /> }<br /> }<br /> <br /> <br /> public function getCurrentPage()<br /> {<br /> $this ->pageAmount();<br /> $this ->currentPage();<br /> return $this ->currentPage;<br /> }<br /> <br /> <br /> public function pageAmount()<br /> {<br /> $this ->pageAmount = ceil ( $this ->totalRows / $this ->pageSize);<br /> if ( $this ->pageAmount <= 0)<br /> {<br /> $this ->pageAmount = '1' ;<br /> }<br /> return $this ->pageAmount;<br /> }<br /> <br /> <br /> private function baseUrl()<br /> {<br /> if (preg_match( '/\?/' , $this ->url))<br /> {<br /> $this ->url = $this ->url. '&' . $this ->pageString. '=' ;<br /> }<br /> else <br /> {<br /> $this ->url = $this ->url. '?' . $this ->pageString. '=' ;<br /> }<br /> }<br /> <br /> <br /> private function currentPage()<br /> {<br /> if ( $this ->currentPage < 1 || ! $this ->currentPage)<br /> {<br /> $this ->currentPage = 1;<br /> }<br /> else if (( $this ->currentPage > $this ->pageAmount))<br /> {<br /> $this ->currentPage = $this ->pageAmount;<br /> }<br /> }<br /> <br /> <br /> private function indexPage()<br /> {<br /> if ( $this ->currentPage == 1) return ;<br /> return '<a href="' . $this ->url. '1">首页</a>' ;<br /> }<br /> <br /> <br /> private function endPage()<br /> {<br /> if ( $this ->currentPage == $this ->pageAmount) return ;<br /> return '<a href="' . $this ->url. $this ->pageAmount. '">尾页</a>' ;<br /> }<br /> <br /> <br /> private function prevPage()<br /> {<br /> if ( $this ->currentPage == 1) return ;<br /> return '<a href="' . $this ->url.( $this ->currentPage - 1 ). '">上一页</a>' ;<br /> }<br /> <br /> <br /> private function nextPage()<br /> {<br /> if ( $this ->currentPage == $this ->pageAmount) return ;<br /> return '<a href="' . $this ->url.( $this ->currentPage + 1 ). '">下一页</a>' ;<br /> }<br /> <br /> <br /> private function pageNumber()<br /> {<br /> $left = "" ;<br /> $right = "" ;<br /> <br /> //如果总记录的条数“大于”所有链接的数量时候<br /> if ( $this ->pageAmount > ( $this ->offset * 2 + 1))<br /> {<br /> //当前页码距离首页的距离<br /> $leftNum = $this ->currentPage - 1;<br /> <br /> //当前页码距离尾页的距离<br /> $rightNum = $this ->pageAmount - $this ->currentPage;<br /> <br /> //当当前页码距离首页距离不足偏移量offset时候,在右边补齐缺少的小方块<br /> if ( $leftNum < $this ->offset)<br /> {<br /> //左边的链接<br /> for ( $i = $leftNum ; $i >= 1 ; $i --)<br /> {<br /> $left .= '<a href="' . $this ->url.( $this ->currentPage - $i ). '">' .( $this ->currentPage - $i ). '</a>' ;<br /> }<br /> <br /> //右边的链接<br /> for ( $j = 1; $j <= ( $this ->offset * 2 - $leftNum ); $j ++)<br /> {<br /> $right .= '<a href="' . $this ->url.( $this ->currentPage + $j ). '">' .( $this ->currentPage + $j ). '</a>' ;<br /> }<br /> }<br /> else if ( $rightNum < $this ->offset)<br /> {<br /> //左边的链接<br /> for ( $i = ( $this ->offset * 2 - $rightNum ); $i >= 1 ; $i --)<br /> {<br /> $left .= '<a href="' . $this ->url.( $this ->currentPage - $i ). '">' .( $this ->currentPage - $i ). '</a>' ;<br /> }<br /> <br /> //右边的链接<br /> for ( $j = 1; $j <= $rightNum ; $j ++)<br /> {<br /> $right .= '<a href="' . $this ->url.( $this ->currentPage + $j ). '">' .( $this ->currentPage + $j ). '</a>' ;<br /> }<br /> }<br /> else <br /> {<br /> //当前链接左边的链接<br /> for ( $i = $this ->offset; $i >= 1 ; $i --)<br /> {<br /> $left .= '<a href="' . $this ->url.( $this ->currentPage - $i ). '">' .( $this ->currentPage - $i ). '</a>' ; <br /> }<br /> <br /> //当前链接右边的链接<br /> for ( $j = 1; $j <= $this ->offset; $j ++)<br /> {<br /> $right .= '<a href="' . $this ->url.( $this ->currentPage + $j ). '">' .( $this ->currentPage + $j ). '</a>' ;<br /> }<br /> }<br /> <br /> return $left . '<a href="' . $this ->url. $this ->currentPage. '" class="here">' . $this ->currentPage. '</a>' . $right ;<br /> }<br /> else <br /> {<br /> $allLink = '' ;<br /> //当页码总数小于需要显示的链接数量时候,则全部显示出来<br /> for ( $j = 1; $j <= $this ->pageAmount; $j ++)<br /> {<br /> $allLink .= '<a href="' . $this ->url. $j . '" ' .( $j == $this ->currentPage? $this ->classHere: '' ). '>' . $j . '</a>' ;<br /> }<br /> return $allLink ;<br /> }<br /> }<br /> <br />}<br />
|
Copy after login
------解决思路----------------------这个类只负责分页条的产生
唯一可能与数据库查询有关的是参数数组 $param['totalRows'] 项
因为待分页的总行数是查询得到的,所以这个查询应在 $param 赋值之前完成
------解决思路----------------------楼主也够懒的了,我这里有个给你,样式都写好了
1 | <br /><?php<br /> class Page { <br /> private $total ;
|
Copy after login
具体调用方法:
$num=15; //每页显示数
$page=new page($total,$num);
$result=$db->query("select * from ".$db->table('article').$where." order by addtime desc {$page->limit}");
循环
fpage(array(3,4,5,6,7,0,1,2,8));?> //php分页类调用,这些数字可以去类文件里面看
fpage(array(3,4,5,6,7,8));?>
这是我调用的
CSS样式文件
.fenye li{float:left; font-family:Arial, Helvetica, sans-serif; margin-left:6px; display:inline; line-height:30px;}
.fenye a{display:block;height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:3px; padding:3px 5px;line-height:30px;text-decoration:none;color:#666;}
.fenye a:hover{background:#FF4500;border-color:#FF4500; color:#FFF;}
.fenye a.here{background:#FF4500;border-color:#FF4500; color:#FFF;}
.fenye .sel{background:#E5EDF2; color:#333; font-weight:bold; border:1px #C2D5E3 solid; padding:0 12px; border-radius:4px}
下面上效果图: