TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Show Off (http://www.talkphp.com/show-off/)
-   -   My Pagination Class (http://www.talkphp.com/show-off/1754-my-pagination-class.html)

Andrew 12-17-2007 06:10 AM

My Pagination Class
 
I've seen quite a bit of talk around here about Pagination, so I thought I'd quickly make my own. My class is able to work with any kind of result, as long as it's an array.

Here is the code for the actual page it is displayed on:
PHP Code:

<?php

include_once 'pagination.php';

$aData = array(
            
'1' => 'oranges',
            
'2' => 'lemons',
            
'3' => 'limes',
            
'4' => 'grapes',
            
'5' => 'grapefuit',
            
'6' => 'peaches',
            
'7' => 'potatoes',
            
'8' => 'cranberries',
            
'9' => 'something else',
            
'10' => 'apples');

$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$page = new Pagination($aData$current_page2);

for (
$i $page->firstResult$i <= $page->lastResult$i++) {
    echo 
$aData[$i].'<br />';
}

echo 
'<ul>'."\n";
echo 
$page->prevLink('<li>''</li>');
foreach (
$page->display() AS $output) {
    echo 
'<li>'.$output.'</li>'."\n";
}
echo 
$page->nextLink('<li>''</li>');
echo 
'</ul>'."\n";

?>

And here is the code for the class:
PHP Code:

<?php

/**
* @name PHP5 Pagination Script
* @desc Usable with any sort of array
* @created 16 Dec 2007
* @copyright Andrew Ryno
**/

class Pagination {
    
    
/* These variables will just hold all the information 
        and values which are used by the class, no editing
        needed here */
    
public $show_max;
    public 
$current_page;
    public 
$total_results;
    public 
$total_pages;
    public 
$prev_link;
    public 
$next_link;
    public 
$firstResult;
    public 
$lastResult;
    
    
/* This function will construct all the variables which
        are needed for the script, and get the values of the
        variables passed through the initializaion */
    
function __construct($aData$iCurrentPage$iMax) {
        
/* Max number of results on a page */
        
$this->show_max $iMax;
        
/* Current page, of course */
        
$this->current_page $iCurrentPage;
        
/* Total number of results to show */
        
$this->total_results count($aData);
        
/* Total number of pages */
        
$this->total_pages ceil($this->total_results $this->show_max);
        
/* This will output the number of the result
            which will be shown first on the page */
        
$this->firstResult = ($this->show_max * ($this->current_page 1)) + 1;
        
/* This will output the number of the result
            which will be shown last on the page */
        
$this->lastResult $this->show_max $this->current_page;
    }
    
    
/* This function, when called, will output the link
        for the previous link, if needed. Using the arguments
        szBefore and szAfter, you can append tags such as <li>
        and </li> to the link, if needed */
    
function prevLink($szBefore$szAfter) {
        if (
$this->current_page 1) {
            
$iPrevNum $this->current_page 1;
            
$this->prev_link $szBefore.'<a href="?page='.$iPrevNum.'">Previous</a>'.$szAfter."\n";
        }
        return 
$this->prev_link;
    }
    
    
/* Displays the list of pages (all of them, for now),
        as links so you can navigate through the pages.
        However, the current page doesn't show up as a
        link */
    
function display() {
        for (
$i 1$i <= $this->total_pages; ++$i) {
            if (
$i == $this->current_page) {
                
$aOutput[] = $i;
            } else {
                
$aOutput[] = '<a href="?page='.$i.'">'.$i.'</a>';
            }
        }
        return 
$aOutput;
    }
    
    
/* This function, when called, will output the link
        for the next link, if needed. Using the arguments
        szBefore and szAfter, you can append tags such as <li>
        and </li> to the link, if needed */
    
function nextLink($szBefore$szAfter) {
        if (
$this->current_page $this->total_pages) {
            
$iNextNum $this->current_page 1;
            
$this->next_link $szBefore.'<a href="?page='.$iNextNum.'">Next</a>'.$szAfter."\n";
        }
        return 
$this->next_link;
    }
    
}

?>

And comments would be welcome, as long as criticsm. Now, I'm not entirely sure how well it would work with raw MySQL results, as I outputted my results using $aData[$i] and I don't recall if MySQL results using mysql_fetch_array() use numbers as I did, so any tips on how to make that work the best would be great without much modification.

Anyway, thanks for looking. This is my first true helpful class I've made that can work alone, as well.

Tanax 12-17-2007 12:49 PM

Well, first of all, you declare the variables with public, but you seem to have forgotten than for the functions. They need to be declared aswell, with private/protected/public.

Also, I would keep the variables as private, and the functions as public.

The links (next link/prev link, etc), shouldn't be an actual link that is returned. Because the user might not want to have ?page, but only ?p=

Anyways, I'll look more deeper when I get home!

bdm 12-17-2007 01:18 PM

I do agree with everything Tanax has mentioned.

I would like to add that you should stick to a consistent coding style. You use camel case for functions and some variables than some variable names are separated by underscores.

Good commenting though.

;-)

Andrew 12-17-2007 11:00 PM

Ah, forgot about the visibility for the functions! I've already added it in ow. Also, I kept most of the variables public because I needed to use some of them outside of the class.. and finally, if someone were to use this, I'm asssuming they know PHP and could edit the links, but it was mainly for my uses. Feel free to use it if you'd like (towards everybody).

bdm 12-17-2007 11:10 PM

Andrew: You should probably use getter and setter functions if you plan on accessing member variables outside of the class scope.


All times are GMT. The time now is 08:15 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0