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