paginator-simple.xml 4.5 KB

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