一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - codeigniter中測試通過的分頁類示例

codeigniter中測試通過的分頁類示例

2020-06-23 13:33PHP技術網 PHP教程

這篇文章主要介紹了codeigniter中測試通過的分頁類示例,需要的朋友可以參考下

通用分頁類(以Codeigniter測試)

codeigniter中測試通過的分頁類示例

page_list.php

復制代碼 代碼如下:


<?php if( ! defined('BASEPATH')) die('No Access');

 

/**
 * 分頁類
 */
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->total<0) $this->total = 0;
        if($this->size<=0) $this->size = 20;
        if($this->page<=0) $this->page = 1;
        if($this->len<=0) $this->len = 4;
        // 執行分頁算法
        $this->page_total = ceil($this->total/$this->size);
        if($this->page_total<=0) $this->page_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; $i<$this->page; $i++){
                $this->page_list[] = $i;
            }
        }
        $this->page_list[] = $this->page;
        if($this->page+$this->len<=$this->page_total){
            for($i=1; $i<=$this->len; $i++){
                $this->page_list[] = $this->page + $i;
            }
        }else{
            for($i=$this->page+1; $i<=$this->page_total; $i++){
                $this->page_list[] = $i;
            }
        }
    }

    /**
     * 默認分頁樣式
     *
     * @return string
     */
    public function get_default_style(){
        $style = '<style type="text/css">';
        $style .= ' div.page_list { margin:0;padding:0;overflow:hidden;zoom:1;}';
        $style .= ' div.page_list a {display:block;float:left;height:20px;line-height:21px; font-size:13px;font-weight:normal;font-style:normal;color:#133DB6;text-decoration:none;margin:0 3px;padding:0 7px;overflow;zoom:1;}';
        $style .= ' div.page_list a.page_list_act { font-size:13px;padding:0 6px;}';
        $style .= ' div.page_list a:link, div.page_list a:visited { background:#FFF;border:1px #EEE solid;text-decoration:none;}';
        $style .= ' div.page_list a:hover, div.page_list a:active { background:#EEE;text-decoration:none;}';
        $style .= ' div.page_list strong { display:block;float:left;height:20px;line-height:21px;font-size:13px;font-weight:bold;font-style:normal;color:#000;margin:0 3px;padding:0 8px;overflow:hidden;zoom:1;}';
        $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 < $this->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.'<div class="page_list">';
        if( ! $this->is_first_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_first_page(), $base_url).'">首頁</a>';
        }
        if($this->has_prev_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_prev_page(), $base_url).'">上一頁</a>';
        }
        foreach($this->page_list as $v){
            if($v==$this->page){
                $str .= '<strong>' . $v . '</strong>';
            }else{
                $str .= '<a href="'.str_replace($place, $v, $base_url).'">'.$v.'</a>';
            }
        }
        if($this->has_next_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_next_page(), $base_url).'">下一頁</a>';
        }
        if( ! $this->is_last_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_last_page(), $base_url).'">尾頁</a>';
        }
        $str .= '</div>';
        return $str;
    }

}
?>

 

/application/view/pagelist.php

復制代碼 代碼如下:


<?php if( ! defined('BASEPATH')) die('No Access');

 

    class Pagelist extends CI_Controller {

        public function page(){
            $this->load->helper('url');
            $page = $this->input->get('page');
            $page = @intval($page);
            if($page<=0) $page = 1;
            $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

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>分頁測試</title>
</head>
<body>
<?php echo $pl; ?>
</body>
</html>

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品吹潮香蕉在线观看 | 亚洲国产在线视频精品 | 免费观看欧美一级高清 | 男人天堂黄色 | 吃瓜视频在线观看 | 精品老司机在线视频香蕉 | 免费十几分视频 | juy_661佐佐木明希在线播放 | 日本在线亚州精品视频在线 | 变形金刚第一部 | 日本一区二区视频在线 | 日噜噜 | 亚洲欧美日韩另类在线 | 热久久天天拍天天拍热久久2018 | 99精品全国免费7观看视频 | 国产精品久久久久久五月尺 | 韩国美女豪爽一级毛片 | 国产精品 色 | 国产999在线观看 | 国产自产2023最新麻豆 | 丝袜捆绑调教视频免费区 | 精品国产综合 | 99精品热线在线观看免费视频 | 美女被狂揉下部羞羞动漫 | 婷婷综合久久中文字幕 | bt天堂在线最新版在线 | 狠狠色婷婷日日综合五月 | 四虎影视网站 | 日本一区视频在线 | 久久国产精品人妻中文 | 亚洲九九九 | 国产馆| 狠狠鲁视频 | 国产亚洲精品福利在线 | 亚洲四虎永久在线播放 | 天天做日日做天天添天天欢公交车 | 国产精品一区二区久久 | 果冻传媒九一制片厂网站 | 欧美洲大黑香蕉在线视频 | 亚洲人成综合在线播放 | 午夜亚洲 |