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