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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 02-08-2008, 05:38 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Pagination class

Hello.

I'm releasing my pagination class.
I already wrote it in another topic while helping someone, so I thought I'd just make an own topic for it.

I've improoved it since I posted it in that other topic, and now I've written descriptions of each function.

Also, this *should* work without MySQL, eventhough my example is based on MySQL.

php Code:
<?php

/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/


    class pagination {
       
        // The total values.
        private $totalPages;
        private $totalResults;
        private $totalPerPage;
       
        // The current values.
        private $currentPage;
       
        // The first result on current page.
        private $firstResult;
       
        /**
         *  Sets the maximum allowed results per page
         *
         *  @param integer $max, default 10
        **/

        public function setMax($max = 10) {
           
            if(is_numeric($max)) {
               
                $this->totalPerPage = $max;
               
            }
           
        }
       
        /**
         *  Generate the first result on the current page
         *
         *  @param integer $page, which page we're currently viewing
         *  @return integer first result
        **/

        public function setPage($page) {
           
            if(is_numeric($page)) {
               
                $this->currentPage = mysql_real_escape_string($page);
                $this->firstResult = (($this->currentPage * $this->totalPerPage) - $this->totalPerPage);
               
                return $this->firstResult;
               
            }
           
        }
       
        /**
         *  Generates how many pages based on the total amount of results
         *
         *  @param array $results, an array of all the results
         *  @return array of pages
        **/

        public function getPages($results) {
           
            $this->totalResults = count($results);
            $totalPages = $this->totalResults / $this->totalPerPage;
            $this->totalPages = ceil($totalPages);
           
            $x = 1;
            $array = array();
           
            while($x <= $this->totalPages) {
               
                $array[] = $x;
                $x++;
               
            }
           
            return $array;
           
        }
       
        /**
         *  Checks if a link is valid
         *
         *  @param integer $pagenr, the number of the page you want to check if it exist
         *  @return true or false
        **/

        public function checkLink($pagenr) {
           
            if($pagenr <= $this->totalPages && $pagenr >= 1) {
               
                return true;
               
            }
           
            return false;
           
        }
       
        /**
         *  Generate the current page number
         *
         *  @return array, [0] = the current page, [1] = total pages
        **/

        public function getCurrentPage() {
           
            $array = array();
            $array[] = $this->currentPage;
            $array[] = $this->totalPages;
           
            return $array;
           
        }
       
    }

?>


Example usage:
php Code:
<?php

/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/

   
    include('pagination.php');
    $p = $_GET['p'];
    (isset($p)) ? $p : 1;
    $max = 10;
   
    // Create the object and set some basic values.
    $pagination = new pagination();
    $pagination->setMax($max)
   
    // Get the total results, and then calculate how many pages that becomes based on how many results per page.
    $totSql = "SELECT * FROM `table`";
    $totQuery = mysql_query($totSql) or die(mysql_error());
    $totResults = mysql_fetch_array($totQuery);
    $totPages = $pagination->getPages($totResults);
   
    // Set the current page, and get the first result on it.
    $first = $pagination->setPage($p);
   
    // Get the results of the current page.
    $exSql = "SELECT * FROM `table` LIMIT $first, $max";
    $exQuery = mysql_query($exSql) or die(mysql_error());
    $exResults = mysql_fetch_array($exQuery);
   
    // Echo out the current page, and the total amount of pages.
    $page = $pagination->getCurrentPage();
    echo 'Page: '.$page[0].' of '.$page[1];
   
    // Get previous page link, and check if it's valid.
    $prevLink = $p - 1;
    if($pagination->checkLink($prevLink)) {
       
        echo '<a href="example.php?page='.$prevLink.'">Previous Page</a>';
       
    }
   
    // Print all the pages
    foreach($totPages as $pageNumber) {
       
        if($pageNumber == $p) {
           
            // The markup for showing that this is the current page is currently <strong>
            echo '<a href="example.php?page='.$pageNumber.'"><strong>'.$pageNumber.'</strong></a>';
           
        }
       
        else {
           
            echo '<a href="example.php?page='.$pageNumber.'">'.$pageNumber.'</a>';
           
        }
       
    }
   
    // Get the next page link, and check if it's valid.
    $nextLink = $p + 1;
    if($pagination->checkLink($nextLink)) {
       
        echo '<a href="example.php?page='.$nextLink.'">Next Page</a>';
       
    }
   
    foreach($exResults as $news) {
       
        echo $news['news_title'];
        echo '<br /><br />';
       
    }
   

?>


I started working on an example to use this with a gallery, where the image files were in a image dir. But I got stuck.
More specificly I got stuck when I was trying to get the results within the first result on a page, and the last result on a page, based on the value of the max nr of results per page.
Maybe someone else can try to work it out?

Anyways, this is my pagination class.
Any comments?
__________________

Last edited by Tanax : 02-19-2009 at 11:51 PM.
Tanax is offline  
Reply With Quote
The Following 3 Users Say Thank You to Tanax For This Useful Post:
Alan @ CIT (02-08-2008), codefreek (10-31-2008), Y.P.Y (01-22-2009)
 



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 11:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design