一个实例php mysql模板分页类
发布:smiling 来源: PHP粉丝网 添加日期:2013-12-19 10:28:43 浏览: 评论:0
- <?php
- /*
- * 模板分页类,源于easp的数据库分页方法,算是easp分页的的php独立版
- * 支持动态和静态分页方式
- * easp官网http://easp.lengshi.com/
- * 作者:钟晶晶
- * 日期:2010-11-3
- * 邮箱:zhongjingjing@gmail.com
- * 博客:http://blog.zaimer.com
- * page([总记录数=1],[分页大小=20],[当前页=1],[显示页数=6],[分页参数='page'],[分页链接=当前页面],[是否静态=false])
- * 动态:
- * 简单用法:
- * $page = new page(50);
- * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
- * echo $page->getpager('zjj');
- * 全参数用法:
- * $page = new page(50,20,1,6,'page','prrr.php',false);
- * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
- * echo $page->getpager('zjj');
- * 静态:
- * $page = new page(50,20,1,6,'page','prrr{page}.html',true);
- * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
- * echo $page->getpager('zjj');
- */
- class page {
- private $page_size; //每页显示的条目数
- private $total_size; //总条目数
- private $current_page; //当前被选中的页
- private $sub_pages; //每次显示的页数
- private $total_pages; //总页数
- private $page_tpl = array (); // 分页模板
- private $pageparam;
- private $pagelink;
- private $static;
- function __construct($total_size = 1, $page_size = 20, $current_page = 1, $sub_pages = 6, $pageparam = 'page', $pagelink = '', $static = false) {
- $this->page_size = intval ( $page_size );
- $this->total_size = intval ( $total_size );
- if (! $current_page) {
- $this->current_page = 1;
- } else {
- $this->current_page = intval ( $current_page );
- }
- $this->total_pages = ceil ( $total_size / $page_size );
- $this->sub_pages = intval ( $sub_pages );
- $this->pageparam = $pageparam;
- $this->pagelink = (emptyempty ( $pagelink ) ? $_server ["php_self"] : $pagelink);
- $this->static = $static;
- $this->page_tpl ['default'] = array ('tpl' => '<div class="pager">{first}{prev}{liststart}{list}{listend}{next}{last} 跳转到{jump}页</div>', 'config' => array () );
- }
- public function __set($param, $value) {
- $this->$param = $value;
- }
- public function __get($param) {
- return $this->$param;
- }
- /*
- __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
- */
- function __destruct() {
- unset ( $page_size ); //每页显示的条目数
- unset ( $total_size ); //总条目数
- unset ( $current_page ); //当前被选中的页
- unset ( $sub_pages ); //每次显示的页数
- unset ( $total_pages ); //总页数
- unset ( $page_tpl ); // 分页模板
- unset ( $pageparam ); //分页参数,默认page
- unset ( $pagelink );
- unset ( $static );
- }
- private function urlparameters($url = array()) {
- foreach ( $url as $key => $val ) {
- if ($key != $this->pageparam)
- $arg [] = $key . '=' . $val;
- }
- $arg [] = $this->pageparam . '=*';
- if ($this->static)
- return str_replace ( '{page}', '*', $this->pagelink );
- else
- return $this->pagelink . '?' . implode ( '&', $arg );
- }
- public function setpager($tpl_name = 'default', $tpl = '', $config = array()) {
- if (emptyempty ( $tpl ))
- $tpl = $this->page_tpl ['default'] ['tpl'];
- if (emptyempty ( $config ))
- $config = $this->page_tpl ['default'] ['config'];
- $this->page_tpl [$tpl_name] = array ('tpl' => $tpl, 'config' => $config );
- }
- public function getpager($tpl_name = 'default') {
- $this->getcurrentpage ();
- return $this->pager ( $this->page_tpl [$tpl_name] );
- }
- public function getcurrentpage() {
- $this->current_page = ($_get [$this->pageparam] <= intval ( $this->total_pages ) ? ($_get [$this->pageparam] < 1 ? 1 : $_get [$this->pageparam]) : intval ( $this->total_pages ));
- }
- public function pager($page_tpl = '') {
- if (emptyempty ( $page_tpl ))
- $page_tpl = $this->page_tpl ['default'];
- $cfg = array ('recordcount' => intval ( $this->total_size ), 'pageindex' => intval ( $this->current_page ), 'pagecount' => intval ( $this->total_pages ), 'pagesize' => intval ( $this->page_size ), 'listlong' => intval ( $this->sub_pages ), 'listsidelong' => 2, 'list' => '*', 'currentclass' => 'current', 'link' => $this->urlparameters ( $_get ), 'first' => '«', 'prev' => '‹', 'next' => '›', 'last' => '»', 'more' => '...', 'disabledclass' => 'disabled', 'jump' => 'input', 'jumpplus' => '', 'jumpaction' => '', 'jumplong' => 50 );
- if (! emptyempty ( $page_tpl ['config'] )) {
- foreach ( $page_tpl ['config'] as $key => $val ) {
- if (array_key_exists ( $key, $cfg ))
- $cfg [$key] = $val;
- }
- }
- $tmpstr = $page_tpl ['tpl'];
- $pstart = $cfg ['pageindex'] - (($cfg ['listlong'] / 2) + ($cfg ['listlong'] % 2)) + 1;
- $pend = $cfg ['pageindex'] + $cfg ['listlong'] / 2;
- if ($pstart < 1) {
- $pstart = 1;
- $pend = $cfg ['listlong'];
- }
- if ($pend > $cfg ['pagecount']) {
- $pstart = $cfg ['pagecount'] - $cfg ['listlong'] + 1;
- $pend = $cfg ['pagecount'];
- }
- if ($pstart < 1)
- $pstart = 1;
- for($i = $pstart; $i <= $pend; $i ++) {
- if ($i == $cfg ['pageindex'])
- $plist .= '<span class="' . $cfg ['currentclass'] . '" >' . str_replace ( '*', $i, $cfg ['list'] ) . '</span> ';
- else
- $plist .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
- }
- if ($cfg ['listsidelong'] > 0) {
- if ($cfg ['listsidelong'] < $pstart) {
- for($i = 1; $i <= $cfg ['listsidelong']; $i ++) {
- $pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
- }
- $pliststart .= ($cfg ['listsidelong'] + 1) == $pstart ? '' : $cfg ['more'] . ' ';
- } else {
- if ($cfg ['listsidelong'] >= $pstart && $pstart > 1) {
- for($i = 1; $i <= ($pstart - 1); $i ++) {
- $pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
- }
- }
- }
- if (($cfg ['pagecount'] - $cfg ['listsidelong']) > $pend) {
- $plistend = ' ' . $cfg ['more'] . $plistend;
- for($i = (($cfg ['pagecount'] - $cfg ['listsidelong']) + 1); $i <= $cfg ['pagecount']; $i ++) {
- $plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
- }
- } else {
- if (($cfg ['pagecount'] - $cfg ['listsidelong']) <= $pend && $pend < $cfg ['pagecount']) {
- for($i = ($pend + 1); $i <= $cfg ['pagecount']; $i ++) {
- $plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
- }
- }
- }
- }
- if ($cfg ['pageindex'] > 1) {
- $pfirst = ' <a href="' . str_replace ( '*', '1', $cfg ['link'] ) . '">' . $cfg ['first'] . '</a> ';
- $pprev = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] - 1, $cfg ['link'] ) . '">' . $cfg ['prev'] . '</a> ';
- } else {
- $pfirst = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['first'] . '</span> ';
- $pprev = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['prev'] . '</span> ';
- }
- if ($cfg ['pageindex'] < $cfg ['pagecount']) {
- $plast = ' <a href="' . str_replace ( '*', $cfg ['pagecount'], $cfg ['link'] ) . '">' . $cfg ['last'] . '</a> ';
- $pnext = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] + 1, $cfg ['link'] ) . '">' . $cfg ['next'] . '</a> ';
- } else {
- $plast = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['last'] . '</span> ';
- $pnext = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['next'] . '</span> ';
- }
- switch (strtolower ( $cfg ['jump'] )) {
- case 'input' :
- $pjumpvalue = 'this.value';
- $pjump = '<input type="text" size="3" title="请输入要跳转到的页数并回车"' . (($cfg ['jumpplus'] == '') ? '' : ' ' . $cfg ['jumpplus']);
- $pjump .= ' onkeydown=":if(event.charcode==13||event.keycode==13){if(!isnan(' . $pjumpvalue . ')){';
- $pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
- $pjump .= '}return false;}" />';
- break;
- case 'select' :
- $pjumpvalue = "this.options[this.selectedindex].value";
- $pjump = '<select ' . ($cfg ['jumpplus'] == '' ? ' ' . $cfg ['jumpplus'] . ' onchange="javascript:' : $cfg ['jumpplus']);
- $pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
- $pjump .= '" title="请选择要跳转到的页数"> ';
- if ($cfg ['jumplong'] == 0) {
- for($i = 0; $i <= $cfg ['pagecount']; $i ++) {
- $pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . ' >' . $i . '</option> ';
- }
- } else {
- $pjumplong = intval ( $cfg ['jumplong'] / 2 );
- $pjumpstart = ((($cfg ['pageindex'] - $pjumplong) < 1) ? 1 : ($cfg ['pageindex'] - $pjumplong));
- $pjumpstart = ((($cfg ['pagecount'] - $cfg ['pageindex']) < $pjumplong) ? ($pjumpstart - ($pjumplong - ($cfg ['pagecount'] - $cfg ['pageindex'])) + 1) : $pjumpstart);
- $pjumpstart = (($pjumpstart < 1) ? 1 : $pjumpstart);
- $j = 1;
- for($i = $pjumpstart; $i <= $cfg ['pageindex']; $i ++, $j ++) {
- $pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . '>' . $i . '</option> ';
- }
- $pjumplong = $cfg ['pagecount'] - $cfg ['pageindex'] < $pjumplong ? $pjumplong : $pjumplong + ($pjumplong - $j) + 1;
- $pjumpend = $cfg ['pageindex'] + $pjumplong > $cfg ['pagecount'] ? $cfg ['pagecount'] : $cfg ['pageindex'] + $pjumplong;
- for($i = $cfg ['pageindex'] + 1; $i <= $pjumpend; $i ++) {
- $pjump .= '<option value="' . $i . '">' . $i . '</option> ';
- }
- }
- $pjump .= '</select>';
- break;
- }
- $patterns = array ('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize}/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev}/', '/{next}/', '/{last}/', '/{jump}/' );
- $replace = array ($cfg ['recordcount'], $cfg ['pagecount'], $cfg ['pageindex'], $cfg ['pagesize'], $plist, $pliststart, $plistend, $pfirst, $pprev, $pnext, $plast, $pjump );
- $tmpstr = chr ( 13 ) . chr ( 10 ) . preg_replace ( $patterns, $replace, $tmpstr ) . chr ( 13 ) . chr ( 10 );
- unset ( $cfg );
- return $tmpstr;
- }
- }
- ?>
Tags: 实例 mysql 模板分页类
- 上一篇:php分页代码
- 下一篇:理解php分页思路方法详解
相关文章
- ·php Ajax分页简单应用实例(2014-01-16)
- ·php分页实例程序函数(2014-07-18)
- ·php简单分页实例程序代码详解(2014-09-22)
- ·PHP MSSQL 分页实例(2019-09-29)
- ·PHP MSSQL 分页实例(2021-07-28)
- ·php实现分页功能的详细实例方法(2021-12-24)
- ·小谈PHP&MYSQL分页原理及实现(2013-11-13)
- ·PHP MySQL分页显示(2013-12-10)
- ·一个简单php和mysql数据分页程序(2014-01-16)
- ·php mysql 分页显示代码(2014-08-02)
- ·二款php mysql 分页代码(2014-08-15)
- ·php mysql 分页函数(2014-08-15)
- ·php mysql网页分页代码(2014-08-18)
- ·mysql 分页存储过程实例(2014-09-11)
- ·php mysql经典分页程序(2014-09-11)
- ·简单实用php mysql分页代码(2014-09-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)