国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php codeigniter框架分頁類

2024-05-04 23:06:55
字體:
來源:轉載
供稿:網友

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

調用方法,代碼如下:

  1. //創建分頁 
  2. $config = array(); 
  3. $this->load->library('hpages'); 
  4. $config['base_url'] = "channel/lists/c{$slug}/{page}"
  5. $config['total_rows'] = intval($total); 
  6. $config['per_page'] = $pagesize
  7. //開源代碼Vevb.com 
  8. $config['uri_segment'] = 1; 
  9. $config['num_links'] = 3; 
  10. $config['underline_uri_seg'] = 1; //下劃線uri中頁數所在的位置 
  11. $this->hpages->init($config);  
  12. $this->template['lists'] = $list
  13. $this->template['pagestr'] = $this->hpages->create_links(1); 

php文件代碼,實例如下:

  1. <?php if (! defined('basepath')) exit('access denied!'); 
  2. /** 
  3. * file_name : hpages.php 
  4. * 浩海網絡 前臺 分頁類 
  5. * 
  6. * @package                haohailuo 
  7. * @author                by laurence.xu <haohailuo@163.com> 
  8. * @copyright        copyright (c) 2010, haohailuo, inc. 
  9. * @link                http://www.haohailuo.com 
  10. * @since                version 1.0 $id$ 
  11. * @version                wed dec 08 12:21:17 cst 2010 
  12. * @filesource 
  13. */ 
  14. class hpages { 
  15.         var $base_url                        = '';        //基本鏈接地址 
  16.         var $total_rows                  = '';        //數據總數 
  17.         var $per_page                         = 10;        //每頁條數 
  18.         var $num_links                        =  2;        //要顯示的左右鏈接的個數 
  19.         var $cur_page                         =  1;        //當前頁數 
  20.         var $first_link                   = '‹ first';        //首頁字符 
  21.         var $next_link                        = '>';                        //下一頁的字符 
  22.         var $prev_link                        = '<';                        //上一頁的字符 
  23.         var $last_link                        = 'last ›';        //末頁的字符 
  24.         var $uri_segment                = 3;                //分頁數所在的uri片段位置 
  25.         var $full_tag_open                = '';                //分頁區域開始的html標簽 
  26.         var $full_tag_close                = '';                //分頁區域結束的后html標簽 
  27.         var $first_tag_open                = '';                //首頁開始的html標簽 
  28.         var $first_tag_close        = ' ';        //首頁結束的html標簽 
  29.         var $last_tag_open                = ' ';        //末頁開始的html標簽 
  30.         var $last_tag_close                = '';                //末頁結束的html標簽 
  31.         var $cur_tag_open                = ' <b>';//當前頁開始的... 
  32.         var $cur_tag_close                = '</b>';        //當前頁結束的... 
  33.         var $next_tag_open                = ' ';        //下一頁開始的..... 
  34.         var $next_tag_close                = ' ';        //下一頁結束的..... 
  35.         var $prev_tag_open                = ' ';        //上一頁開始的..... 
  36.         var $prev_tag_close                = '';                //上一頁結束的..... 
  37.         var $num_tag_open                = ' ';        //“數字”鏈接的打開標簽。 
  38.         var $num_tag_close                = '';                //“數字”鏈接的結束標簽。 
  39.         var $page_query_string        = false; 
  40.         var $query_string_segment = 'per_page'
  41.          
  42.         var $page_mode                        = 'default';        //default for add page at the end? if include {page}, will replace it for current page. 
  43.         var $underline_uri_seg        = -1;                        //存在下劃線時,頁碼所在數組下標位置 
  44.         var $custom_cur_page        = 0;                        //自定義當前頁碼,存在此值是,系統將不自動判斷當前頁數,默認不啟用 
  45.          
  46.         function __construct() { 
  47.                 $this->hpages(); 
  48.         } 
  49.         /** 
  50.          * constructor 
  51.          * 
  52.          * @access        public 
  53.          */ 
  54.         function hpages() { 
  55.                 if (file_exists(apppath.'config/pagination.php')) { 
  56.                         require_once(apppath.'config/pagination.php'); 
  57.                          
  58.                         foreach ($config as $key=>$val) { 
  59.                                 $this->{$key} = $val
  60.                         } 
  61.                 } 
  62.                  
  63.                 log_message('debug'"hpages class initialized"); 
  64.         } 
  65.          
  66.         /** 
  67.          * 初始化參數 
  68.          * 
  69.          * @see                init() 
  70.          * @author        laurence.xu <haohailuo@163.com> 
  71.          * @version        wed dec 08 12:26:07 cst 2010 
  72.          * @param        <array> $params 待初始化的參數 
  73.         */ 
  74.         function init($params = array()) { 
  75.                 if (count($params) > 0) { 
  76.                         foreach ($params as $key => $val) { 
  77.                                 if (isset($this->$key)) { 
  78.                                         $this->$key = $val
  79.                                 } 
  80.                         }                 
  81.                 } 
  82.         } 
  83.          
  84.         /** 
  85.          * 創建分頁鏈接 
  86.          * 
  87.          * @see                create_links() 
  88.          * @author        laurence.xu <haohailuo@163.com> 
  89.          * @version        wed dec 08 15:02:27 cst 2010 
  90.          * @param        <boolean> $show_info 是否顯示總條數等信息 
  91.          * @return        <string> $output 
  92.         */ 
  93.         function create_links($show_info = false, $top_info = false) { 
  94.                 //如果沒有記錄或者每頁條數為0,則返回空 
  95.                 if ($this->total_rows == 0 || $this->per_page == 0) { 
  96.                         return ''
  97.                 } 
  98.                 //計算總頁數 
  99.                 $num_pages = ceil($this->total_rows / $this->per_page); 
  100.                 //只有一頁,返回空 
  101.                 if ($num_pages == 1 && !$show_info) { 
  102.                         return ''
  103.                 } 
  104.                  
  105.                 $ci =& get_instance(); 
  106.                 //獲取當前頁編號 
  107.                 if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) { 
  108.                         if ($ci->input->get($this->query_string_segment) != 0) { 
  109.                                 $this->cur_page = $ci->input->get($this->query_string_segment); 
  110.                                 // prep the current page - no funny business! 
  111.                                 $this->cur_page = (int) $this->cur_page; 
  112.                         } 
  113.                 } else { 
  114.                         if (intval($this->custom_cur_page) > 0) { 
  115.                                 $this->cur_page = (int) $this->custom_cur_page; 
  116.                         }else
  117.                                 $uri_segment = $ci->uri->segment($this->uri_segment, 0); 
  118.                                 if ( !emptyempty($uri_segment) ) { 
  119.                                         $this->cur_page = $uri_segment
  120.                                         //如果有下劃線 
  121.                                         if ($this->underline_uri_seg >= 0) { 
  122.                                                 if (strpos($this->cur_page, '-') !== false) { 
  123.                                                         $arr = explode('-'$this->cur_page); 
  124.                                                 }else { 
  125.                                                         $arr = explode('_'$this->cur_page); 
  126.                                                 } 
  127.                                                 $this->cur_page = $arr[$this->underline_uri_seg]; 
  128.                                                 unset($arr); 
  129.                                         } 
  130.                                         // prep the current page - no funny business! 
  131.                                         $this->cur_page = (int) $this->cur_page; 
  132.                                 } 
  133.                         } 
  134.                 } 
  135.                 //echo $this->cur_page;exit; 
  136.                 //左右顯示的頁碼個數 
  137.                 $this->num_links = (int)$this->num_links; 
  138.                 if ($this->num_links < 1) { 
  139.                         show_error('your number of links must be a positive number.'); 
  140.                 } 
  141.                 if ( ! is_numeric($this->cur_page) || $this->cur_page < 1) { 
  142.                         $this->cur_page = 1; 
  143.                 } 
  144.                  
  145.                 //如果當前頁數大于總頁數,則賦值給當前頁數最大值 
  146.                 if ($this->cur_page > $num_pages) { 
  147.                         $this->cur_page = $num_pages
  148.                 } 
  149.                 $uri_page_number = $this->cur_page; 
  150.                 if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) { 
  151.                         $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'='
  152.                 } else { 
  153.                         $this->base_url = rtrim($this->base_url, '/') .'/'
  154.                 } 
  155.                  
  156.                 if (strpos($this->base_url, "{page}") !== false) { 
  157.                         $this->page_mode = 'replace'
  158.                 } 
  159.                  
  160.                 $output = $top_output = ''
  161.                 //數據總量信息 
  162.                 if ($show_info) { 
  163.                         $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>條 "
  164.                 } 
  165.                 //數據信息,顯示在上面,以供提醒 
  166.                 if ($top_info) { 
  167.                         $top_output = " 共 <b>".$this->total_rows ."</b> 條記錄 第<span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>頁/共<b>".$num_pages."</b>頁 "
  168.                 } 
  169.                 //判斷是否要顯示首頁 
  170.                 if  ($this->cur_page > $this->num_links+1) { 
  171.                         $output .= $this->first_tag_open.'<a href="'.$this->makelink().'">'.$this->first_link.'</a>'.$this->first_tag_close; 
  172.                 } 
  173.                  
  174.                 //顯示上一頁 
  175.                 if  ($this->cur_page != 1) { 
  176.                         $j = $this->cur_page - 1; 
  177.                         if ($j == 0) $j = ''
  178.                         $output .= $this->prev_tag_open.'<a href="'.$this->makelink($j).'">'.$this->prev_link.'</a>'.$this->prev_tag_close; 
  179.                 } 
  180.                  
  181.                 //顯示中間頁 
  182.                 for ($i=1; $i <= $num_pages$i++){ 
  183.                         if ($i < $this->cur_page-$this->num_links || $i > $this->cur_page+$this->num_links) { 
  184.                                 continue
  185.                         } 
  186.                          
  187.                         //顯示中間頁數 
  188.                         if($this->cur_page == $i){ 
  189.                                 $output .= $this->cur_tag_open.$i.$this->cur_tag_close; //當前頁 
  190.                         }else { 
  191.                                 $output .= $this->num_tag_open.'<a href="'.$this->makelink($i).'">'.$i.'</a>'.$this->num_tag_close; 
  192.                         } 
  193.                 } 
  194.                  
  195.                 //顯示下一頁 
  196.                 if  ($this->cur_page < $num_pages) { 
  197.                         $k = $this->cur_page + 1; 
  198.                         $output .= $this->next_tag_open.'<a href="'.$this->makelink($k).'">'.$this->next_link.'</a>'.$this->next_tag_close; 
  199.                 } 
  200.                  
  201.                 //顯示尾頁 
  202.                 if (($this->cur_page + $this->num_links) < $num_pages) { 
  203.                         $output .= $this->last_tag_open.'<a href="'.$this->makelink($num_pages).'">'.$this->last_link.'</a>'.$this->last_tag_close; 
  204.                 } 
  205.                 $output = preg_replace("#([^:])//+#""1/"$output); 
  206.                 // add the wrapper html if exists 
  207.                 $output = $this->full_tag_open.$output.$this->full_tag_close; 
  208.                 if ($top_info) { 
  209.                         return array($output$top_output); 
  210.                 }else { 
  211.                         return $output
  212.                 } 
  213.         } 
  214.          
  215.         /** 
  216.          * 創建鏈接url地址 
  217.          * 
  218.          * @param <string> $str 
  219.          * @return <string> 
  220.          */ 
  221.         function makelink($str = '') { 
  222.                 if($this->page_mode == 'default') { 
  223.                         return $this->_forsearch($this->base_url.$str); 
  224.                 } else { 
  225.                         $url = $this->base_url; 
  226.                         if ($str == 1) { 
  227.                                 $url = str_replace('/{page}'''$this->base_url); 
  228.                         } 
  229.                         $url = str_replace("{page}"$str$url); 
  230.                          
  231.                         return $this->_forsearch($url); 
  232.                 } 
  233.         } 
  234.          
  235.         /** 
  236.          * 處理url地址 
  237.          * 
  238.          * @see                _forsearch() 
  239.          * @author        laurence.xu <haohailuo@163.com> 
  240.          * @version        wed dec 08 14:33:58 cst 2010 
  241.          * @param        <string> $string pinfo 
  242.          * @return        <string> 
  243.         */ 
  244.         function _forsearch($string) { 
  245.                 $length = strlen($string) - 1; 
  246.                 if($string{$length} == '/') { 
  247.                         $string = rtrim($string'/'); 
  248.                 } 
  249.                  
  250.                 return site_url($string); 
  251.                 return $string
  252.         } 
  253. // end pagination class 
  254. /* end of file hpages.php */ 
  255. /* location: ./system/libraries/hpages.php */ 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汉阴县| 清水河县| 舟山市| 泰安市| 淮南市| 土默特左旗| 南平市| 晋宁县| 历史| 百色市| 洪湖市| 监利县| 安溪县| 渝中区| 新龙县| 兴和县| 西盟| 即墨市| 仁布县| 海宁市| 利川市| 甘泉县| 汽车| 海门市| 盱眙县| 同江市| 永丰县| 依兰县| 黄浦区| 柳州市| 元阳县| 当阳市| 沈阳市| 嫩江县| 边坝县| 隆安县| 得荣县| 庆阳市| 葵青区| 将乐县| 宝坻区|