程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php codeigniter框架分頁類

php codeigniter框架分頁類

編輯:關於PHP編程

codeigniter 具有非常容易使用的分頁類。在本教程中我會做一個從數據庫教程返回一組結果並分頁這些結果的簡單例子。我將使用最新版本的 ci。分頁類並沒有修改(至少我認為沒有),用最新的穩定版框架總是好的
調用方法

//創建分頁
$config = array();
$this->load->library('hpages');
$config['base_url'] = "channel/lists/c{$slug}/{page}";
$config['total_rows'] = intval($total);
$config['per_page'] = $pagesize;
$config['uri_segment'] = 1;
$config['num_links'] = 3;
$config['underline_uri_seg'] = 1; //下劃線uri中頁數所在的位置
$this->hpages->init($config);
$this->template['lists'] = $list;
$this->template['pagestr'] = $this->hpages->create_links(1);

php教程文件代碼

<?php if (! defined('basepath')) exit('access denied!');
/**
* file_name : hpages.php
* 浩海網絡 前台 分頁類
*
* @package                haohailuo
* @author                by laurence.xu <[email protected]>
* @copyright        copyright (c) 2010, haohailuo, inc.
* @link                http://www.haohailuo.com
* @since                version 1.0 $id$
* @version                wed dec 08 12:21:17 cst 2010
* @filesource
*/
class hpages {

        var $base_url                        = '';        //基本鏈接地址
        var $total_rows                  = '';        //數據總數
        var $per_page                         = 10;        //每頁條數
        var $num_links                        =  2;        //要顯示的左右鏈接的個數
        var $cur_page                         =  1;        //當前頁數
        var $first_link                   = '‹ first';        //首頁字符
        var $next_link                        = '>';                        //下一頁的字符
        var $prev_link                        = '<';                        //上一頁的字符
        var $last_link                        = 'last ›';        //末頁的字符
        var $uri_segment                = 3;                //分頁數所在的uri片段位置
        var $full_tag_open                = '';                //分頁區域開始的html標簽
        var $full_tag_close                = '';                //分頁區域結束的後html標簽
        var $first_tag_open                = '';                //首頁開始的html標簽
        var $first_tag_close        = ' ';        //首頁結束的html標簽
        var $last_tag_open                = ' ';        //末頁開始的html標簽
        var $last_tag_close                = '';                //末頁結束的html標簽
        var $cur_tag_open                = ' <b>';//當前頁開始的...
        var $cur_tag_close                = '</b>';        //當前頁結束的...
        var $next_tag_open                = ' ';        //下一頁開始的.....
        var $next_tag_close                = ' ';        //下一頁結束的.....
        var $prev_tag_open                = ' ';        //上一頁開始的.....
        var $prev_tag_close                = '';                //上一頁結束的.....
        var $num_tag_open                = ' ';        //“數字”鏈接的打開標簽。
        var $num_tag_close                = '';                //“數字”鏈接的結束標簽。
        var $page_query_string        = false;
        var $query_string_segment = 'per_page';
       
        var $page_mode                        = 'default';        //default for add page at the end? if include {page}, will replace it for current page.
        var $underline_uri_seg        = -1;                        //存在下劃線時,頁碼所在數組下標位置
        var $custom_cur_page        = 0;                        //自定義當前頁碼,存在此值是,系統將不自動判斷當前頁數,默認不啟用
       
        function __construct() {
                $this->hpages();
        }
        /**
         * constructor
         *
         * @access        public
         */
        function hpages() {
                if (file_exists(apppath.'config/pagination.php')) {
                        require_once(apppath.'config/pagination.php');
                       
                        foreach ($config as $key=>$val) {
                                $this->{$key} = $val;
                        }
                }
               
                log_message('debug', "hpages class initialized");
        }
       
        /**
         * 初始化參數
         *
         * @see                init()
         * @author        laurence.xu <[email protected]>
         * @version        wed dec 08 12:26:07 cst 2010
         * @param        <array> $params 待初始化的參數
        */
        function init($params = array()) {
                if (count($params) > 0) {
                        foreach ($params as $key => $val) {
                                if (isset($this->$key)) {
                                        $this->$key = $val;
                                }
                        }               
                }
        }
       
        /**
         * 創建分頁鏈接
         *
         * @see                create_links()
         * @author        laurence.xu <[email protected]>
         * @version        wed dec 08 15:02:27 cst 2010
         * @param        <boolean> $show_info 是否顯示總條數等信息
         * @return        <string> $output
        */
        function create_links($show_info = false, $top_info = false) {
                //如果沒有記錄或者每頁條數為0,則返回空
                if ($this->total_rows == 0 || $this->per_page == 0) {
                        return '';
                }

                //計算總頁數
                $num_pages = ceil($this->total_rows / $this->per_page);

                //只有一頁,返回空
                if ($num_pages == 1 && !$show_info) {
                        return '';
                }
               
                $ci =& get_instance();

                //獲取當前頁編號
                if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
                        if ($ci->input->get($this->query_string_segment) != 0) {
                                $this->cur_page = $ci->input->get($this->query_string_segment);

                                // prep the current page - no funny business!
                                $this->cur_page = (int) $this->cur_page;
                        }
                } else {
                        if (intval($this->custom_cur_page) > 0) {
                                $this->cur_page = (int) $this->custom_cur_page;
                        }else{
                                $uri_segment = $ci->uri->segment($this->uri_segment, 0);
                                if ( !empty($uri_segment) ) {
                                        $this->cur_page = $uri_segment;
                                        //如果有下劃線
                                        if ($this->underline_uri_seg >= 0) {
                                                if (strpos($this->cur_page, '-') !== false) {
                                                        $arr = explode('-', $this->cur_page);
                                                }else {
                                                        $arr = explode('_', $this->cur_page);
                                                }
                                                $this->cur_page = $arr[$this->underline_uri_seg];
                                                unset($arr);
                                        }
                                        // prep the current page - no funny business!
                                        $this->cur_page = (int) $this->cur_page;
                                }
                        }
                }
                //echo $this->cur_page;exit;
                //左右顯示的頁碼個數
                $this->num_links = (int)$this->num_links;

                if ($this->num_links < 1) {
                        show_error('your number of links must be a positive number.');
                }

                if ( ! is_numeric($this->cur_page) || $this->cur_page < 1) {
                        $this->cur_page = 1;
                }
               
                //如果當前頁數大於總頁數,則賦值給當前頁數最大值
                if ($this->cur_page > $num_pages) {
                        $this->cur_page = $num_pages;
                }

                $uri_page_number = $this->cur_page;

                if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
                        $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
                } else {
                        $this->base_url = rtrim($this->base_url, '/') .'/';
                }
               
                if (strpos($this->base_url, "{page}") !== false) {
                        $this->page_mode = 'replace';
                }
               
                $output = $top_output = '';
                //數據總量信息
                if ($show_info) {
                        $output = " 共<b>".$this->total_rows ."</b>條記錄 <span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>/<b>".$num_pages."</b>頁 每頁<b>{$this->per_page}</b>條 ";
                }
                //數據信息,顯示在上面,以供提醒
                if ($top_info) {
                        $top_output = " 共 <b>".$this->total_rows ."</b> 條記錄 第<span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>頁/共<b>".$num_pages."</b>頁 ";
                }
                //判斷是否要顯示首頁
                if  ($this->cur_page > $this->num_links+1) {
                        $output .= $this->first_tag_open.'<a href="'.$this->makelink().'">'.$this->first_link.'</a>'.$this->first_tag_close;
                }
               
                //顯示上一頁
                if  ($this->cur_page != 1) {
                        $j = $this->cur_page - 1;
                        if ($j == 0) $j = '';
                        $output .= $this->prev_tag_open.'<a href="'.$this->makelink($j).'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
                }
               
                //顯示中間頁
                for ($i=1; $i <= $num_pages; $i++){
                        if ($i < $this->cur_page-$this->num_links || $i > $this->cur_page+$this->num_links) {
                                continue;
                        }
                       
                        //顯示中間頁數
                        if($this->cur_page == $i){
                                $output .= $this->cur_tag_open.$i.$this->cur_tag_close; //當前頁
                        }else {
                                $output .= $this->num_tag_open.'<a href="'.$this->makelink($i).'">'.$i.'</a>'.$this->num_tag_close;
                        }
                }
               
                //顯示下一頁
                if  ($this->cur_page < $num_pages) {
                        $k = $this->cur_page + 1;
                        $output .= $this->next_tag_open.'<a href="'.$this->makelink($k).'">'.$this->next_link.'</a>'.$this->next_tag_close;
                }
               
                //顯示尾頁
                if (($this->cur_page + $this->num_links) < $num_pages) {
                        $output .= $this->last_tag_open.'<a href="'.$this->makelink($num_pages).'">'.$this->last_link.'</a>'.$this->last_tag_close;
                }

                $output = preg_replace("#([^:])//+#", "1/", $output);

                // add the wrapper html if exists
                $output = $this->full_tag_open.$output.$this->full_tag_close;

                if ($top_info) {
                        return array($output, $top_output);
                }else {
                        return $output;
                }
        }
       
        /**
         * 創建鏈接url地址
         *
         * @param <string> $str
         * @return <string>
         */
        function makelink($str = '') {
                if($this->page_mode == 'default') {
                        return $this->_forsearch($this->base_url.$str);
                } else {
                        $url = $this->base_url;
                        if ($str == 1) {
                                $url = str_replace('/{page}', '', $this->base_url);
                        }
                        $url = str_replace("{page}", $str, $url);
                       
                        return $this->_forsearch($url);
                }
        }
       
        /**
         * 處理url地址
         *
         * @see                _forsearch()
         * @author        laurence.xu <[email protected]>
         * @version        wed dec 08 14:33:58 cst 2010
         * @param        <string> $string pinfo
         * @return        <string>
        */
        function _forsearch($string) {
                $length = strlen($string) - 1;
                if($string{$length} == '/') {
                        $string = rtrim($string, '/');
                }
               
                return site_url($string);
                return $string;
        }
}

// end pagination class

/* end of file hpages.php */
/* location: ./system/libraries/hpages.php */


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved