Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Pagination Class |
| 3 | ################ |
| 4 | |
| 5 | CodeIgniter's Pagination class is very easy to use, and it is 100% |
| 6 | customizable, either dynamically or via stored preferences. |
| 7 | |
| 8 | If you are not familiar with the term "pagination", it refers to links |
| 9 | that allows you to navigate from page to page, like this:: |
| 10 | |
| 11 | « First < 1 2 3 4 5 > Last » |
| 12 | |
| 13 | ******* |
| 14 | Example |
| 15 | ******* |
| 16 | |
| 17 | Here is a simple example showing how to create pagination in one of your |
| 18 | :doc:`controller <../general/controllers>` functions:: |
| 19 | |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 20 | $this->load->library('pagination'); |
| 21 | |
| 22 | $config['base_url'] = 'http://example.com/index.php/test/page/'; |
| 23 | $config['total_rows'] = 200; |
Marco Monteiro | 4b84d62 | 2012-06-26 11:53:20 +0100 | [diff] [blame] | 24 | $config['per_page'] = 20; |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 25 | |
Marco Monteiro | 4b84d62 | 2012-06-26 11:53:20 +0100 | [diff] [blame] | 26 | $this->pagination->initialize($config); |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 27 | |
| 28 | echo $this->pagination->create_links(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | |
| 30 | Notes |
| 31 | ===== |
| 32 | |
| 33 | The $config array contains your configuration variables. It is passed to |
| 34 | the $this->pagination->initialize function as shown above. Although |
| 35 | there are some twenty items you can configure, at minimum you need the |
| 36 | three shown. Here is a description of what those items represent: |
| 37 | |
| 38 | - **base_url** This is the full URL to the controller class/function |
| 39 | containing your pagination. In the example above, it is pointing to a |
| 40 | controller called "Test" and a function called "page". Keep in mind |
| 41 | that you can :doc:`re-route your URI <../general/routing>` if you |
| 42 | need a different structure. |
| 43 | - **total_rows** This number represents the total rows in the result |
| 44 | set you are creating pagination for. Typically this number will be |
| 45 | the total rows that your database query returned. |
| 46 | - **per_page** The number of items you intend to show per page. In the |
| 47 | above example, you would be showing 20 items per page. |
| 48 | |
| 49 | The create_links() function returns an empty string when there is no |
| 50 | pagination to show. |
| 51 | |
| 52 | Setting preferences in a config file |
| 53 | ==================================== |
| 54 | |
| 55 | If you prefer not to set preferences using the above method, you can |
| 56 | instead put them into a config file. Simply create a new file called |
| 57 | pagination.php, add the $config array in that file. Then save the file |
| 58 | in: config/pagination.php and it will be used automatically. You will |
| 59 | NOT need to use the $this->pagination->initialize function if you save |
| 60 | your preferences in a config file. |
| 61 | |
| 62 | ************************** |
| 63 | Customizing the Pagination |
| 64 | ************************** |
| 65 | |
| 66 | The following is a list of all the preferences you can pass to the |
| 67 | initialization function to tailor the display. |
| 68 | |
| 69 | $config['uri_segment'] = 3; |
| 70 | ============================ |
| 71 | |
| 72 | The pagination function automatically determines which segment of your |
| 73 | URI contains the page number. If you need something different you can |
| 74 | specify it. |
| 75 | |
| 76 | $config['num_links'] = 2; |
| 77 | ========================== |
| 78 | |
| 79 | The number of "digit" links you would like before and after the selected |
| 80 | page number. For example, the number 2 will place two digits on either |
| 81 | side, as in the example links at the very top of this page. |
| 82 | |
Bo-Yi Wu | 65bdaf5 | 2012-09-21 13:49:09 +0800 | [diff] [blame] | 83 | $config['use_page_numbers'] = TRUE; |
Andrey Andreev | 73766c2 | 2012-10-05 20:32:14 +0300 | [diff] [blame] | 84 | =================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
| 86 | By default, the URI segment will use the starting index for the items |
| 87 | you are paginating. If you prefer to show the the actual page number, |
| 88 | set this to TRUE. |
| 89 | |
| 90 | $config['page_query_string'] = TRUE; |
| 91 | ==================================== |
| 92 | |
| 93 | By default, the pagination library assume you are using :doc:`URI |
| 94 | Segments <../general/urls>`, and constructs your links something |
| 95 | like |
| 96 | |
| 97 | :: |
| 98 | |
| 99 | http://example.com/index.php/test/page/20 |
| 100 | |
| 101 | |
| 102 | If you have $config['enable_query_strings'] set to TRUE your links |
| 103 | will automatically be re-written using Query Strings. This option can |
| 104 | also be explictly set. Using $config['page_query_string'] set to TRUE, |
| 105 | the pagination link will become. |
| 106 | |
| 107 | :: |
| 108 | |
| 109 | http://example.com/index.php?c=test&m=page&per_page=20 |
| 110 | |
| 111 | |
| 112 | Note that "per_page" is the default query string passed, however can be |
| 113 | configured using $config['query_string_segment'] = 'your_string' |
| 114 | |
Phil Sturgeon | f82b929 | 2012-06-23 15:49:23 +0100 | [diff] [blame] | 115 | $config['reuse_query_string'] = FALSE; |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 116 | ====================================== |
Phil Sturgeon | f82b929 | 2012-06-23 15:49:23 +0100 | [diff] [blame] | 117 | |
Marco Monteiro | 4b84d62 | 2012-06-26 11:53:20 +0100 | [diff] [blame] | 118 | By default your Query String arguments (nothing to do with other |
| 119 | query string options) will be ignored. Setting this config to |
| 120 | TRUE will add existing query string arguments back into the |
Phil Sturgeon | f82b929 | 2012-06-23 15:49:23 +0100 | [diff] [blame] | 121 | URL after the URI segment and before the suffix |
| 122 | |
| 123 | :: |
| 124 | |
| 125 | http://example.com/index.php/test/page/20?query=search%term |
| 126 | |
| 127 | This helps you mix together normal :doc:`URI Segments <../general/urls>` |
| 128 | as well as query string arguments, which until 3.0 was not possible. |
| 129 | |
Marco Monteiro | 4b84d62 | 2012-06-26 11:53:20 +0100 | [diff] [blame] | 130 | $config['prefix'] = ''; |
| 131 | ================================== |
| 132 | |
| 133 | A custom prefix added to the path. The prefix value will be right before |
| 134 | the offset segment. |
| 135 | |
| 136 | $config['suffix'] = ''; |
| 137 | ================================== |
| 138 | |
| 139 | A custom suffix added to the path. The sufix value will be right after |
| 140 | the offset segment. |
| 141 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | *********************** |
| 143 | Adding Enclosing Markup |
| 144 | *********************** |
| 145 | |
| 146 | If you would like to surround the entire pagination with some markup you |
| 147 | can do it with these two prefs: |
| 148 | |
| 149 | $config['full_tag_open'] = '<p>'; |
| 150 | =================================== |
| 151 | |
| 152 | The opening tag placed on the left side of the entire result. |
| 153 | |
| 154 | $config['full_tag_close'] = '</p>'; |
| 155 | ===================================== |
| 156 | |
| 157 | The closing tag placed on the right side of the entire result. |
| 158 | |
| 159 | ************************** |
| 160 | Customizing the First Link |
| 161 | ************************** |
| 162 | |
| 163 | $config['first_link'] = 'First'; |
| 164 | ================================= |
| 165 | |
| 166 | The text you would like shown in the "first" link on the left. If you do |
| 167 | not want this link rendered, you can set its value to FALSE. |
| 168 | |
| 169 | $config['first_tag_open'] = '<div>'; |
| 170 | ====================================== |
| 171 | |
| 172 | The opening tag for the "first" link. |
| 173 | |
| 174 | $config['first_tag_close'] = '</div>'; |
| 175 | ======================================== |
| 176 | |
| 177 | The closing tag for the "first" link. |
| 178 | |
| 179 | ************************* |
| 180 | Customizing the Last Link |
| 181 | ************************* |
| 182 | |
| 183 | $config['last_link'] = 'Last'; |
| 184 | =============================== |
| 185 | |
| 186 | The text you would like shown in the "last" link on the right. If you do |
| 187 | not want this link rendered, you can set its value to FALSE. |
| 188 | |
| 189 | $config['last_tag_open'] = '<div>'; |
| 190 | ===================================== |
| 191 | |
| 192 | The opening tag for the "last" link. |
| 193 | |
| 194 | $config['last_tag_close'] = '</div>'; |
| 195 | ======================================= |
| 196 | |
| 197 | The closing tag for the "last" link. |
| 198 | |
| 199 | *************************** |
| 200 | Customizing the "Next" Link |
| 201 | *************************** |
| 202 | |
| 203 | $config['next_link'] = '>'; |
| 204 | =============================== |
| 205 | |
| 206 | The text you would like shown in the "next" page link. If you do not |
| 207 | want this link rendered, you can set its value to FALSE. |
| 208 | |
| 209 | $config['next_tag_open'] = '<div>'; |
| 210 | ===================================== |
| 211 | |
| 212 | The opening tag for the "next" link. |
| 213 | |
| 214 | $config['next_tag_close'] = '</div>'; |
| 215 | ======================================= |
| 216 | |
| 217 | The closing tag for the "next" link. |
| 218 | |
| 219 | ******************************* |
| 220 | Customizing the "Previous" Link |
| 221 | ******************************* |
| 222 | |
| 223 | $config['prev_link'] = '<'; |
| 224 | =============================== |
| 225 | |
| 226 | The text you would like shown in the "previous" page link. If you do not |
| 227 | want this link rendered, you can set its value to FALSE. |
| 228 | |
| 229 | $config['prev_tag_open'] = '<div>'; |
| 230 | ===================================== |
| 231 | |
| 232 | The opening tag for the "previous" link. |
| 233 | |
| 234 | $config['prev_tag_close'] = '</div>'; |
| 235 | ======================================= |
| 236 | |
| 237 | The closing tag for the "previous" link. |
| 238 | |
| 239 | *********************************** |
| 240 | Customizing the "Current Page" Link |
| 241 | *********************************** |
| 242 | |
| 243 | $config['cur_tag_open'] = '<b>'; |
| 244 | ================================== |
| 245 | |
| 246 | The opening tag for the "current" link. |
| 247 | |
| 248 | $config['cur_tag_close'] = '</b>'; |
| 249 | ==================================== |
| 250 | |
| 251 | The closing tag for the "current" link. |
| 252 | |
| 253 | **************************** |
| 254 | Customizing the "Digit" Link |
| 255 | **************************** |
| 256 | |
| 257 | $config['num_tag_open'] = '<div>'; |
| 258 | ==================================== |
| 259 | |
| 260 | The opening tag for the "digit" link. |
| 261 | |
| 262 | $config['num_tag_close'] = '</div>'; |
| 263 | ====================================== |
| 264 | |
| 265 | The closing tag for the "digit" link. |
| 266 | |
| 267 | **************** |
| 268 | Hiding the Pages |
| 269 | **************** |
| 270 | |
| 271 | If you wanted to not list the specific pages (for example, you only want |
| 272 | "next" and "previous" links), you can suppress their rendering by |
| 273 | adding:: |
| 274 | |
| 275 | $config['display_pages'] = FALSE; |
| 276 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 277 | **************************** |
| 278 | Adding attributes to anchors |
| 279 | **************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 280 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 281 | If you want to add an extra attribute to be added to every link rendered |
| 282 | by the pagination class, you can set them as key/value pairs in the |
| 283 | "attributes" config |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 284 | |
| 285 | :: |
| 286 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 287 | // Produces: class="myclass" |
| 288 | $config['attributes'] = array('class' => 'myclass'); |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 289 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 290 | .. note:: Usage of the old method of setting classes via "anchor_class" |
| 291 | is deprecated. |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 292 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 293 | ***************************** |
| 294 | Disabling the "rel" attribute |
| 295 | ***************************** |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 296 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 297 | By default the rel attribute is dynamically generated and appended to |
| 298 | the appropriate anchors. If for some reason you want to turn it off, |
| 299 | you can pass boolean FALSE as a regular attribute |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 300 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 301 | :: |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 302 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 303 | $config['attributes']['rel'] = FALSE; |