• home
  • forum
  • my
  • kt
  • download
  • Object Orientated: Creating an Advanced Pagination Class

    Author: 2008-08-19 09:22:21 From:

    Hey all, well i thought i would now write a advance pagination. After racking my brain for awhile i finally figured out a nice class script for creating one.

    This class will display the results of any MySQL table including all the fields and rows. It will work out how many fields there are and display the results accordingly.

    You can view a demo HERE

    When changing the table in the demo if you change to CD list and go through them it like pressing number 2 or next it will go back to DVD's as this is a class tutorial. Was mainly to show you that it works

    Start:
    PHP Code:
    class pagination{


    Firstly we need to get up some variables.

    PHP Code:
    var $p=1$max_r,$limits;
    var 
    $count_all=0,$sql,$total,$table,$totalres,$totalpages;
    var 
    $r,$i;
    var 
    $show=10
    Now we need to start setting up some functions. First one of course is our MySQL connect function

    PHP Code:
    function connect($host,$username,$password,$name){
      
    $connectect mysql_connect($host$username$password) or die(mysql_error());
      
    $selected mysql_select_db($name) or die(mysql_error());

    Now we have done that we need to create a function to set the maxium number of posts to be displayed on each page. I have also included working out the limits and maximum values ready for the query.

    PHP Code:
    function setMax($max_r){
                     
       
    $this->$_GET['p'];
       
    $this->max_r $max_r;
                
        if(empty(
    $this->p))
        {
             
    $this->1;
        }
                
       
    $this->limits = ($this->1) * $this->max_r;


    Now that we have done that we can start with the quires and setting the data.

    PHP Code:
    function setData($table){
                        
       
    $this->table $table;
       
    $this->sql "SELECT * FROM ".$this->table." LIMIT ".$this->limits.",".$this->max_r."";
       
    $this->sql mysql_query($this->sql) or die(mysql_error());
       
    $this->total "SELECT * FROM ".$this->table."";
       
    $this->totalres =  mysql_query($this->total) or die(mysql_error());
       
    $this->count_all mysql_num_rows($this->totalres); // count all the rows
       
    $this->totalpages ceil($this->count_all $this->max_r); // work out total pages 

    Ok now comes the interesting and clever part. We now need to display the table. There are a couple of things we need to take into account here. We need to know how many fields there are in the table we are using and also how many rows and display them nicely.

    PHP Code:
    function display(){

    echo 
    "<b>Total Values(s):</b>".$this->count_all."<br><br>";
                      
    echo 
    "<b>Page:</b> ".$this->p."<br>";
        
    $fields=mysql_num_fields($this->totalres); // workout number of fields
                            
                            
    echo "<table border=1 width=100%><tr>";

      for (
    $i=0$i mysql_num_fields($this->sql); $i++) //Table Header
      

         print 
    "<th>".mysql_field_name($this->sql$i)."</th>"//display field name
      
    }
         echo 
    "</tr>";
      while (
    $row mysql_fetch_row($this->sql)) 
      { 
         echo 
    "<tr>";
        for (
    $f=0$f $fields$f++) 
        {
           echo 
    "<td>$row[$f]</td>"
        }
      echo 
    "</tr>\n";
      }
      echo 
    "</table><p>";
                     

    Now all we want to do now is display the links.

    PHP Code:
     function displayLinks($show){
                    
        
    $this->show $show// How many links to show
            
        
    echo "<br><br>";
               
        if(
    $this->1// If p > then one then give link to first page
        
    {
            echo 
    "<a href=?p=1> [FIRST] </a>  ";    
        }
            else
            { 
    // else show nothing
                
    echo "";
        }
        if(
    $this->!= 1)
            { 
    // if p aint equal to 1 then show previous text
                      
                
    $previous $this->p-1;
            echo 
    "<a href=?p=$previous> [ PREVIOUS ] </a>";
                            
        }
        else
            { 
    //else show nothing
            
    echo "";
        } 
        for(
    $i =1$i <= $this->show$i++) // show ($show) links
        
    {
                         
                if(
    $this->$this->totalpages)
                    { 
    // if p is greater then totalpages then display nothing
                    
    echo "";
            }
            else if(
    $_GET["p"] == $this->p)
                    { 
    //if p is equal to the current loop value then dont display that value as link
                       
    echo $this->;
            }
            else{
                       echo 
    " <a href=?p=".$this->p."> ( ".$this->p.") </a>"// else display the rest as links
            
    }
                
            
    $this->p++; //increment $p  
        
    }
        echo 
    "....."// display dots
                        
        
    if($_GET["p"] == $this->totalpages)
            {
    // if page is equal to totalpages then  dont display the last page at the end of links
            
    echo "";
        }
        else 
    // else display the last page link after other ones
        
    {
            echo 
    "<a href=?p=".$this->totalpages."> ( ".$this->totalpages.") </a>"
        }
        if(
    $_GET["p"] < $this->totalpages)// if p is less then total pages then show next link
        
    {
            
    $next $_GET["p"] + 1;
            echo 
    "<a href=?p=$next> [ NEXT >] </a>";    
        }
                      
        echo 
    "<br><br>";


    Thats the class now finished. All we want to do now is start displaying it and creating the instance.

    PHP Code:
    $page= new pagination;
    $page->connect("host","username","password","dbname");
    $page->setMax(25);      // 25 being number of results to be displaued
    $page->setData("tablename");
    $page->display();
    $page->displayLinks(5); // 5 being number of links to display 
    This is my brushing up on my classes haha.

    Full CODE:

    PHP Code:
    <?php

        
    class pagination{
        
                var 
    $p=1$max_r,$limits;
                var 
    $count_all=0,$sql,$total,$table,$totalres,$totalpages;
                var 
    $r,$i;
                var 
    $show=10;

                function 
    connect($host,$username,$password,$name){
                    
                    
    $connectect mysql_connect($host$username$password) or die(mysql_error());
                    
    $selected mysql_select_db($name) or die(mysql_error());
                }
            
                function 
    setMax($max_r){
                     
                      
    $this->$_GET['p'];
                      
    $this->max_r $max_r;
                
                          if(empty(
    $this->p))
                          {
                            
    $this->1;
                          }
                
                      
    $this->limits = ($this->1) * $this->max_r;

                }    
                
                function 
    setData($table){
                        
                      
    $this->table $table;
                      
    $this->sql "SELECT * FROM ".$this->table." LIMIT ".$this->limits.",".$this->max_r."";
                        
    $this->sql mysql_query($this->sql) or die(mysql_error());
                      
    $this->total "SELECT * FROM ".$this->table."";
                      
    $this->totalres =  mysql_query($this->total) or die(mysql_error());
                      
    $this->count_all mysql_num_rows($this->totalres);
                      
    $this->totalpages ceil($this->count_all $this->max_r);
                }
                
                function 
    display(){

                      echo 
    "<b>Total Values(s):</b>".$this->count_all."<br><br>";
                      
                      echo 
    "<b>Page:</b> ".$this->p."<br>";
        
                        
    $fields=mysql_num_fields($this->totalres);
                            
                            
                          echo 
    "<table border=1 width=100%><tr>";
                            for (
    $i=0$i mysql_num_fields($this->sql); $i++) //Table Header
                            

                                print 
    "<th>".mysql_field_name($this->sql$i)."</th>"
                            }
                            echo 
    "</tr>";
                            while (
    $row mysql_fetch_row($this->sql)) 
                            { 
                                echo 
    "<tr>";
                                for (
    $f=0$f $fields$f++) 
                                {
                                    echo 
    "<td>$row[$f]</td>"
                                }
                                echo 
    "</tr>\n";
                            }
                            echo 
    "</table><p>";
                     
                }
                
                function 
    displayLinks($show){
                    
                      
    $this->show $show// How many links to show
            
                       
    echo "<br><br>";
               
                      if(
    $this->1// If p > then one then give link to first page
                      
    {
                            echo 
    "<a href=?p=1> [FIRST] </a>  ";    
                      }
                      else{ 
    // else show nothing
                            
    echo "";
                      }
                      if(
    $this->!= 1){ // if p aint equal to 1 then show previous text
                      
                              
    $previous $this->p-1;
                              echo 
    "<a href=?p=$previous> [ PREVIOUS ] </a>";
                            
                      }
                      else{ 
    //else show nothing
                              
    echo "";
                      } 
                      for(
    $i =1$i <= $this->show$i++) // show ($show) links
                      
    {
                         
                            if(
    $this->$this->totalpages){ // if p is greater then totalpages then display nothing
                                
    echo "";
                            }
                            else if(
    $_GET["p"] == $this->p){ //if p is equal to the current loop value then dont display that value as link
                                
    echo $this->;
                            }
                            else{
                                echo 
    " <a href=?p=".$this->p."> ( ".$this->p.") </a>"// else display the rest as links
                            
    }
                
                            
    $this->p++; //increment $p  
                      
    }
                        echo 
    "....."// display dots
                        
                       
    if($_GET["p"] == $this->totalpages){// if page is equal to totalpages then  dont display the last page at the end of links
                                
    echo "";
                       }
                       else 
    // else display the last page link after other ones
                       
    {
                            echo 
    "<a href=?p=".$this->totalpages."> ( ".$this->totalpages.") </a>"
                       }
                       if(
    $_GET["p"] < $this->totalpages)// if p is less then total pages then show next link
                       
    {
                            
    $next $_GET["p"] + 1;
                            echo 
    "<a href=?p=$next> [ NEXT >] </a>";    
                       }
                      
                       echo 
    "<br><br>";

                }
            
        }

    $page= new pagination;
    $page->connect("host","username","password","dbname");
    $page->setMax(25);
    $page->setData("tablename");
    $page->display();
    $page->displayLinks(5);
    }

    ?>
    Nice approach.

    Note that I'm not an OOP wizard, but hopefully these tips will help you and if someone disagrees, let me know.
    - What version of PHP are you using? Because I've noticed that you're still using the var.
    - You must sanitize your data ($_GET['p']) before executing a query (see below). Either use mysql_real_escape_string() or make up a regular expression to weed out all non-numbers.
    - It'd be neat if you modified the class to support method chaining.

    For example:
    PHP Code:
    $page= new pagination;
    $page->connect("host","username","password","dbname");
    $page->setMax(25)
         ->
    setData("tablename")
         ->
    display()
         ->
    displayLinks(5
    - I do believe that for greater flexibility, that you should perhaps accept the dots ('.....') as a parameter in your constructor.
    - I'm anal about standards, but you should replace <br> with <br />

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (20)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot