From f82b92999e8309155df3e665e25a261c26b0e93d Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Sat, 23 Jun 2012 15:49:23 +0100 Subject: Added ['reuse_query_string'] to Pagination. This allows automatic repopulation of query string arguments, combined with normal URI segments. --- system/libraries/Pagination.php | 98 ++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 40 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Pagination.php b/system/libraries/Pagination.php index 5a4ca8f10..75745dd48 100644 --- a/system/libraries/Pagination.php +++ b/system/libraries/Pagination.php @@ -36,39 +36,40 @@ */ class CI_Pagination { - public $base_url = ''; // The page we are linking to - public $prefix = ''; // A custom prefix added to the path. - public $suffix = ''; // A custom suffix added to the path. - public $total_rows = 0; // Total number of items (database results) - public $per_page = 10; // Max number of items you want shown per page - public $num_links = 2; // Number of "digit" links to show before/after the currently viewed page - public $cur_page = 0; // The current page being viewed - public $use_page_numbers = FALSE; // Use page number for segment instead of offset - public $first_link = '‹ First'; - public $next_link = '>'; - public $prev_link = '<'; - public $last_link = 'Last ›'; - public $uri_segment = 3; - public $full_tag_open = ''; - public $full_tag_close = ''; - public $first_tag_open = ''; - public $first_tag_close = ' '; - public $last_tag_open = ' '; - public $last_tag_close = ''; - public $first_url = ''; // Alternative URL for the First Page. - public $cur_tag_open = ' '; - public $cur_tag_close = ''; - public $next_tag_open = ' '; - public $next_tag_close = ' '; - public $prev_tag_open = ' '; - public $prev_tag_close = ''; - public $num_tag_open = ' '; - public $num_tag_close = ''; - public $page_query_string = FALSE; - public $query_string_segment = 'per_page'; - public $display_pages = TRUE; - protected $_attributes = ''; - protected $_link_types = array(); + protected $base_url = ''; // The page we are linking to + protected $prefix = ''; // A custom prefix added to the path. + protected $suffix = ''; // A custom suffix added to the path. + protected $total_rows = 0; // Total number of items (database results) + protected $per_page = 10; // Max number of items you want shown per page + protected $num_links = 2; // Number of "digit" links to show before/after the currently viewed page + protected $cur_page = 0; // The current page being viewed + protected $use_page_numbers = FALSE; // Use page number for segment instead of offset + protected $first_link = '‹ First'; + protected $next_link = '>'; + protected $prev_link = '<'; + protected $last_link = 'Last ›'; + protected $uri_segment = 3; + protected $full_tag_open = ''; + protected $full_tag_close = ''; + protected $first_tag_open = ''; + protected $first_tag_close = ' '; + protected $last_tag_open = ' '; + protected $last_tag_close = ''; + protected $first_url = ''; // Alternative URL for the First Page. + protected $cur_tag_open = ' '; + protected $cur_tag_close = ''; + protected $next_tag_open = ' '; + protected $next_tag_close = ' '; + protected $prev_tag_open = ' '; + protected $prev_tag_close = ''; + protected $num_tag_open = ' '; + protected $num_tag_close = ''; + protected $page_query_string = FALSE; + protected $query_string_segment = 'per_page'; + protected $display_pages = TRUE; + protected $_attributes = ''; + protected $_link_types = array(); + protected $reuse_query_string = FALSE; /** * Constructor @@ -222,6 +223,23 @@ class CI_Pagination { // And here we go... $output = ''; + $query_string = ''; + + // Add anything in the query string back to the links + // Note: Nothing to do with query_string_segment or any other query string options + if ($this->reuse_query_string === TRUE) + { + $get = $CI->input->get(); + + // Unset the controll, method, old-school routing options + unset($get['c'], $get['m'], $get[$this->query_string_segment]); + + // Put everything else onto the end + $query_string = (strpos($this->base_url, '&') !== FALSE ? '&' : '?') . http_build_query($get, '', '&'); + + // Add this after the suffix to put it into more links easily + $this->suffix .= $query_string; + } // Render the "First" link if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1)) @@ -232,19 +250,19 @@ class CI_Pagination { } // Render the "previous" link - if ($this->prev_link !== FALSE && $this->cur_page !== 1) + if ($this->prev_link !== FALSE && $this->cur_page !== 1) { $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page; if ($i === $base_page && $this->first_url !== '') { - $output .= $this->prev_tag_open.'_attributes.$this->_attr_rel('prev').'>' + $output .= $this->prev_tag_open.'_attributes.$this->_attr_rel('prev').'>' .$this->prev_link.''.$this->prev_tag_close; } else { - $i = ($i === $base_page) ? '' : $this->prefix.$i.$this->suffix; - $output .= $this->prev_tag_open.'_attributes.$this->_attr_rel('prev').'>' + $append = ($i === $base_page) ? $query_string : $this->prefix.$i.$this->suffix; + $output .= $this->prev_tag_open.'_attributes.$this->_attr_rel('prev').'>' .$this->prev_link.''.$this->prev_tag_close; } @@ -268,13 +286,13 @@ class CI_Pagination { $n = ($i === $base_page) ? '' : $i; if ($n === '' && ! empty($this->first_url)) { - $output .= $this->num_tag_open.'_attributes.$this->_attr_rel('start').'>' + $output .= $this->num_tag_open.'_attributes.$this->_attr_rel('start').'>' .$loop.''.$this->num_tag_close; } else { - $n = ($n === '') ? '' : $this->prefix.$n.$this->suffix; - $output .= $this->num_tag_open.'_attributes.$this->_attr_rel('start').'>' + $append = ($n === '') ? $query_string : $this->prefix.$n.$this->suffix; + $output .= $this->num_tag_open.'_attributes.$this->_attr_rel('start').'>' .$loop.''.$this->num_tag_close; } } -- cgit v1.2.3-24-g4f1b