Zend_Paginator-Usage.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.paginator.usage">
  4. <title>Usage</title>
  5. <sect2 id="zend.paginator.usage.paginating">
  6. <title>Paginating data collections</title>
  7. <para>
  8. In order to paginate items into pages, <classname>Zend_Paginator</classname>
  9. must have a generic way of accessing that data. For that reason,
  10. all data access takes place through data source adapters. Several
  11. adapters ship with Zend Framework by default:
  12. </para>
  13. <table id="zend.paginator.usage.paginating.adapters">
  14. <title>Adapters for Zend_Paginator</title>
  15. <tgroup cols="2">
  16. <thead>
  17. <row>
  18. <entry>Adapter</entry>
  19. <entry>Description</entry>
  20. </row>
  21. </thead>
  22. <tbody>
  23. <row>
  24. <entry>Array</entry>
  25. <entry>Use a PHP array</entry>
  26. </row>
  27. <row>
  28. <entry>DbSelect</entry>
  29. <entry>
  30. Use a <link linkend="zend.db.select"><classname>Zend_Db_Select</classname></link>
  31. instance, which will return an array
  32. </entry>
  33. </row>
  34. <row>
  35. <entry>DbTableSelect</entry>
  36. <entry>
  37. Use a <link linkend="zend.db.table.fetch-all"><classname>Zend_Db_Table_Select</classname></link>
  38. instance, which will return an instance of
  39. <classname>Zend_Db_Table_Rowset_Abstract</classname>.
  40. This provides additional information about the
  41. result set, such as column names.
  42. </entry>
  43. </row>
  44. <row>
  45. <entry>Iterator</entry>
  46. <entry>
  47. Use an <ulink url="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></ulink>
  48. instance
  49. </entry>
  50. </row>
  51. <row>
  52. <entry>Null</entry>
  53. <entry>
  54. Do not use <classname>Zend_Paginator</classname> to manage
  55. data pagination. You can still take advantage of
  56. the pagination control feature.
  57. </entry>
  58. </row>
  59. </tbody>
  60. </tgroup>
  61. </table>
  62. <note>
  63. <para>
  64. Instead of selecting every matching row of a given query, the
  65. DbSelect and DbTableSelect adapters retrieve only the smallest
  66. amount of data necessary for displaying the current page.
  67. </para>
  68. <para>
  69. Because of this, a second query is dynamically generated to
  70. determine the total number of matching rows. However, it is
  71. possible to directly supply a count or count query yourself.
  72. See the <code>setRowCount()</code> method in the DbSelect
  73. adapter for more information.
  74. </para>
  75. </note>
  76. <para>
  77. To create an instance of <classname>Zend_Paginator</classname>, you must
  78. supply an adapter to the constructor:
  79. </para>
  80. <para>
  81. <programlisting language="php"><![CDATA[
  82. $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));
  83. ]]></programlisting>
  84. </para>
  85. <para>
  86. For convenience, you may take advantage of the static
  87. <code>factory()</code> method for the adapters packaged with Zend
  88. Framework:
  89. </para>
  90. <para>
  91. <programlisting language="php"><![CDATA[
  92. $paginator = Zend_Paginator::factory($array);
  93. ]]></programlisting>
  94. </para>
  95. <note>
  96. <para>
  97. In the case of the Null adapter, in lieu of a data collection
  98. you must supply an item count to its constructor.
  99. </para>
  100. </note>
  101. <para>
  102. Although the instance is technically usable in this state, in your
  103. controller action you'll need to tell the paginator what page
  104. number the user requested. This allows him to advance through the
  105. paginated data.
  106. </para>
  107. <para>
  108. <programlisting language="php"><![CDATA[
  109. $paginator->setCurrentPageNumber($page);
  110. ]]></programlisting>
  111. </para>
  112. <para>
  113. The simplest way to keep track of this value is through a URL.
  114. Although we recommend using a
  115. <classname>Zend_Controller_Router_Interface</classname>-compatible
  116. router to handle this, it is not a requirement.
  117. </para>
  118. <para>
  119. The following is an example route you might use in an INI
  120. configuration file:
  121. </para>
  122. <para>
  123. <programlisting language="php"><![CDATA[
  124. routes.example.route = articles/:articleName/:page
  125. routes.example.defaults.controller = articles
  126. routes.example.defaults.action = view
  127. routes.example.defaults.page = 1
  128. routes.example.reqs.articleName = \w+
  129. routes.example.reqs.page = \d+
  130. ]]></programlisting>
  131. </para>
  132. <para>
  133. With the above route (and using Zend Framework MVC components),
  134. you might set the current page number like this:
  135. </para>
  136. <para>
  137. <programlisting language="php"><![CDATA[
  138. $paginator->setCurrentPageNumber($this->_getParam('page'));
  139. ]]></programlisting>
  140. </para>
  141. <para>
  142. There are other options available; see
  143. <link linkend="zend.paginator.configuration">Configuration</link>
  144. for more on them.
  145. </para>
  146. <para>
  147. Finally, you'll need to assign the paginator instance to your view.
  148. If you're using <classname>Zend_View</classname> with the ViewRenderer action
  149. helper, the following will work:
  150. </para>
  151. <para>
  152. <programlisting language="php"><![CDATA[
  153. $this->view->paginator = $paginator;
  154. ]]></programlisting>
  155. </para>
  156. </sect2>
  157. <sect2 id="zend.paginator.usage.dbselect">
  158. <title>The DbSelect and DbTableSelect adapter</title>
  159. <para>
  160. The usage of most adapters is pretty straight-forward. However, the
  161. database adapters require a more detailed explanation. Contrary to popular
  162. believe, these adapters do NOT fetch all records from the database in order to count them.
  163. Instead, the adapters manipulates the original query to produce the corresponding
  164. COUNT query. Paginator then executes that COUNT query to get the number of rows.
  165. This does require an extra round-trip to the database, but this is many times
  166. faster than fetching an entire result set and using count(). Especially with
  167. large collections of data.
  168. </para>
  169. <para>
  170. The database adapters will try and build the most efficient query that will execute
  171. on pretty much all modern databases. However, depending on your database or even your
  172. own schema setup, there might be more efficient ways to get a rowcount. For this scenario
  173. the database adapters allow you to set a custom COUNT query. For example,
  174. if you keep track of the count of blog posts in a separate table, you could achieve a
  175. faster count query with the following setup:
  176. </para>
  177. <programlisting language="php"><![CDATA[
  178. $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
  179. $adapter->setRowCount(
  180. $db->select()->from('item_counts', array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count'))
  181. )
  182. $paginator = new Zend_Paginator($adapter);
  183. ]]></programlisting>
  184. <para>
  185. This approach will probably not give you a huge performance gain on
  186. small collections and/or simple select queries. However, with complex
  187. queries and large collections, a similar approach could give you a
  188. significant performance boost.
  189. </para>
  190. </sect2>
  191. <sect2 id="zend.paginator.rendering">
  192. <title>Rendering pages with view scripts</title>
  193. <para>
  194. The view script is used to render the page items (if you're using
  195. <classname>Zend_Paginator</classname> to do so) and display the pagination
  196. control.
  197. </para>
  198. <para>
  199. Because <classname>Zend_Paginator</classname> implements the SPL interface
  200. <ulink url="http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html"><code>IteratorAggregate</code></ulink>,
  201. looping over your items and displaying them is simple.
  202. </para>
  203. <para>
  204. <programlisting language="php"><![CDATA[
  205. <html>
  206. <body>
  207. <h1>Example</h1>
  208. <?php if (count($this->paginator)): ?>
  209. <ul>
  210. <?php foreach ($this->paginator as $item): ?>
  211. <li><?php echo $item; ?></li>
  212. <?php endforeach; ?>
  213. </ul>
  214. <?php endif; ?>
  215. <?php echo $this->paginationControl($this->paginator,
  216. 'Sliding',
  217. 'my_pagination_control.phtml'); ?>
  218. </body>
  219. </html>
  220. ]]></programlisting>
  221. </para>
  222. <para>
  223. Notice the view helper call near the end. PaginationControl
  224. accepts up to four parameters: the paginator instance, a scrolling
  225. style, a view partial, and an array of additional parameters.
  226. </para>
  227. <para>
  228. The second and third parameters are very important. Whereas the
  229. view partial is used to determine how the pagination control should
  230. <emphasis>look</emphasis>, the scrolling style is used to control
  231. how it should <emphasis>behave</emphasis>. Say the view partial is
  232. in the style of a search pagination control, like the one below:
  233. </para>
  234. <para>
  235. <inlinegraphic align="center" valign="middle"
  236. fileref="figures/zend.paginator.usage.rendering.control.png"
  237. format="PNG"/>
  238. </para>
  239. <para>
  240. What happens when the user clicks the "next" link a few times?
  241. Well, any number of things could happen. The current page number
  242. could stay in the middle as you click through (as it does on
  243. Yahoo!), or it could advance to the end of the page range and then
  244. appear again on the left when the user clicks "next" one more time.
  245. The page numbers might even expand and contract as the user
  246. advances (or "scrolls") through them (as they do on Google).
  247. </para>
  248. <para>
  249. There are four scrolling styles packaged with Zend Framework:
  250. </para>
  251. <table id="zend.paginator.usage.rendering.scrolling-styles">
  252. <title>Scrolling styles for Zend_Paginator</title>
  253. <tgroup cols="2">
  254. <thead>
  255. <row>
  256. <entry>Scrolling style</entry>
  257. <entry>Description</entry>
  258. </row>
  259. </thead>
  260. <tbody>
  261. <row>
  262. <entry>All</entry>
  263. <entry>
  264. Returns every page. This is useful for dropdown
  265. menu pagination controls with relatively few
  266. pages. In these cases, you want all pages
  267. available to the user at once.
  268. </entry>
  269. </row>
  270. <row>
  271. <entry>Elastic</entry>
  272. <entry>
  273. A Google-like scrolling style that expands and
  274. contracts as a user scrolls through the pages.
  275. </entry>
  276. </row>
  277. <row>
  278. <entry>Jumping</entry>
  279. <entry>
  280. As users scroll through, the page number advances
  281. to the end of a given range, then starts again at
  282. the beginning of the new range.
  283. </entry>
  284. </row>
  285. <row>
  286. <entry>Sliding</entry>
  287. <entry>
  288. A Yahoo!-like scrolling style that positions the
  289. current page number in the center of the page
  290. range, or as close as possible. This is the
  291. default style.
  292. </entry>
  293. </row>
  294. </tbody>
  295. </tgroup>
  296. </table>
  297. <para>
  298. The fourth and final parameter is reserved for an optional
  299. associative array of additional variables that you want available
  300. in your view partial (available via <code>$this</code>). For
  301. instance, these values could include extra URL parameters for
  302. pagination links.
  303. </para>
  304. <para>
  305. By setting the default view partial, default
  306. scrolling style, and view instance, you can eliminate the calls to
  307. PaginationControl completely:
  308. </para>
  309. <para>
  310. <programlisting language="php"><![CDATA[
  311. Zend_Paginator::setDefaultScrollingStyle('Sliding');
  312. Zend_View_Helper_PaginationControl::setDefaultViewPartial(
  313. 'my_pagination_control.phtml'
  314. );
  315. $paginator->setView($view);
  316. ]]></programlisting>
  317. </para>
  318. <para>
  319. When all of these values are set, you can render the pagination
  320. control inside your view script with a simple echo statement:
  321. </para>
  322. <para>
  323. <programlisting language="php"><![CDATA[
  324. <?php echo $this->paginator; ?>
  325. ]]></programlisting>
  326. </para>
  327. <note>
  328. <para>
  329. Of course, it's possible to use <classname>Zend_Paginator</classname>
  330. with other template engines. For example, with Smarty you
  331. might do the following:
  332. </para>
  333. <para>
  334. <programlisting language="php"><![CDATA[
  335. $smarty->assign('pages', $paginator->getPages());
  336. ]]></programlisting>
  337. </para>
  338. <para>
  339. You could then access paginator values from a template like so:
  340. </para>
  341. <para>
  342. <programlisting language="php"><![CDATA[
  343. {$pages.pageCount}
  344. ]]></programlisting>
  345. </para>
  346. </note>
  347. <sect3 id="zend.paginator.usage.rendering.example-controls">
  348. <title>Example pagination controls</title>
  349. <para>
  350. The following example pagination controls will hopefully help
  351. you get started:
  352. </para>
  353. <para>
  354. Search pagination
  355. <programlisting language="php"><![CDATA[
  356. <!--
  357. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
  358. -->
  359. <?php if ($this->pageCount): ?>
  360. <div class="paginationControl">
  361. <!-- Previous page link -->
  362. <?php if (isset($this->previous)): ?>
  363. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  364. &lt; Previous
  365. </a> |
  366. <?php else: ?>
  367. <span class="disabled">&lt; Previous</span> |
  368. <?php endif; ?>
  369. <!-- Numbered page links -->
  370. <?php foreach ($this->pagesInRange as $page): ?>
  371. <?php if ($page != $this->current): ?>
  372. <a href="<?php echo $this->url(array('page' => $page)); ?>">
  373. <?php echo $page; ?>
  374. </a> |
  375. <?php else: ?>
  376. <?php echo $page; ?> |
  377. <?php endif; ?>
  378. <?php endforeach; ?>
  379. <!-- Next page link -->
  380. <?php if (isset($this->next)): ?>
  381. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  382. Next &gt;
  383. </a>
  384. <?php else: ?>
  385. <span class="disabled">Next &gt;</span>
  386. <?php endif; ?>
  387. </div>
  388. <?php endif; ?>
  389. ]]></programlisting>
  390. </para>
  391. <para>
  392. Item pagination
  393. <programlisting language="php"><![CDATA[
  394. <!--
  395. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
  396. -->
  397. <?php if ($this->pageCount): ?>
  398. <div class="paginationControl">
  399. <?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
  400. of <?php echo $this->totalItemCount; ?>
  401. <!-- First page link -->
  402. <?php if (isset($this->previous)): ?>
  403. <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
  404. First
  405. </a> |
  406. <?php else: ?>
  407. <span class="disabled">First</span> |
  408. <?php endif; ?>
  409. <!-- Previous page link -->
  410. <?php if (isset($this->previous)): ?>
  411. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  412. &lt; Previous
  413. </a> |
  414. <?php else: ?>
  415. <span class="disabled">&lt; Previous</span> |
  416. <?php endif; ?>
  417. <!-- Next page link -->
  418. <?php if (isset($this->next)): ?>
  419. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  420. Next &gt;
  421. </a> |
  422. <?php else: ?>
  423. <span class="disabled">Next &gt;</span> |
  424. <?php endif; ?>
  425. <!-- Last page link -->
  426. <?php if (isset($this->next)): ?>
  427. <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
  428. Last
  429. </a>
  430. <?php else: ?>
  431. <span class="disabled">Last</span>
  432. <?php endif; ?>
  433. </div>
  434. <?php endif; ?>
  435. ]]></programlisting>
  436. </para>
  437. <para>
  438. Dropdown pagination
  439. <programlisting language="php"><![CDATA[
  440. <?php if ($this->pageCount): ?>
  441. <select id="paginationControl" size="1">
  442. <?php foreach ($this->pagesInRange as $page): ?>
  443. <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
  444. <option value="<?php
  445. echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
  446. <?php echo $page; ?>
  447. </option>
  448. <?php endforeach; ?>
  449. </select>
  450. <?php endif; ?>
  451. <script type="text/javascript"
  452. src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
  453. </script>
  454. <script type="text/javascript">
  455. $('paginationControl').observe('change', function() {
  456. window.location = this.options[this.selectedIndex].value;
  457. })
  458. </script>
  459. ]]></programlisting>
  460. </para>
  461. </sect3>
  462. <sect3 id="zend.paginator.usage.rendering.properties">
  463. <title>Listing of properties</title>
  464. <para>
  465. The following options are available to pagination control
  466. view partials:
  467. </para>
  468. <table id="zend.paginator.usage.rendering.properties.table">
  469. <title>Properties available to view partials</title>
  470. <tgroup cols="3">
  471. <thead>
  472. <row>
  473. <entry>Property</entry>
  474. <entry>Type</entry>
  475. <entry>Description</entry>
  476. </row>
  477. </thead>
  478. <tbody>
  479. <row>
  480. <entry>first</entry>
  481. <entry>integer</entry>
  482. <entry>First page number (i.e., 1)</entry>
  483. </row>
  484. <row>
  485. <entry>firstItemNumber</entry>
  486. <entry>integer</entry>
  487. <entry>
  488. Absolute number of the first item on this page
  489. </entry>
  490. </row>
  491. <row>
  492. <entry>firstPageInRange</entry>
  493. <entry>integer</entry>
  494. <entry>
  495. First page in the range returned by the
  496. scrolling style
  497. </entry>
  498. </row>
  499. <row>
  500. <entry>current</entry>
  501. <entry>integer</entry>
  502. <entry>Current page number</entry>
  503. </row>
  504. <row>
  505. <entry>currentItemCount</entry>
  506. <entry>integer</entry>
  507. <entry>Number of items on this page</entry>
  508. </row>
  509. <row>
  510. <entry>itemCountPerPage</entry>
  511. <entry>integer</entry>
  512. <entry>Maximum number of items available to each page</entry>
  513. </row>
  514. <row>
  515. <entry>last</entry>
  516. <entry>integer</entry>
  517. <entry>Last page number</entry>
  518. </row>
  519. <row>
  520. <entry>lastItemNumber</entry>
  521. <entry>integer</entry>
  522. <entry>
  523. Absolute number of the last item on this page
  524. </entry>
  525. </row>
  526. <row>
  527. <entry>lastPageInRange</entry>
  528. <entry>integer</entry>
  529. <entry>
  530. Last page in the range returned by the
  531. scrolling style
  532. </entry>
  533. </row>
  534. <row>
  535. <entry>next</entry>
  536. <entry>integer</entry>
  537. <entry>Next page number</entry>
  538. </row>
  539. <row>
  540. <entry>pageCount</entry>
  541. <entry>integer</entry>
  542. <entry>Number of pages</entry>
  543. </row>
  544. <row>
  545. <entry>pagesInRange</entry>
  546. <entry>array</entry>
  547. <entry>
  548. Array of pages returned by the scrolling style
  549. </entry>
  550. </row>
  551. <row>
  552. <entry>previous</entry>
  553. <entry>integer</entry>
  554. <entry>Previous page number</entry>
  555. </row>
  556. <row>
  557. <entry>totalItemCount</entry>
  558. <entry>integer</entry>
  559. <entry>Total number of items</entry>
  560. </row>
  561. </tbody>
  562. </tgroup>
  563. </table>
  564. </sect3>
  565. </sect2>
  566. </sect1>
  567. <!--
  568. vim:se ts=4 sw=4 et:
  569. -->