pagination_library.php
1 <?php
2
/**
3  * Pagination Library
4  *
5  * @author Zhou Yuan <yuanzhou19@gmail.com>
6  * @link http://www.infopotato.com/
7  * @copyright Copyright &copy; 2009-2011 Zhou Yuan
8  * @license http://www.opensource.org/licenses/mit-license.php MIT Licence
9  * @link http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/
10  */
11
class Pagination_Library {
12     
/**
13      * The desired number of items to be shown on each page.
14      * 
15      * @var integer 
16      */
17     
protected $items_per_page 10;
18     
19     
/**
20      * The total number of items you'll be paginating.
21      *
22      * @var integer
23      */
24     
protected $items_total;
25     
26     
/**
27      * The page the user is viewing. Will always be an integer >= 1
28      * 
29      * @var integer 
30      */
31     
protected $current_page 1;
32     
33     
/**
34      * The CSS class for current page
35      * 
36      * @var string
37      */
38     
protected $current_page_class '';
39     
40     
/**
41      * The number of pages to show 'around' the current page, is odd and >=3
42      * 
43      * The mid range is the number of pages that the paginator will display, 
44      * centered around and including the selected page. For example, 
45      * if the mid range is set to seven ($this->mid_range = 7;) then 
46      * when browsing page 50 of 100, the mid range generates links to 
47      * pages 47, 48, 49, 50, 51, 52, and 53. The mid range moves in relation to 
48      * the selected page. If the user is at either the low or high end of the list of pages, 
49      * it will slide  the range toward the other side to accommodate the position. 
50      * For example, if  the user visits page 99 of 100, the mid range will generate links for 
51      * pages  94, 95, 96, 97, 98, 99, and 100.
52      * 
53      * @var integer 
54      */
55     
protected $mid_range 7;
56     
57     
/**
58      * The base page URI we are linking to
59      * 
60      * @var string
61      */
62     
protected $base_uri '';
63     
64     
/**
65      * The pagination data in an array for debug
66      * 
67      * @var array()
68      */
69     
private $_pagination_data = array();
70     
71     
/**
72      * Constructor
73      */    
74     
public function __construct(array $config NULL) { 
75         if (
count($config) > 0) {
76             foreach (
$config as $key => $val) {
77                 
$this->$key $val;
78             }
79         }
80     }
81
82     
/**
83      * The build_pagination method is what determines how many page numbers to display,
84      * figures out how they should be linked, and applies CSS for styling.
85      *
86      * @return the pagination string
87      */
88     
public function build_pagination() {
89         
// The total number of pages as generated by the pagination class
90         
$num_pages ceil($this->items_total/$this->items_per_page);        
91
92         
// Create the pagination link
93         
$output '';
94             
95         if (
$num_pages 1) {
96             
// The number of pages to show 'around' the current page
97             
$start_range $this->current_page floor($this->mid_range/2);
98             
$end_range $this->current_page floor($this->mid_range/2);
99
100             if (
$start_range <= 0) {
101                 
$end_range += abs($start_range) + 1;
102                 
$start_range 1;
103             }
104             if (
$end_range $num_pages) {
105                 
$start_range -= $end_range $num_pages;
106                 
$end_range $num_pages;
107             }
108             
109             
$this->_pagination_data = array(
110                 
'base_uri' => $this->base_uri,
111                 
'items_total' => $this->items_total,
112                 
'items_per_page' => $this->items_per_page,
113                 
'mid_range' => $this->mid_range,
114                 
'current_page' => $this->current_page,
115                 
'current_page_class' => $this->current_page_class,
116                 
'prev_page' => $this->current_page 1,
117                 
'next_page' => $this->current_page 1,
118                 
'num_pages' => $num_pages,
119                 
'offset_low' => ($this->current_page 1) * $this->items_per_page,
120                 
'offset_high' => $this->current_page $this->items_per_page,
121                 
'range' => range($start_range$end_range), // Create an array containing a range of elements
122             
);
123             
124             
125             if (
$this->_pagination_data['current_page'] > 1) {
126                 
$output '<a href="'.$this->_pagination_data['base_uri'].$this->_pagination_data['prev_page'].'">&laquo;</a> ';
127             }
128         
129             for (
$i 1$i <= $this->_pagination_data['num_pages']; $i++) {
130                 if (
$this->_pagination_data['range'][0] > && $i == $this->_pagination_data['range'][0]) {
131                     
$output .= '...';
132                 }
133             
134                 if (
$i == || $i == $this->_pagination_data['num_pages'] || in_array($i$this->_pagination_data['range'])) {
135                     if (
$i === $this->_pagination_data['current_page']) {
136                         
$output .= '<span class="'.$this->_pagination_data['current_page_class'].'">'.$i.'</span>'
137                     } else {
138                         
$output .= '<a href="'.$this->_pagination_data['base_uri'].$i.'">'.$i.'</a>'
139                     }
140                 }
141             
142                 if (
$this->_pagination_data['range'][$this->_pagination_data['mid_range']-1] < $this->_pagination_data['num_pages']-&& $i == $this->_pagination_data['range'][$this->_pagination_data['mid_range']-1]) {
143                     
$output .= '...';
144                 }
145             }
146         
147             if (
$this->_pagination_data['current_page'] != $this->_pagination_data['num_pages']) {
148                 
$output .= '<a href="'.$this->_pagination_data['base_uri'].$this->_pagination_data['next_page'].'">&raquo;</a>'
149             }
150         } 
151         
152         return 
$output;
153     }
154     
155     
/**
156      * Get all the pagination metadata
157      *
158      * @return the pagination data array
159      */
160     
public function get_pagination_data() {
161         return 
$this->_pagination_data;
162     }
163 }
164
165
/* End of file: ./system/libraries/pagination/pagination_library.php */

Page URI: http://www.infopotato.com/index.php/code/library/pagination/