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;
- $this->hpages->init($config);
- $this->template['lists'] = $list;
- $this->template['pagestr'] = $this->hpages->create_links(1);
php文件代码,实例如下:
- <?php if (! defined('basepath')) exit('access denied!');
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
- var $full_tag_open = '';
- var $full_tag_close = '';
- var $first_tag_open = '';
- var $first_tag_close = ' ';
- var $last_tag_open = ' ';
- var $last_tag_close = '';
- 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';
- var $underline_uri_seg = -1;
- var $custom_cur_page = 0;
-
- function __construct() {
- $this->hpages();
- }
-
-
-
-
-
- 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");
- }
-
-
-
-
-
-
-
-
-
- function init($params = array()) {
- if (count($params) > 0) {
- foreach ($params as $key => $val) {
- if (isset($this->$key)) {
- $this->$key = $val;
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- function create_links($show_info = false, $top_info = false) {
-
- 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);
-
- $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 ( !emptyempty($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);
- }
-
- $this->cur_page = (int) $this->cur_page;
- }
- }
- }
-
-
- $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);
-
- $output = $this->full_tag_open.$output.$this->full_tag_close;
- if ($top_info) {
- return array($output, $top_output);
- }else {
- return $output;
- }
- }
-
-
-
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
-
-
-
-
-
- function _forsearch($string) {
- $length = strlen($string) - 1;
- if($string{$length} == '/') {
- $string = rtrim($string, '/');
- }
-
- return site_url($string);
- return $string;
- }
- }
-
-
-
|