| 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.You'll need these as a reference, zend.paginator.usage and zend.db.select. 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:<prev 1 2 3 4 5 next >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: <table class="my_table">
<thead> <tr class="table_head"> <th>People</th> </tr> </thead> <tfoot> <tr class="table_foot"> <td colspan="4"><?= $this->paginationControl($this->paginator, 'Sliding', 'partials/search-pagination.phtml'); ?></td> </tr> </tfoot> <tbody> <?= $this->partialLoop('partial-loops/people.phtml', $this->paginator) ?> </tbody> </table> 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): <tr> 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:<prev 1 2 3 4 5 next >HTML is in 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): <?php if ($this->pageCount): ?> </div> 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 Job done.
Comments [28th of Oct, 2008 @ 03:12 PM] Simon said: "make an SQL select() object (I find this disgusting syntax, but hey ho)..." Why so? [28th of Oct, 2008 @ 03:23 PM] monk.e.boy said: Compare this: $select = $db->select() ->from(array('p' => 'people'), array('id','name')) ->order('name'); To this: SELECT id, name FROM people ORDER BY name I think the second is easier to read. PHP should make SQL part of the language, a mini language, so you could do this: $s = SELECT id, name FROM people ORDER BY name; (note: no quotes) echo $s->order_by; $s->order_by->append( 'id' ); or something like that. [9th of Nov, 2008 @ 09:43 AM] Martin Winkel said: @monk.e.boy I agree it's far more readable, but on the other hand, there are tiny differences between database-systems in the SQL language. Using a tiny language will not solve that problemn, while a select-object does. Anyway, I hope this stays just here ;). Usefull tutorial by the way. [18th of Feb, 2009 @ 08:36 AM] dayg said: Thanks for the tutorial! It works. Minor correction for the navigation: id="selected"><?php endif; ?> Should be id="selected"<?php endif; ?>> [21st of Mar, 2009 @ 04:42 PM] rafagas said: Thanks for the tutorial man great help! choose [14th of Apr, 2009 @ 11:13 AM] said: [14th of Apr, 2009 @ 11:14 AM] said: [14th of Apr, 2009 @ 11:14 AM] said: [8th of Jul, 2009 @ 12:38 PM] sasi said: this is working? [16th of Oct, 2009 @ 02:01 PM] said: [16th of Oct, 2009 @ 02:01 PM] said: [16th of Oct, 2009 @ 02:01 PM] said: [16th of Oct, 2009 @ 02:02 PM] said: <script>alert('hi');</script> [16th of Dec, 2009 @ 12:55 PM] said: [16th of Dec, 2009 @ 12:56 PM] said said: not working. returns this error : Message: No paginator instance provided or incorrect type [17th of Dec, 2009 @ 06:53 AM] said: [17th of Dec, 2009 @ 06:54 AM] said: <script>alert('test');</script> [5th of Mar, 2010 @ 08:07 AM] johan said: @said What are you trying to do with ur alert tests? you're just tying to make fun of this page. arent you? damn dumb you!!!!!!! [24th of Jun, 2010 @ 07:48 PM] Vheapfioricet93roa said: http://www.livescribe.com/forums/member.php?u=8053&ul11=1 [url=http://www.livescribe.com/forums/member.php?u=8058&ul11=1]viagra[/url] <a href="http://www.livescribe.com/forums/member.php?u=8059&ul11=1">buy cialis</a> <a href=http://www.livescribe.com/forums/member.php?u=8055&ul11=1>buy diazepam</a> [url="http://www.livescribe.com/forums/member.php?u=8056&ul11=1"]tramadol[/url] [LINK http://www.livescribe.com/forums/member.php?u=8060&ul11=1]buy cialis online[/LINK] huxm [25th of Jun, 2010 @ 01:50 AM] Butpropecia81xuy said: http://www.livescribe.com/forums/member.php?u=8062&ul11=2 [url=http://www.livescribe.com/forums/member.php?u=8067&ul11=2]cheap xanax[/url] <a href="http://www.livescribe.com/forums/member.php?u=8061&ul11=2">cheap levitra</a> <a href=http://forums.bleachexile.com/member.php?u=55575&ul11=2>hydrocodone online</a> [url="http://www.livescribe.com/forums/member.php?u=8065&ul11=2"]weight loss pills[/url] [LINK http://www.livescribe.com/forums/member.php?u=8063&ul11=2]cheap adipex[/LINK] qjpx [27th of Jun, 2010 @ 07:27 PM] Weightloaspills69mps said: http://forums.bleachexile.com/member.php?u=55575&ul15=1 [url=http://www.livescribe.com/forums/member.php?u=8067&ul15=1]buy xanax[/url] <a href="http://www.livescribe.com/forums/member.php?u=8072&ul15=1">cheap propecia</a> <a href=http://www.livescribe.com/forums/member.php?u=8066&ul15=1>retin-a</a> [url="http://www.livescribe.com/forums/member.php?u=8061&ul15=1"]cheap levitra[/url] [LINK http://www.livescribe.com/forums/member.php?u=8062&ul15=1]buy acomplia[/LINK] chlt [27th of Jun, 2010 @ 09:12 PM] Cheapfioriicet91vku said: http://www.livescribe.com/forums/member.php?u=8056&ul15=2 [url=http://www.livescribe.com/forums/member.php?u=8060&ul15=2]cheapest cialis[/url] <a href="http://www.livescribe.com/forums/member.php?u=8058&ul15=2">generic viagra</a> <a href=http://www.livescribe.com/forums/member.php?u=8053&ul15=2>hydrocodone</a> [url="http://www.livescribe.com/forums/member.php?u=8059&ul15=2"]cialis[/url] [LINK http://www.livescribe.com/forums/member.php?u=8055&ul15=2]diazepam[/LINK] agbu [28th of Jun, 2010 @ 01:31 AM] Rerin-a14cbx said: http://www.livescribe.com/forums/member.php?u=8062&ul15=3 [url=http://www.livescribe.com/forums/member.php?u=8072&ul15=3]cheap propecia[/url] <a href="http://forums.bleachexile.com/member.php?u=55575&ul15=3">hydrocodone</a> <a href=http://www.livescribe.com/forums/member.php?u=8063&ul15=3>cheap adipex</a> [url="http://www.livescribe.com/forums/member.php?u=8061&ul15=3"]buy levitra[/url] [LINK http://www.livescribe.com/forums/member.php?u=8065&ul15=3]diet pills[/LINK] otik [28th of Jun, 2010 @ 04:18 AM] Hudrocodone56kmj said: http://www.livescribe.com/forums/member.php?u=8054&ul15=4 [url=http://www.livescribe.com/forums/member.php?u=8055&ul15=4]diazepam online[/url] <a href="http://www.livescribe.com/forums/member.php?u=8058&ul15=4">viagra</a> <a href=http://www.livescribe.com/forums/member.php?u=8057&ul15=4>buy viagra</a> [url="http://www.livescribe.com/forums/member.php?u=8060&ul15=4"]cheapest cialis[/url] [LINK http://www.livescribe.com/forums/member.php?u=8059&ul15=4]cheap cialis[/LINK] nbfz [4th of Jul, 2010 @ 09:54 AM] Fioticet92dxq said: http://www.livescribe.com/forums/member.php?u=8053&ul23=1 [url=http://www.livescribe.com/forums/member.php?u=8059&ul23=1]cheap cialis[/url] <a href="http://www.livescribe.com/forums/member.php?u=8060&ul23=1">buy cialis online</a> <a href=http://www.livescribe.com/forums/member.php?u=8055&ul23=1>diazepam</a> [url="http://www.livescribe.com/forums/member.php?u=8058&ul23=1"]free viagra[/url] [LINK http://www.livescribe.com/forums/member.php?u=8056&ul23=1]cheap tramadol[/LINK] gxns [4th of Jul, 2010 @ 01:55 PM] Buytramadool50hnt said: http://www.livescribe.com/forums/member.php?u=8057&ul23=2 [url=http://www.livescribe.com/forums/member.php?u=8053&ul23=2]hydrocodone online[/url] <a href="http://www.livescribe.com/forums/member.php?u=8058&ul23=2">free viagra</a> <a href=http://www.livescribe.com/forums/member.php?u=8059&ul23=2>cheap cialis</a> [url="http://www.livescribe.com/forums/member.php?u=8060&ul23=2"]generic cialis[/url] [LINK http://www.livescribe.com/forums/member.php?u=8054&ul23=2]buy fioricet[/LINK] obag [4th of Jul, 2010 @ 09:16 PM] Cheaptramsdol09kuw said: http://www.livescribe.com/forums/member.php?u=8055&ul23=3 [url=http://www.livescribe.com/forums/member.php?u=8054&ul23=3]fioricet[/url] <a href="http://www.livescribe.com/forums/member.php?u=8053&ul23=3">hydrocodone online</a> <a href=http://www.livescribe.com/forums/member.php?u=8060&ul23=3>cheapest cialis</a> [url="http://www.livescribe.com/forums/member.php?u=8059&ul23=3"]cheap cialis[/url] [LINK http://www.livescribe.com/forums/member.php?u=8057&ul23=3]cheap viagra[/LINK] iogh |
| Server Grind [0.3380 seconds] |