Zend_Paginator-Usage.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 Null 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. Contrary to popular
  153. believe, these adapters do <emphasis>not</emphasis> fetch all records from the database
  154. in order to count them. Instead, the adapters manipulates the original query to produce
  155. the corresponding COUNT query. Paginator then executes that COUNT query to get the
  156. number of rows. This does require an extra round-trip to the database, but this is many
  157. times faster than fetching an entire result set and using
  158. <methodname>count()</methodname>. Especially with large collections of data.
  159. </para>
  160. <para>
  161. The database adapters will try and build the most efficient query that will execute
  162. on pretty much all modern databases. However, depending on your database or even your
  163. own schema setup, there might be more efficient ways to get a rowcount. For this
  164. scenario the database adapters allow you to set a custom COUNT query. For example,
  165. if you keep track of the count of blog posts in a separate table, you could achieve a
  166. faster count query with the following setup:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
  170. $adapter->setRowCount(
  171. $db->select()
  172. ->from(
  173. 'item_counts',
  174. array(
  175. Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count'
  176. )
  177. )
  178. );
  179. $paginator = new Zend_Paginator($adapter);
  180. ]]></programlisting>
  181. <para>
  182. This approach will probably not give you a huge performance gain on
  183. small collections and/or simple select queries. However, with complex
  184. queries and large collections, a similar approach could give you a
  185. significant performance boost.
  186. </para>
  187. </sect2>
  188. <sect2 id="zend.paginator.rendering">
  189. <title>Rendering pages with view scripts</title>
  190. <para>
  191. The view script is used to render the page items (if you're using
  192. <classname>Zend_Paginator</classname> to do so) and display the pagination
  193. control.
  194. </para>
  195. <para>
  196. Because <classname>Zend_Paginator</classname> implements the <acronym>SPL</acronym>
  197. interface <ulink
  198. url="http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html"><classname>IteratorAggregate</classname></ulink>,
  199. looping over your items and displaying them is simple.
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. <html>
  203. <body>
  204. <h1>Example</h1>
  205. <?php if (count($this->paginator)): ?>
  206. <ul>
  207. <?php foreach ($this->paginator as $item): ?>
  208. <li><?php echo $item; ?></li>
  209. <?php endforeach; ?>
  210. </ul>
  211. <?php endif; ?>
  212. <?php echo $this->paginationControl($this->paginator,
  213. 'Sliding',
  214. 'my_pagination_control.phtml'); ?>
  215. </body>
  216. </html>
  217. ]]></programlisting>
  218. <para>
  219. Notice the view helper call near the end. PaginationControl
  220. accepts up to four parameters: the paginator instance, a scrolling
  221. style, a view partial, and an array of additional parameters.
  222. </para>
  223. <para>
  224. The second and third parameters are very important. Whereas the
  225. view partial is used to determine how the pagination control should
  226. <emphasis>look</emphasis>, the scrolling style is used to control
  227. how it should <emphasis>behave</emphasis>. Say the view partial is
  228. in the style of a search pagination control, like the one below:
  229. </para>
  230. <para>
  231. <inlinegraphic align="center" valign="middle"
  232. fileref="figures/zend.paginator.usage.rendering.control.png"
  233. format="PNG"/>
  234. </para>
  235. <para>
  236. What happens when the user clicks the "next" link a few times?
  237. Well, any number of things could happen. The current page number
  238. could stay in the middle as you click through (as it does on
  239. Yahoo!), or it could advance to the end of the page range and then
  240. appear again on the left when the user clicks "next" one more time.
  241. The page numbers might even expand and contract as the user
  242. advances (or "scrolls") through them (as they do on Google).
  243. </para>
  244. <para>
  245. There are four scrolling styles packaged with Zend Framework:
  246. </para>
  247. <table id="zend.paginator.usage.rendering.scrolling-styles">
  248. <title>Scrolling styles for Zend_Paginator</title>
  249. <tgroup cols="2">
  250. <thead>
  251. <row>
  252. <entry>Scrolling style</entry>
  253. <entry>Description</entry>
  254. </row>
  255. </thead>
  256. <tbody>
  257. <row>
  258. <entry>All</entry>
  259. <entry>
  260. Returns every page. This is useful for dropdown
  261. menu pagination controls with relatively few
  262. pages. In these cases, you want all pages
  263. available to the user at once.
  264. </entry>
  265. </row>
  266. <row>
  267. <entry>Elastic</entry>
  268. <entry>
  269. A Google-like scrolling style that expands and
  270. contracts as a user scrolls through the pages.
  271. </entry>
  272. </row>
  273. <row>
  274. <entry>Jumping</entry>
  275. <entry>
  276. As users scroll through, the page number advances
  277. to the end of a given range, then starts again at
  278. the beginning of the new range.
  279. </entry>
  280. </row>
  281. <row>
  282. <entry>Sliding</entry>
  283. <entry>
  284. A Yahoo!-like scrolling style that positions the
  285. current page number in the center of the page
  286. range, or as close as possible. This is the
  287. default style.
  288. </entry>
  289. </row>
  290. </tbody>
  291. </tgroup>
  292. </table>
  293. <para>
  294. The fourth and final parameter is reserved for an optional
  295. associative array of additional variables that you want available
  296. in your view partial (available via <varname>$this</varname>). For
  297. instance, these values could include extra <acronym>URL</acronym> parameters for
  298. pagination links.
  299. </para>
  300. <para>
  301. By setting the default view partial, default
  302. scrolling style, and view instance, you can eliminate the calls to
  303. PaginationControl completely:
  304. </para>
  305. <programlisting language="php"><![CDATA[
  306. Zend_Paginator::setDefaultScrollingStyle('Sliding');
  307. Zend_View_Helper_PaginationControl::setDefaultViewPartial(
  308. 'my_pagination_control.phtml'
  309. );
  310. $paginator->setView($view);
  311. ]]></programlisting>
  312. <para>
  313. When all of these values are set, you can render the pagination
  314. control inside your view script with a simple echo statement:
  315. </para>
  316. <programlisting language="php"><![CDATA[
  317. <?php echo $this->paginator; ?>
  318. ]]></programlisting>
  319. <note>
  320. <para>
  321. Of course, it's possible to use <classname>Zend_Paginator</classname>
  322. with other template engines. For example, with Smarty you
  323. might do the following:
  324. </para>
  325. <programlisting language="php"><![CDATA[
  326. $smarty->assign('pages', $paginator->getPages());
  327. ]]></programlisting>
  328. <para>
  329. You could then access paginator values from a template like so:
  330. </para>
  331. <programlisting language="php"><![CDATA[
  332. {$pages.pageCount}
  333. ]]></programlisting>
  334. </note>
  335. <sect3 id="zend.paginator.usage.rendering.example-controls">
  336. <title>Example pagination controls</title>
  337. <para>
  338. The following example pagination controls will hopefully help
  339. you get started:
  340. </para>
  341. <para>
  342. Search pagination:
  343. </para>
  344. <programlisting language="php"><![CDATA[
  345. <!--
  346. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
  347. -->
  348. <?php if ($this->pageCount): ?>
  349. <div class="paginationControl">
  350. <!-- Previous page link -->
  351. <?php if (isset($this->previous)): ?>
  352. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  353. &lt; Previous
  354. </a> |
  355. <?php else: ?>
  356. <span class="disabled">&lt; Previous</span> |
  357. <?php endif; ?>
  358. <!-- Numbered page links -->
  359. <?php foreach ($this->pagesInRange as $page): ?>
  360. <?php if ($page != $this->current): ?>
  361. <a href="<?php echo $this->url(array('page' => $page)); ?>">
  362. <?php echo $page; ?>
  363. </a> |
  364. <?php else: ?>
  365. <?php echo $page; ?> |
  366. <?php endif; ?>
  367. <?php endforeach; ?>
  368. <!-- Next page link -->
  369. <?php if (isset($this->next)): ?>
  370. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  371. Next &gt;
  372. </a>
  373. <?php else: ?>
  374. <span class="disabled">Next &gt;</span>
  375. <?php endif; ?>
  376. </div>
  377. <?php endif; ?>
  378. ]]></programlisting>
  379. <para>
  380. Item pagination:
  381. </para>
  382. <programlisting language="php"><![CDATA[
  383. <!--
  384. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
  385. -->
  386. <?php if ($this->pageCount): ?>
  387. <div class="paginationControl">
  388. <?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
  389. of <?php echo $this->totalItemCount; ?>
  390. <!-- First page link -->
  391. <?php if (isset($this->previous)): ?>
  392. <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
  393. First
  394. </a> |
  395. <?php else: ?>
  396. <span class="disabled">First</span> |
  397. <?php endif; ?>
  398. <!-- Previous page link -->
  399. <?php if (isset($this->previous)): ?>
  400. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  401. &lt; Previous
  402. </a> |
  403. <?php else: ?>
  404. <span class="disabled">&lt; Previous</span> |
  405. <?php endif; ?>
  406. <!-- Next page link -->
  407. <?php if (isset($this->next)): ?>
  408. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  409. Next &gt;
  410. </a> |
  411. <?php else: ?>
  412. <span class="disabled">Next &gt;</span> |
  413. <?php endif; ?>
  414. <!-- Last page link -->
  415. <?php if (isset($this->next)): ?>
  416. <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
  417. Last
  418. </a>
  419. <?php else: ?>
  420. <span class="disabled">Last</span>
  421. <?php endif; ?>
  422. </div>
  423. <?php endif; ?>
  424. ]]></programlisting>
  425. <para>
  426. Dropdown pagination:
  427. </para>
  428. <programlisting language="php"><![CDATA[
  429. <?php if ($this->pageCount): ?>
  430. <select id="paginationControl" size="1">
  431. <?php foreach ($this->pagesInRange as $page): ?>
  432. <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
  433. <option value="<?php
  434. echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
  435. <?php echo $page; ?>
  436. </option>
  437. <?php endforeach; ?>
  438. </select>
  439. <?php endif; ?>
  440. <script type="text/javascript"
  441. src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
  442. </script>
  443. <script type="text/javascript">
  444. $('paginationControl').observe('change', function() {
  445. window.location = this.options[this.selectedIndex].value;
  446. })
  447. </script>
  448. ]]></programlisting>
  449. </sect3>
  450. <sect3 id="zend.paginator.usage.rendering.properties">
  451. <title>Listing of properties</title>
  452. <para>
  453. The following options are available to pagination control
  454. view partials:
  455. </para>
  456. <table id="zend.paginator.usage.rendering.properties.table">
  457. <title>Properties available to view partials</title>
  458. <tgroup cols="3">
  459. <thead>
  460. <row>
  461. <entry>Property</entry>
  462. <entry>Type</entry>
  463. <entry>Description</entry>
  464. </row>
  465. </thead>
  466. <tbody>
  467. <row>
  468. <entry>first</entry>
  469. <entry>integer</entry>
  470. <entry>First page number (i.e., 1)</entry>
  471. </row>
  472. <row>
  473. <entry>firstItemNumber</entry>
  474. <entry>integer</entry>
  475. <entry>
  476. Absolute number of the first item on this page
  477. </entry>
  478. </row>
  479. <row>
  480. <entry>firstPageInRange</entry>
  481. <entry>integer</entry>
  482. <entry>
  483. First page in the range returned by the
  484. scrolling style
  485. </entry>
  486. </row>
  487. <row>
  488. <entry>current</entry>
  489. <entry>integer</entry>
  490. <entry>Current page number</entry>
  491. </row>
  492. <row>
  493. <entry>currentItemCount</entry>
  494. <entry>integer</entry>
  495. <entry>Number of items on this page</entry>
  496. </row>
  497. <row>
  498. <entry>itemCountPerPage</entry>
  499. <entry>integer</entry>
  500. <entry>Maximum number of items available to each page</entry>
  501. </row>
  502. <row>
  503. <entry>last</entry>
  504. <entry>integer</entry>
  505. <entry>Last page number</entry>
  506. </row>
  507. <row>
  508. <entry>lastItemNumber</entry>
  509. <entry>integer</entry>
  510. <entry>
  511. Absolute number of the last item on this page
  512. </entry>
  513. </row>
  514. <row>
  515. <entry>lastPageInRange</entry>
  516. <entry>integer</entry>
  517. <entry>
  518. Last page in the range returned by the
  519. scrolling style
  520. </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>
  536. Array of pages returned by the scrolling style
  537. </entry>
  538. </row>
  539. <row>
  540. <entry>previous</entry>
  541. <entry>integer</entry>
  542. <entry>Previous page number</entry>
  543. </row>
  544. <row>
  545. <entry>totalItemCount</entry>
  546. <entry>integer</entry>
  547. <entry>Total number of items</entry>
  548. </row>
  549. </tbody>
  550. </tgroup>
  551. </table>
  552. </sect3>
  553. </sect2>
  554. </sect1>
  555. <!--
  556. vim:se ts=4 sw=4 et:
  557. -->