PHP分页类

下面是我自己的分页类,可适当的修改
<?php
/**
 * @name Pager's class
 * @author Refer
 * @copyright www.phpcq.com
 * @version Alpha 1.0
 * @todo 下拉菜单分页---From表单传值...可以根据自己的情况(采用GET传值)
 *
 */
class Pager{
    
/**
     * startRow
     *
     * @var Int
     */
    
public $startRow;
    
/**
     * 页面的总数
     *
     * @var Int
     */
    
public $pageTotal;
    
/**
     *翻页值
     *
     * @var Int
     */
    
public $offset;
    
/**
     * 每页所显示记录数
     *
     * @var Int
     */
    
public $pageSize;
    
/**
     * get参数
     *
     * @var Int
     */
    
public $get;
    
/**
     * post参数
     *
     * @var Int
     */
    
public $post;
    
/**
     * 构造函数--初始化翻页类
     *
     * @param Int $total
     * @param Int $pageSize
     */
    
public function __construct($total,$pageSize){
        
$this->get = (int)$_GET['startRow'];
        
$this->post = (int)$_POST['pageNums'];
        
$this->pageSize $pageSize;
        
$this->pageTotal ceil($total $pageSize);
        switch (
$_GET['method']){
            default:
                
$this->startRow 1;
                
$this->offset 0;
            break;
            case 
'first':
                
$this->first();
            break;    
            case 
'previous':
                
$this->previous();
            break;    
            case 
'next':
                
$this->next();
            break;    
            case 
'last':
                
$this->last();
            break;    
        }
    } 
    
/**
     * 首页方法
     *
     */
    
public function first(){
        
$this->startRow 1;
        
$this->offset = ($this->startRow 1) * $this->pageSize;
    }
    
/**
     * 上一页方法
     *
     */
    
public function previous(){
        
$this->startRow $this->get 1;
        if (
$this->startRow 1) {
            
$this->startRow 1;
        }
        
$this->offset = ($this->startRow 1) * $this->pageSize;
    }
    
/**
     * 下一页方法
     *
     */
    
public function next(){
        
$this->startRow $this->get 1;
        if (
$this->startRow $this->pageTotal) {
            
$this->startRow $this->pageTotal;
        }
        
$this->offset = ($this->startRow 1) * $this->pageSize;
    }
    
/**
     * 最后页方法
     *
     */
    
public function last(){
        
$this->startRow $this->pageTotal;
        
$this->offset = ($this->startRow 1) * $this->pageSize;
    }
    
/**
     * 下拉菜单翻页
     *
     * @return Int
     */
    
public function numPages(){
        if (
$this->post) {
            
$pageNums "<option>$this->post</option>";
            
$this->startRow $this->post;
            
$this->offset = ($this->post 1) * $this->pageSize;
        }
        for (
$i 1$i <= $this->pageTotal;$i++){
            if (
$i != $this->post) {
                
$pageNums .= "<option>$i</option>";
            }
        }
        return 
$pageNums;
    }
    
/**
     * 当前是第几页
     *
     * @return Int
     */
    
public function noncePage(){
        if (
$this->post) {
            return 
$this->startRow $this->post;
        } else {
            return 
$this->startRow $this->startRow;
        }
    }
}

调用方法:
$total $total->total();//总记录
            
$pageSize 15;//每页显示的记录
            
$pager = new Pager($total,$pageSize);
            if (
$pager->post){$startRow $pager->noncePage();}
            
$startRow $pager->startRow;
            
$pager->numPages();
            
$article->query("SELECT * FROM `categories` as c,`articles` as n WHERE c.id = n.pid
                    ORDER BY n.id DESC LIMIT $pager->offset,$pageSize"
);
            while (
$article->fetchArray()) {
                
$article->data['content'] = mb_substr(strip_tags($article->data['content']),0,40,'utf-8');
                
$articleIndex[] = $article->data;            
            }
            
$article->free();
        } else {
            
header("location:?act=login");
        }
        
$view->view->assign('startRow',$startRow);//caption
        
$view->view->assign('total',$total);//caption
        
$view->view->assign('pageNums',$pager->numPages());//pageNums
        
$view->view->assign('noncePage',$pager->noncePage());//pageNums
        
$view->view->assign('pageTotal',$pager->pageTotal);//pageTotal 

模板页面:
  <!--{if 
$pageTotal 1}-->
 <
form action="" method="post">
 <
div id="projectpages" class="fontpage">
 
Pager:
 <
select name="pageNums" onchange="javascript:submit();" style="height:20px;">
         <!--{
$numPages}-->
   </
select>
 <
a href="?act=blog&method=first&startRow=<!--{$startRow}-->">First</a
 <
a href="?act=blog&method=previous&startRow=<!--{$startRow}-->">Previous</a
 <
a href="?act=blog&method=next&startRow=<!--{$startRow}-->">Next</a>
 <
a href="?act=blog&method=last&startRow=<!--{$startRow}-->">Last</a>
 </
div>
 </
form>
 <!--{/if}-->    

上一篇文章: Mysql锁表 下一篇文章: 高效的模型设计

关于PHP博客

PHP blogger

博客-致力于分享PHP技术的开发心得, 记录站长生活的点点滴滴,PHP博客采用PHP 面向对象编程+Smarty模板引擎+Mysql数据库 开发(全站纯静态化与伪静态化)
QQ:334192009
电邮:admin@PHPdesigner.org

本站搜索

本站最新文章

博客最近档案

友情链接