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; |
| 24 | $config['per_page'] = 20; |
| 25 | |
| 26 | $this->pagination->initialize($config); |
| 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 | |
| 83 | $config['use_page_number'] = TRUE; |
| 84 | ================================== |
| 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 | |
| 115 | *********************** |
| 116 | Adding Enclosing Markup |
| 117 | *********************** |
| 118 | |
| 119 | If you would like to surround the entire pagination with some markup you |
| 120 | can do it with these two prefs: |
| 121 | |
| 122 | $config['full_tag_open'] = '<p>'; |
| 123 | =================================== |
| 124 | |
| 125 | The opening tag placed on the left side of the entire result. |
| 126 | |
| 127 | $config['full_tag_close'] = '</p>'; |
| 128 | ===================================== |
| 129 | |
| 130 | The closing tag placed on the right side of the entire result. |
| 131 | |
| 132 | ************************** |
| 133 | Customizing the First Link |
| 134 | ************************** |
| 135 | |
| 136 | $config['first_link'] = 'First'; |
| 137 | ================================= |
| 138 | |
| 139 | The text you would like shown in the "first" link on the left. If you do |
| 140 | not want this link rendered, you can set its value to FALSE. |
| 141 | |
| 142 | $config['first_tag_open'] = '<div>'; |
| 143 | ====================================== |
| 144 | |
| 145 | The opening tag for the "first" link. |
| 146 | |
| 147 | $config['first_tag_close'] = '</div>'; |
| 148 | ======================================== |
| 149 | |
| 150 | The closing tag for the "first" link. |
| 151 | |
| 152 | ************************* |
| 153 | Customizing the Last Link |
| 154 | ************************* |
| 155 | |
| 156 | $config['last_link'] = 'Last'; |
| 157 | =============================== |
| 158 | |
| 159 | The text you would like shown in the "last" link on the right. If you do |
| 160 | not want this link rendered, you can set its value to FALSE. |
| 161 | |
| 162 | $config['last_tag_open'] = '<div>'; |
| 163 | ===================================== |
| 164 | |
| 165 | The opening tag for the "last" link. |
| 166 | |
| 167 | $config['last_tag_close'] = '</div>'; |
| 168 | ======================================= |
| 169 | |
| 170 | The closing tag for the "last" link. |
| 171 | |
| 172 | *************************** |
| 173 | Customizing the "Next" Link |
| 174 | *************************** |
| 175 | |
| 176 | $config['next_link'] = '>'; |
| 177 | =============================== |
| 178 | |
| 179 | The text you would like shown in the "next" page link. If you do not |
| 180 | want this link rendered, you can set its value to FALSE. |
| 181 | |
| 182 | $config['next_tag_open'] = '<div>'; |
| 183 | ===================================== |
| 184 | |
| 185 | The opening tag for the "next" link. |
| 186 | |
| 187 | $config['next_tag_close'] = '</div>'; |
| 188 | ======================================= |
| 189 | |
| 190 | The closing tag for the "next" link. |
| 191 | |
| 192 | ******************************* |
| 193 | Customizing the "Previous" Link |
| 194 | ******************************* |
| 195 | |
| 196 | $config['prev_link'] = '<'; |
| 197 | =============================== |
| 198 | |
| 199 | The text you would like shown in the "previous" page link. If you do not |
| 200 | want this link rendered, you can set its value to FALSE. |
| 201 | |
| 202 | $config['prev_tag_open'] = '<div>'; |
| 203 | ===================================== |
| 204 | |
| 205 | The opening tag for the "previous" link. |
| 206 | |
| 207 | $config['prev_tag_close'] = '</div>'; |
| 208 | ======================================= |
| 209 | |
| 210 | The closing tag for the "previous" link. |
| 211 | |
| 212 | *********************************** |
| 213 | Customizing the "Current Page" Link |
| 214 | *********************************** |
| 215 | |
| 216 | $config['cur_tag_open'] = '<b>'; |
| 217 | ================================== |
| 218 | |
| 219 | The opening tag for the "current" link. |
| 220 | |
| 221 | $config['cur_tag_close'] = '</b>'; |
| 222 | ==================================== |
| 223 | |
| 224 | The closing tag for the "current" link. |
| 225 | |
| 226 | **************************** |
| 227 | Customizing the "Digit" Link |
| 228 | **************************** |
| 229 | |
| 230 | $config['num_tag_open'] = '<div>'; |
| 231 | ==================================== |
| 232 | |
| 233 | The opening tag for the "digit" link. |
| 234 | |
| 235 | $config['num_tag_close'] = '</div>'; |
| 236 | ====================================== |
| 237 | |
| 238 | The closing tag for the "digit" link. |
| 239 | |
| 240 | **************** |
| 241 | Hiding the Pages |
| 242 | **************** |
| 243 | |
| 244 | If you wanted to not list the specific pages (for example, you only want |
| 245 | "next" and "previous" links), you can suppress their rendering by |
| 246 | adding:: |
| 247 | |
| 248 | $config['display_pages'] = FALSE; |
| 249 | |
| 250 | ****************************** |
| 251 | Adding a class to every anchor |
| 252 | ****************************** |
| 253 | |
| 254 | If you want to add a class attribute to every link rendered by the |
| 255 | pagination class, you can set the config "anchor_class" equal to the |
| 256 | classname you want. |