cgm225 Posted May 28, 2008 Share Posted May 28, 2008 Currently for my applications I generate pagination with maybe 20 lines of procedural code. However, it is generally not that reusable and requires a fair amount of reworking if I use it in a new script. My question is, are there any OOP approaches to pagination? If so, could you provide some example code or tutorial? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/ Share on other sites More sharing options...
mithras Posted May 28, 2008 Share Posted May 28, 2008 If you use a database abstraction layer, it might support pagination. If it don't, you can add the pagination feature yourself or maybe switch from database abstraction layer. I start about a database abstration layer, because it's a nice way to abstract pagination from your code in the controlling object. Example: $query = "SELECT name, description FROM items"; database::enablePagination( '5' ); //5 items per page $result = database::query( $query ); while( $pageSet = $result->getNextPage() ){ echo "Current page: " . $result->getCurrentPage(); foreach( $pageSet as $item ){ echo $item->name . " - " . $item->description; } } Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-552088 Share on other sites More sharing options...
cgm225 Posted May 29, 2008 Author Share Posted May 29, 2008 What would that class look like? Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-552530 Share on other sites More sharing options...
DeadlySin3 Posted June 12, 2008 Share Posted June 12, 2008 It's been my experience that Object Oriented pagination scripts are just not worth the effort. This may not be true for you... but for me, that's the way it is. I use a Database Abstraction Layer that was written by Matt Zandstra and slightly modified by me and I use some procedural code to paginate my results on several applications. It could be written into a method but i'm lazy and as i've said, I dont believe it's worth the effort. The Database Abstraction Layer I'm talking about can be found on my website: http://www.deadlysin3.net/FREE/database.abstraction/ It looks confusing at first, but when you dive into it, it really simplifies life as a coder. Feel free to check it out and use it if you think it'll suit your needs. Maybe the procedural code I wrote to paginate the results will work for you. Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-563599 Share on other sites More sharing options...
keeB Posted June 12, 2008 Share Posted June 12, 2008 I don't think he said.. 'Hey, please tell me OOP makes my life confusing' The argument will go on until the end of time. People like to code the way they like to code. As for an answer to pagination, moving to OOP should be really simple. All pagination is is a double-linked list (search it on wikipedia) that instead retrieves <x> instead of just next(). The general method of implementing it is the following: 1. Retrieve data in to cache 2. Pull first <x> from cache. $x = $list->getNext(); ($index is 0), $list->getPrev() returns null 3. Pull next <x> from cache. $x = $list->getNext(); The helper methods you would want to provide are things like: ->getCurrentPageNum(); ->getTotalPages(); Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-563653 Share on other sites More sharing options...
Xeoncross Posted June 14, 2008 Share Posted June 14, 2008 My whole pagination function is about 20 lines and works with any type of DB/file/etc results I give it. So yes it can be done in OOP very easily. (so easy it is just 1 simple function) Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-565579 Share on other sites More sharing options...
cgm225 Posted June 18, 2008 Author Share Posted June 18, 2008 Will you post that function? Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-567808 Share on other sites More sharing options...
Xeoncross Posted June 18, 2008 Share Posted June 18, 2008 This is a part of the http://codexplorer.com system. <?php /////////////////////////////////////////////////////////// // Panation function to create multiple pages for items over a certain limit /////////////////////////////////////////////////////////// // What info does this function need? ////////////////////////////////////////////////// // // $total_number = The total number of rows // $limit = the max items to show per-page // $urlvalue = a value to add to each link (optional) // ////////////////////////////////////////////////// // Example data ////////////////////////////////////////////////// // // $total_number = 67; // $limit = 10; // $urlvalue = '&order_by=date'; // ////////////////////////////////////////////////// public static function pagination($total_number, $limit, $urlvalue = '') { global $cx_config; //Initialize $data["pagelinks"] = null; //The page to show based on the number of links and the current page $data["from"] = (($limit * CURRENT_PAGE) - $limit); //The Number of pages based on the total number of results //Will through a nasty error if one of the values is NOT a number $numberofpages = ceil($total_number / $limit); if(CURRENT_PAGE > 1) { $data["pagelinks"] = '<a href="[[LINK]]'. (CURRENT_PAGE - 1). '" class="previous">Previous</a>'; } for($i = 1; $i <= $numberofpages; $i++) { if(CURRENT_PAGE == $i) { $data["pagelinks"] .= '<a class="current_page">'. $i. '</a>'; } else { $data["pagelinks"] .= '<a href="[[LINK]]'. $i. '">'. $i. '</a>'; } } if(CURRENT_PAGE < $numberofpages) { $data["pagelinks"] .= '<a href="[[LINK]]'. (CURRENT_PAGE + 1). '" class="next">Next</a>'; } //Now lets format the paging links with some nice div's, line-breaks, and tabs... $data["pagelinks"] = '<div id="pagination">'. "\n\t". $data["pagelinks"]. "\n\t". '</div>'; return $data; } ?> Now this function makes use of the "CURRENT_PAGE" constant and some other stuff that is set in the config of the system. But this should give you a general idea. Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-568243 Share on other sites More sharing options...
keeB Posted June 18, 2008 Share Posted June 18, 2008 The thing about pagination is, you need to store a local cache if you want it to be effective/if you're working with large data sets. As for that function posted, it doesn't really explain itself. Where does the data come from? Where's the result set? Looks like this function just displays the pages, but doesn't display the content of the page. If this were implemented this way in OOP, it'd be a mess. Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-568278 Share on other sites More sharing options...
nloding Posted June 19, 2008 Share Posted June 19, 2008 I agree partially with you, keeB, but I don't think pagination that includes FORMATTING a result set should be part of the pagination object and/or the database class. I think if you have a method to insert a variable that is the formatted result set, that's fine. When the pagination is drawn, it just echo's that variable. Just my two-cents. It's what I'm working on next for my site. Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-569139 Share on other sites More sharing options...
keeB Posted June 20, 2008 Share Posted June 20, 2008 If you'd like to run the design by me, I'd be happy to give you some pointers. Quote Link to comment https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/#findComment-569839 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.