Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/107571-pagination-procedural-versus-oop/
Share on other sites

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;
  }
}

  • 2 weeks later...

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.

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();

 

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.