Heim  >  Artikel  >  Backend-Entwicklung  >  codeigniter中测试通过的分页类示例_PHP教程

codeigniter中测试通过的分页类示例_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:32:13717Durchsuche

通用分页类(以Codeigniter测试)

codeigniter中测试通过的分页类示例_PHP教程

page_list.php

复制代码 代码如下:

/**
 * 分页类
 */
class Page_list {

    /**
     * 总数据
     * @var int
     */
    private $total;
    /**
     * 每页显示数据
     * @var int
     */
    private $size;
    /**
     * 当前页数
     * @var int
     */
    private $page;
    /**
     * 页数列表左右页数
     * @var int
     */
    private $len;

    /**
     * 总页数
     * @var int
     */
    private $page_total;
    /**
     * 页码列表
     * @var array
     */
    private $page_list;

    /**
     * 基准地址
     * @var string
     */
    private $base_url;
    /**
     * 替换标志
     * @var string
     */
    private $place;
    /**
     * 分页样式
     * @var string
     */
    private $style;


    /**
     * 构造函数
     *
     * @param array $config 配置数组
     */
    public function __construct($config = array()){
        // 初始化默认值
        $this->total = 0;
        $this->size = 20;
        $this->page = 1;
        $this->len = 4;
        $this->page_total = 1;
        $this->page_list = array();
        $this->base_url = '?page=-page-';
        $this->place = '-page-';
        $this->style = $this->get_default_style();
        $this->initialize($config);
    }

    /**
     * 初始化分页
     *
     * @param array $config 配置数组
     */
    public function initialize($config = array()){
        // 取得配置值
        if(is_array($config)){
            if(array_key_exists('total', $config)) $this->total = @intval($config['total']);
            if(array_key_exists('size', $config)) $this->size = @intval($config['size']);
            if(array_key_exists('page', $config)) $this->page = @intval($config['page']);
            if(array_key_exists('len', $config)) $this->len = @intval($config['len']);
            if(array_key_exists('base_url', $config)) $this->base_url = @strval($config['base_url']);
            if(array_key_exists('place', $config)) $this->place = @strval($config['place']);
            if(array_key_exists('style', $config)) $this->style = @strval($config['style']);
        }
        // 修正值
        if($this->totaltotal = 0;
        if($this->sizesize = 20;
        if($this->pagepage = 1;
        if($this->lenlen = 4;
        // 执行分页算法
        $this->page_total = ceil($this->total/$this->size);
        if($this->page_totalpage_total = 1;
        if($this->page>$this->page_total) $this->page = $this->page_total;
        if($this->page-$this->len>=1){
            for($i=$this->len; $i>0; $i--){
                $this->page_list[] = $this->page - $i;
            }
        }else{
            for($i=1; $ipage; $i++){
                $this->page_list[] = $i;
            }
        }
        $this->page_list[] = $this->page;
        if($this->page+$this->lenpage_total){
            for($i=1; $ilen; $i++){
                $this->page_list[] = $this->page + $i;
            }
        }else{
            for($i=$this->page+1; $ipage_total; $i++){
                $this->page_list[] = $i;
            }
        }
    }

    /**
     * 默认分页样式
     *
     * @return string
     */
    public function get_default_style(){
        $style = '';
        return $style;
    }

    /**
     * 是否是第一页
     *
     * @return bool
     */
    public function is_first_page(){
        return $this->page == 1;
    }

    /**
     * 获取第一页页码
     *
     * @return int
     */
    public function get_first_page(){
        return 1;
    }

    /**
     * 是否是最后一页
     *
     * @return bool
     */
    public function is_last_page(){
        return $this->page == $this->page_total;
    }

    /**
     * 获取最后一页页码
     *
     * @return int
     */
    public function get_last_page(){
        return $this->page_total;
    }

    /**
     * 是否存在上一页
     *
     * @return bool
     */
    public function has_prev_page(){
        return $this->page > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return bool
     */
    public function has_next_page(){
        return $this->page page_total;
    }

    /**
     * 获取上一页页码
     *
     * @return int
     */
    public function get_prev_page(){
        return $this->has_prev_page() ? $this->page - 1 : $this->page;
    }

    /**
     * 获取下一页页码
     *
     * @return int
     */
    public function get_next_page(){
        return $this->has_next_page() ? $this->page + 1 : $this->page;
    }

    /**
     * 获取当前页页码
     *
     * @return int
     */
    public function get_curr_page(){
        return $this->page;
    }

    /**
     * 获取总数据数
     *
     * @return int
     */
    public function get_total(){
        return $this->total;
    }

    /**
     * 获取每页显示数据数
     *
     * @return int
     */
    public function get_size(){
        return $this->size;
    }

    /**
     * 获取总页数
     *
     * @return int
     */
    public function get_total_page(){
        return $this->page_total;
    }

    /**
     * 构建并返回分页代码
     *
     * @param string $base_url 基准地址
     * @param string $place 替换标志
     * @param string $style 分页样式
     * @return string 分页HTML代码
     */
    public function display($base_url = '', $place = '', $style = ''){
        if($base_url==='') $base_url = $this->base_url;
        if($place==='') $place = $this->place;
        if($style==='') $style = $this->style;
        $str = $style.'

';
        if( ! $this->is_first_page()){
            $str .= '首页';
        }
        if($this->has_prev_page()){
            $str .= '上一页';
        }
        foreach($this->page_list as $v){
            if($v==$this->page){
                $str .= '' . $v . '';
            }else{
                $str .= ''.$v.'';
            }
        }
        if($this->has_next_page()){
            $str .= '下一页';
        }
        if( ! $this->is_last_page()){
            $str .= '尾页';
        }
        $str .= '
';
        return $str;
    }

}
?>

/application/view/pagelist.php

复制代码 代码如下:

    class Pagelist extends CI_Controller {

        public function page(){
            $this->load->helper('url');
            $page = $this->input->get('page');
            $page = @intval($page);
            if($page            $this->load->library('page_list',array('total'=>10000,'size'=>16,'page'=>$page));
            $pl = $this->page_list->display(site_url('pagelist/page/page/-page-'));
            $this->load->view('pagelist', array('pl' => $pl));
        }

    }
?>

/application/view/pagelist.php

复制代码 代码如下:




   
    分页测试





www.bkjia.comtruehttp://www.bkjia.com/PHPjc/755840.htmlTechArticle通用分页类(以Codeigniter测试) page_list.php 复制代码 代码如下: ?php if( ! defined('BASEPATH')) die('No Access'); /** * 分页类 */ class Page_list { /** * 总数...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn