|Latest |Kites |Pictures |Programming |Life|
[filed under Programming]Zend Framework MySQL DB Pagination Tutorial
Tags: Zend Framework 1.7 Pagination MySQL SQL Pages Paged Page Next Prev Previous
Just a quick how-to-make a MySQL table paginatior in Zend Framework.
First in your controller make a link to MySQL and make an SQL select() object (I find this disgusting syntax, but hey ho)... So, in the following code I am in my controllers action method:
public function myTableAction()
$db = Zend_Db::factory( $this->config->db );
So you have passed the MySQL syntax object to the paginator. This will add the LIMIT to the SQL and run it. The LIMIT syntax is built up from the page you are on and how many items per page you are displaying, this information is grabbed from the URL.
Next you need to make your view show this data and the:HTML. The paginatior object we passed to the view in the above code (the second to last line) is an iterator, so we can just loop over it:
<td colspan="4"><?= $this->paginationControl($this->paginator, 'Sliding', 'partials/search-pagination.phtml'); ?></td>
<?= $this->partialLoop('partial-loops/people.phtml', $this->paginator) ?>
OK, so the bit that builds the table is the partialLoop, we pass our pagination object that contains our table rows to the partialLoop iterator. This builds each <tr> row from the paginatior, the paginatior got the data from the MySQL table (after it manipulated your SQL adding a LIMIT 10,10) and got the item count from your table (it altered your query adding a COUNT() and re-queried the DB)
The loop partial looks like this (partial-loops/people.phtml):
Note how we have access to the two columns from MySQL as object properties. BTW I keep my view .phtml files in views/scripts and my partials in views/scripts/partials and my loop partials in views/scripts/loop-partials. You don't have to follow what I do, just make sure your paths make sense.
The style of the:partials/search-pagination.phtml view, this you can copy from this page (zend.paginator.usage -- Example Paginations Controls (at the end of the page)) and save it in the correct script view directory. Mine is a modified version (partials/search-pagination.phtml):HTML is in the
<?php if ($this->pageCount): ?>
That's it really. You'll need a bit of CSS to style the links.
For more info take a look at the Zend Framework wiki: Zend_Paginator
|Server Grind [0.0521 seconds]|