Zend_Paginator-Usage.xml 23 KB

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