Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Pagination Class : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 21 | |
| 22 | </head> |
| 23 | <body> |
| 24 | |
| 25 | <!-- START NAVIGATION --> |
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> |
| 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> |
| 28 | <div id="masthead"> |
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 30 | <tr> |
Pascal Kriete | 1f62229 | 2011-04-07 12:06:51 -0400 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 33 | </tr> |
| 34 | </table> |
| 35 | </div> |
| 36 | <!-- END NAVIGATION --> |
| 37 | |
| 38 | |
| 39 | <!-- START BREADCRUMB --> |
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 41 | <tr> |
| 42 | <td id="breadcrumb"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | Pagination Class |
| 46 | </td> |
| 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> |
| 48 | </tr> |
| 49 | </table> |
| 50 | <!-- END BREADCRUMB --> |
| 51 | |
| 52 | <br clear="all" /> |
| 53 | |
| 54 | |
| 55 | <!-- START CONTENT --> |
| 56 | <div id="content"> |
| 57 | |
| 58 | |
| 59 | <h1>Pagination Class</h1> |
| 60 | |
| 61 | <p>CodeIgniter's Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences.</p> |
| 62 | |
| 63 | <p>If you are not familiar with the term "pagination", it refers to links that allows you to navigate from page to page, like this:</p> |
| 64 | |
| 65 | <code><a href="#">« First</a> <a href="#"><</a> <a href="#">1</a> <a href="#">2</a> <b>3</b> <a href="#">4</a> <a href="#">5</a> <a href="#">></a> <a href="#">Last »</a></code> |
| 66 | |
| 67 | <h2>Example</h2> |
| 68 | |
| 69 | <p>Here is a simple example showing how to create pagination in one of your <a href="../general/controllers.html">controller</a> functions:</p> |
| 70 | |
| 71 | <code> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | $this->load->library('pagination');<br /><br /> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | $config['base_url'] = 'http://example.com/index.php/test/page/';<br /> |
kenjis | c0b133c | 2011-04-22 19:58:43 +0900 | [diff] [blame] | 74 | $config['total_rows'] = 200;<br /> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 75 | $config['per_page'] = 20; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | <br /><br /> |
| 77 | $this->pagination->initialize($config); |
| 78 | |
| 79 | <br /><br /> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 80 | echo $this->pagination->create_links();</code> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | |
| 82 | <h3>Notes:</h3> |
| 83 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 84 | <p>The <var>$config</var> array contains your configuration variables. It is passed to the <dfn>$this->pagination->initialize</dfn> function as shown above. Although there are some twenty items you can configure, at |
| 85 | minimum you need the three shown. Here is a description of what those items represent:</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | |
| 87 | <ul> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 88 | <li><strong>base_url</strong> This is the full URL to the controller class/function containing your pagination. In the example |
| 89 | above, it is pointing to a controller called "Test" and a function called "page". Keep in mind that you can |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | <a href="../general/routing.html">re-route your URI</a> if you need a different structure.</li> |
| 91 | <li><strong>total_rows</strong> This number represents the total rows in the result set you are creating pagination for. |
| 92 | Typically this number will be the total rows that your database query returned. |
| 93 | </li> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 94 | <li><strong>per_page</strong> The number of items you intend to show per page. In the above example, you would be showing 20 items per page.</li> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | </ul> |
| 96 | |
| 97 | <p>The <var>create_links()</var> function returns an empty string when there is no pagination to show.</p> |
| 98 | |
| 99 | |
| 100 | <h3>Setting preferences in a config file</h3> |
| 101 | |
| 102 | <p>If you prefer not to set preferences using the above method, you can instead put them into a config file. |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 103 | Simply create a new file called <var>pagination.php</var>, add the <var>$config</var> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | array in that file. Then save the file in: <var>config/pagination.php</var> and it will be used automatically. You |
| 105 | will NOT need to use the <dfn>$this->pagination->initialize</dfn> function if you save your preferences in a config file.</p> |
| 106 | |
| 107 | |
| 108 | <h2>Customizing the Pagination</h2> |
| 109 | |
| 110 | <p>The following is a list of all the preferences you can pass to the initialization function to tailor the display.</p> |
| 111 | |
| 112 | |
| 113 | <h4>$config['uri_segment'] = 3;</h4> |
| 114 | |
| 115 | <p>The pagination function automatically determines which segment of your URI contains the page number. If you need |
| 116 | something different you can specify it.</p> |
| 117 | |
| 118 | <h4>$config['num_links'] = 2;</h4> |
| 119 | |
| 120 | <p>The number of "digit" links you would like before and after the selected page number. For example, the number 2 |
| 121 | will place two digits on either side, as in the example links at the very top of this page.</p> |
| 122 | <h4>$config['page_query_string'] = TRUE</h4> |
| 123 | <p>By default, the pagination library assume you are using <a href="../general/urls.html">URI Segments</a>, and constructs your links something like</p> |
| 124 | <p><code>http://example.com/index.php/test/page/20</code></p> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 125 | <p>If you have $config['enable_query_strings'] set to TRUE your links will automatically be re-written using Query Strings. This option can also be explictly set. Using $config['page_query_string'] set to TRUE, the pagination link will become.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | <p><code>http://example.com/index.php?c=test&m=page&per_page=20</code></p> |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 127 | <p>Note that "per_page" is the default query string passed, however can be configured using $config['query_string_segment'] = 'your_string'</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | <h2>Adding Enclosing Markup</h2> |
| 129 | |
| 130 | <p>If you would like to surround the entire pagination with some markup you can do it with these two prefs:</p> |
| 131 | |
| 132 | <h4>$config['full_tag_open'] = '<p>';</h4> |
| 133 | <p>The opening tag placed on the left side of the entire result.</p> |
| 134 | |
| 135 | <h4>$config['full_tag_close'] = '</p>';</h4> |
| 136 | <p>The closing tag placed on the right side of the entire result.</p> |
| 137 | |
| 138 | |
| 139 | <h2>Customizing the First Link</h2> |
| 140 | |
| 141 | <h4>$config['first_link'] = 'First';</h4> |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 142 | <p>The text you would like shown in the "first" link on the left. If you do not want this link rendered, you can set its value to FALSE.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | |
| 144 | <h4>$config['first_tag_open'] = '<div>';</h4> |
| 145 | <p>The opening tag for the "first" link.</p> |
| 146 | |
| 147 | <h4>$config['first_tag_close'] = '</div>';</h4> |
| 148 | <p>The closing tag for the "first" link.</p> |
| 149 | |
| 150 | <h2>Customizing the Last Link</h2> |
| 151 | |
| 152 | <h4>$config['last_link'] = 'Last';</h4> |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 153 | <p>The text you would like shown in the "last" link on the right. If you do not want this link rendered, you can set its value to FALSE.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | |
| 155 | <h4>$config['last_tag_open'] = '<div>';</h4> |
| 156 | <p>The opening tag for the "last" link.</p> |
| 157 | |
| 158 | <h4>$config['last_tag_close'] = '</div>';</h4> |
| 159 | <p>The closing tag for the "last" link.</p> |
| 160 | |
| 161 | <h2>Customizing the "Next" Link</h2> |
| 162 | |
| 163 | <h4>$config['next_link'] = '&gt;';</h4> |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 164 | <p>The text you would like shown in the "next" page link. If you do not want this link rendered, you can set its value to FALSE.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | |
| 166 | <h4>$config['next_tag_open'] = '<div>';</h4> |
| 167 | <p>The opening tag for the "next" link.</p> |
| 168 | |
| 169 | <h4>$config['next_tag_close'] = '</div>';</h4> |
| 170 | <p>The closing tag for the "next" link.</p> |
| 171 | |
| 172 | <h2>Customizing the "Previous" Link</h2> |
| 173 | |
| 174 | <h4>$config['prev_link'] = '&lt;';</h4> |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 175 | <p>The text you would like shown in the "previous" page link. If you do not want this link rendered, you can set its value to FALSE.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | |
| 177 | <h4>$config['prev_tag_open'] = '<div>';</h4> |
| 178 | <p>The opening tag for the "previous" link.</p> |
| 179 | |
| 180 | <h4>$config['prev_tag_close'] = '</div>';</h4> |
| 181 | <p>The closing tag for the "previous" link.</p> |
| 182 | |
| 183 | <h2>Customizing the "Current Page" Link</h2> |
| 184 | |
| 185 | <h4>$config['cur_tag_open'] = '<b>';</h4> |
| 186 | <p>The opening tag for the "current" link.</p> |
| 187 | |
| 188 | <h4>$config['cur_tag_close'] = '</b>';</h4> |
| 189 | <p>The closing tag for the "current" link.</p> |
| 190 | |
| 191 | |
| 192 | <h2>Customizing the "Digit" Link</h2> |
| 193 | |
| 194 | <h4>$config['num_tag_open'] = '<div>';</h4> |
| 195 | <p>The opening tag for the "digit" link.</p> |
| 196 | |
| 197 | <h4>$config['num_tag_close'] = '</div>';</h4> |
| 198 | <p>The closing tag for the "digit" link.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 200 | <h2>Hiding the Pages</h2> |
| 201 | |
| 202 | <p>If you wanted to not list the specific pages (for example, you only want "next" and "previous" links), you can suppress their rendering by adding:</p> |
| 203 | |
| 204 | <code> |
| 205 | $config['display_pages'] = FALSE; |
| 206 | </code> |
| 207 | |
| 208 | |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 209 | <h2>Adding a class to every anchor</h2> |
| 210 | |
| 211 | <p>If you want to add a class attribute to every link rendered by the pagination class, you can set the config "anchor_class" equal to the classname you want.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | |
| 213 | </div> |
| 214 | <!-- END CONTENT --> |
| 215 | |
| 216 | |
| 217 | <div id="footer"> |
| 218 | <p> |
| 219 | Previous Topic: <a href="output.html">Output Class</a> |
| 220 | · |
| 221 | <a href="#top">Top of Page</a> · |
| 222 | <a href="../index.html">User Guide Home</a> · |
| 223 | Next Topic: <a href="sessions.html">Session Class</a> |
| 224 | </p> |
Derek Jones | 898949f | 2011-01-28 07:42:16 -0600 | [diff] [blame] | 225 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | </div> |
| 227 | |
| 228 | </body> |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 229 | </html> |