TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-17-2007, 06:10 AM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default 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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 12-17-2007, 12:49 PM   #2 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 748
Thanks: 85
Tanax is on a distinguished road
Default

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!
Tanax is offline  
Reply With Quote
Old 12-17-2007, 01:18 PM   #3 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

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.

bdm is offline  
Reply With Quote
Old 12-17-2007, 11:00 PM   #4 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

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).
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 12-17-2007, 11:10 PM   #5 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Andrew: You should probably use getter and setter functions if you plan on accessing member variables outside of the class scope.
bdm is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:30 PM.

 
     

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