用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

php codeigniter框架分页类

时间:2015-01-23  来源:西部数据  作者:西部数据

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. //开源代码phpfensi.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 */ 
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
    无相关信息
栏目更新
栏目热门