Page 1 of 1

Pagination Class Critique

Posted: Fri Jan 16, 2009 2:08 am
by invisibled
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hey Everybody,

So I've been working on writing classes to speed up development in my projects, and my first one is a pagination class. I would like to hear what you guys think of it, as I'm new to writing classes and maybe you think I could be more efficient or use better practices. Code below

Code: Select all

 
<?php
/*
 
Pagination
Written and Maintained by 
Shan Robertson (http://invisibled.com)
        
    Include the file: 
        require $root.'lib/inc/class.pagination.php';
        
    Variables:
        $p->page_table  = database_table;
        $p->per_page    = string;
        
    Initiate an instance:
        $p->output();
    
    Add this to the end of your Mysql query:
        LIMIT $p->res, $p->per_page
        
        
*///
class pagination{
 
 
    function output(){
        
        
        
        //Access vars outside of class
        $page_table = $this->page_table;
        $per_page = $this->per_page;
        
        
        
        //Global Vars
        $get_page = $_GET['page'];
        
        
        
        //If the page param isnt set, set to page 1
        if(isset($get_page)):
            $this->res  = $per_page * $get_page - $per_page;
        else:
            $this->res  = $per_page * 1 - $per_page;
        endif;
        
        
            
        //The Query
        $page_query = mysql_query("SELECT id FROM $page_table");
 
        
 
        //Stores total number of results
        $num = mysql_num_rows($page_query);
 
 
 
        //Divides the number of results by the number per page
        $pages = ceil($num / $per_page);
 
        
 
        //Sets the increment value at 1
        $i = "1";
        
 
        
        //Next and Prev Links
        $next = $get_page + 1;
        $prev = $get_page - 1;
        
        
        
        //The Output
        print '<div class="pagination">';
            
            if(isset($get_page) && $get_page != "1"):
                print '<a href="'.$php_self.'?page='.$prev.'"><</a>';
            endif;
            
            while($i <= $pages):
                print '<a href="'.$php_self.'?page='.$i.'" ';
                
                if($get_page == $i):
                    print 'class="current"';
                endif;
                
                print '>'.$i.'</a>';
                
                $i++;
            endwhile;
            
            if($get_page != $pages):
                print '<a href="'.$php_self.'?page='.$next.'">></a>';
            endif;
            
        print '</div>'; 
    }
}
 
 
 
// Create a new class object
$p = new pagination();
 
 
 
?>
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Pagination Class Critique

Posted: Fri Jan 16, 2009 4:32 am
by susrisha
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


hi.. I have created a similiar class which i am using in my project as of now. Here is the code.Dont know if i can post it here.

Code: Select all

 
<?php 
/* Developped and implemented by susrisha@gmail.com
 * For public use..Have fun
 * Member variables of the class
 * @param : $page_num -> the present page number of the page.
 * @param : $offset -> the offset from which the records are to be counted
 * @param : $rows_per_page-> the number of rows you need to have in each page
 * @param : $max_pages -> the maximum pages for the records
 */
class cPagination
{
    public $page_num; //the page number
    public $offset;  //the offset
    public $rows_per_page; //number of rows per page
    public $max_pages; //max pages 
    
    /* construct function for the class
     * @param : $maxrows -> them number of rows in mysql query to get the results paginated
     * @param : $rows_in_page -> the number of rows each page will have
     * 
     * function sets the variables page_num, offset, rows_per_page, $max_pages for the object
     * 
     */
    
  public function __construct($maxrows,$rows_in_page)
 {
    $this->max_pages= ceil($maxrows/$rows_in_page);
    $this->rows_per_page = $rows_in_page;
    if(isset($_GET['page']))
    {
        $this->page_num= $_GET['page'];
    }
    else
    {
        $this->page_num = 1;
    }
    $this->offset = ($this->page_num-1)*$this->rows_per_page;
    
 }//end of construct
 
 /* function get_links
  * gives out the First, prev, next, Last links 
  */
    
    public function get_links()
    {
        $self = $_SERVER['PHP_SELF'];
        if($this->page_num>1)
        {
            $page= $this->page_num -1;
            $prev = "<a href = \"$self?page=$page\"><abbr title=\"Previous\">[<]</abbr></a>";
            $first = "<a href =\"$self?page=1\"><abbr title=\"First\">[<<]</abbr></a>";
        }
        else
        {
            $prev="&nbsp;";
            $first="&nbsp;";
        }//end of previous and first
        
        if($this->page_num<$this->max_pages)
        {
            $page = $this->page_num +1;
            $next = "<a href =\"$self?page=$page\"><abbr title=\"Next\">[>]</abbr></a>";
            $last = "<a href =\"$self?page=$this->max_pages\"><abbr title=\"Last\">[>>]</abbr></a>";
        }
    else
        {
            $next = "&nbsp;";
            $last = "&nbsp;";
        }
        echo $first.$prev.$next.$last;
        
        
    }//end of get_links
    
}//end of class
 
 
 
?>
 
Usage:

Code: Select all

 
$query = "SELECT * FROM transactions";
$result1=mysql_query($query,$con);
$max_rows = mysql_num_rows($result1);
$rows_per_page =10;
$paginator = new cPaginator($max_rows,$rows_per_page);
 
//this is the query you will be using for further page displaying
$query2 = "SELECT * FROM transactions LIMIT $paginator->offset,$paginator->rows_per_page";
$result2 = mysql_query($query2,$con);
 
//at the end of the page to add the links
$paginator->get_links();
 
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Pagination Class Critique

Posted: Fri Jan 16, 2009 10:35 am
by pickle
@ ~susrisha: It's great you have your own class, but this thread is about critiquing ~invisibled class, not to create a repository of Pagination classes. If you want your class to be critiqued as well, please start your own thread.

@ ~invisibled: This looks like a very specific class, only useful for your own project. That's not necessarily bad or selfish or anything, but it does reduce the usefulness of the class - even for yourself.

Your class also has no constructor - a function that sets it up. That's pretty much necessary.
You also haven't set up the object variables you use in output().

I'd recommend doing some more research on how to create classes, and maybe look at some other pagination classes to see how they're built.

Re: Pagination Class Critique

Posted: Fri Jan 16, 2009 4:17 pm
by invisibled
thanks for the tip's! I will look those up