1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="learning.paginator.simple">
4 <title>Simple Examples</title>
7 In this first example we won't do anything spectacular, but hopefully it will
8 give you a good idea of what <classname>Zend_Paginator</classname> is designed to do.
9 Let's say we have an array called $data with the numbers 1 to 100 in it, which
10 we want to divide over a number of pages. We can use the static
11 <methodname>factory()</methodname> method in the <classname>Zend_Paginator</classname>
12 class to get a <classname>Zend_Paginator</classname> object with our array in it.
15 <programlisting language="php"><![CDATA[
16 // Create an array with numbers 1 to 100
17 $data = range(1, 100);
19 // Get a Paginator object using Zend_Paginator's built-in factory.
20 $paginator = Zend_Paginator::factory($data);
24 We're already almost done! The $paginator variable now contains a reference to the
25 Paginator object. By default it is setup to display 10 items per page.
26 To display the items for the currently active page, all you need to do is iterate
27 over the Paginator object with a foreach loop. The currently active page defaults
28 to the first page if it's not explicitly specified. We will see how you can select
29 a specific page later on. The snippet below will display an unordered list containing the
30 numbers 1 to 10, which are the numbers on the first page.
33 <programlisting language="php"><![CDATA[
34 // Create an array with numbers 1 to 100
35 $data = range(1, 100);
37 // Get a Paginator object using Zend_Paginator's built-in factory.
38 $paginator = Zend_Paginator::factory($data);
42 // Render each item for the current page in a list-item
43 foreach ($paginator as $item) {
44 echo '<li>' . $item . '</li>';
51 Now let's try and render the items on the second page. You can use the
52 <methodname>setCurrentPageNumber()</methodname> method to select which page you want to
56 <programlisting language="php"><![CDATA[
57 // Create an array with numbers 1 to 100
58 $data = range(1, 100);
60 // Get a Paginator object using Zend_Paginator's built-in factory.
61 $paginator = Zend_Paginator::factory($data);
63 // Select the second page
64 $paginator->setCurrentPageNumber(2);
68 // Render each item for the current page in a list-item
69 foreach ($paginator as $item) {
70 echo '<li>' . $item . '</li>';
77 As expected, this little snippet will render an unordered list with the numbers
82 These simple examples demonstrate a small portion of what can be achieved with
83 <classname>Zend_Paginator</classname>. However, a real application rarely reads its data
84 from a plain array, so the next section is dedicated to showing you how you can use
85 Paginator to paginate the results of a database query. Before reading on, make sure you're
86 familiar with the way <classname>Zend_Db_Select</classname> works!
90 In the database examples we will look at a table with blog posts called 'posts'.
91 The 'posts' table has four columns: id, title, body, date_created.
92 Let's dive right in and have a look at a simple example.
95 <programlisting language="php"><![CDATA[
96 // Create a select query. $db is a Zend_Db_Adapter object, which we assume
97 // already exists in your script.
98 $select = $db->select()->from('posts')->order('date_created DESC');
100 // Get a Paginator object using Zend_Paginator's built-in factory.
101 $paginator = Zend_Paginator::factory($select);
103 // Select the second page
104 $paginator->setCurrentPageNumber(2);
108 // Render each the title of each post for the current page in a list-item
109 foreach ($paginator as $item) {
110 echo '<li>' . $item->title . '</li>';
117 As you can see, this example is not that different from the previous one.
118 The only difference is that you pass a <classname>Zend_Db_Select</classname> object to the
119 Paginator's <methodname>factory()</methodname> method, rather than an array.
120 For more details on how the database adapter makes sure that your query
121 is being executed efficiently, see the <classname>Zend_Paginator</classname> chapter in the
122 reference manual on the DbSelect and DbTableSelect adapters.