ouok 发表于 2009-10-14 18:24:15

分享一个分页类

该类用于分段式URL

page.php类放到libraries文件夹下,代码如下

<?php
class page
{

function __construct()
{
      
}

/**
* constructor构造函数
* @param total_page 传入数据库查询的总记录
* @param nowindex 当前页码
* @param config page的配置文件读取的设置
* @param show 显示方式 1为数字版 "<< < > >>"   2为文字版 "[首页 上页 下页 末页 第3页]"
* @param url url
*/
      function pages($total_page,$nowindex=1,$config,$show=2,$url)
      {      
                $this->page_name=array_key_exists('page_name',$config)?$config['page_name']:'page';
                $total=(intval($total_page)||$total_page>0)?intval($total_page):exit("total参数错误");
                $this->paginal=array_key_exists('paginal',$config)?$config['paginal']:10;
                $this->url=$url.'/index/'.$this->page_name."/";//设置链接地址
                $this->totalpage=ceil($total/$this->paginal);
                $this->nowindex=(!empty($nowindex)&&array_key_exists($this->page_name,$nowindex)&&$nowindex[$this->page_name]<=$this->totalpage)?intval($nowindex[$this->page_name]):1;
                $this->offset=($this->nowindex-1)*$this->paginal;
                $this->total=$total.'条记录 每页'.$this->paginal.'条 ';
                return $this->show($show);
      }

/**
* 获取显示"下一页"的代码
*/
function next_page()
{      
      if($this->nowindex<$this->totalpage){
                return $this->_get_link($this->url.($this->nowindex+1),$this->next_page);
      }
      return '<span>'.$this->next_page.'</span>';
}

/**
* 获取显示“上一页”的代码
*/
function pre_page()
{      
      if($this->nowindex>1){
                return $this->_get_link($this->url.($this->nowindex-1),$this->pre_page);
      }
      return '<span>'.$this->pre_page.'</span>';
}

/**
* 获取显示“首页”的代码
*/
function first_page($style='')
{      
      if($this->nowindex==1)
      {
                return '<span>'.$this->first_page.'</span>';
      }
      return $this->_get_link($this->url.'1',$this->first_page);
}

/**
* 获取显示“尾页”的代码
*/
function last_page()
      {
      if($this->nowindex==$this->totalpage )
      {
                  return '<span>'.$this->last_page.'</span>';
      }
      return $this->_get_link($this->url.($this->totalpage),$this->last_page);
}

//显示数字页码条
function nowbar()
      {
      $plus=ceil($this->paginal/2);
      if($this->paginal-$plus+$this->nowindex>$this->totalpage)$plus=($this->paginal-$this->totalpage+$this->nowindex);
      $begin=$this->nowindex-$plus+1;
      $begin=($begin>=1)?$begin:1;
      $return='';
      for($i=$begin;$i<$begin+$this->paginal;$i++)
                {
                if($i<=$this->totalpage){
                        if($i!=$this->nowindex)
                              $return.=$this->_get_link($this->url.$i,'['.$i.']');
                        else
                              $return.='<span>'.$i.'</span>';
                }else{
                        break;
                }
                $return.="\n";
                }
      unset($begin);
      return $return;
}
/**
* 获取显示跳转按钮的代码
*/
function select()
{
      $return=' <select name="PB_Page_Select">';
      for($i=1;$i<=$this->totalpage;$i++)
      {
                if($i==$this->nowindex){
                        $return.='<option value="'.$i.'" selected>'.$i.'/'.$this->totalpage.'</option>';
                }else{
                        $return.='<option value="'.$i.'">'.$i.'/'.$this->totalpage.'</option>';
                }
      }
unset($i);
$return.='</select> ';
return $return;
}

/**
* 控制分页显示风格(你可以增加相应的风格)
* @return string
*/
function show($show)
{      
      switch ($show)
      {
      case '1'://上一页 1 2 3 4 5 6 7 8 下一页数字板
                $this->pre_page='<';
                $this->next_page='>';
                $this->first_page='<<';
                $this->last_page='>>';
                return $this->total.$this->first_page().' '.$this->pre_page().' '.$this->nowbar().$this->next_page().' '.$this->last_page();
                break;
      case '2'://首页 上一页 下一页 第5页 下一页 尾页   文字版
                $this->next_page='下页';
                $this->pre_page='上页';
                $this->first_page='首页';
                $this->last_page='尾页';
                return $this->total.'['.$this->first_page().' '.$this->pre_page().' '.$this->next_page().' '.$this->last_page().']'.' 第'.$this->select().'页 ';
                break;
      }

}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
   * 获取链接地址
*/
      function _get_link($url,$text)
      {
                        return '<a href="'.$url.'">'.$text.'</a>';
      }
}


page.php配置文件放到config目录下
<?php

$config['pageconfig']['page_name']='page';
$config['pageconfig']['total']='0';//统计数据总数量,由控制器完成计算
$config['pageconfig']['paginal']='15';//每页显示记录数量
$config['pageconfig']['nowindex']='1';//当前page页码






在aotuload文件中自动加载该类和配置文件
该分页只能用于index方法下,调用方式如下

echo $this->page->pages(count($users),$this->uri->ruri_to_assoc(3),$pageconfig,2,$this->url);

smallrong 发表于 2010-2-9 03:00:41

没有图片哦。。估计要自己亲自试试了。看看效果了。。
页: [1]
查看完整版本: 分享一个分页类