Source Code

hPagination - A Simple PHP pagination class

Options

  • define page url
  • define row per page
  • works with SEO url (http://example.com/topic/page/1) and normal url (http://example.com/index.php?page=topic&page=1)
  • change URL param (http://example.com/topic/page/1 or http://example.com/topic/pg/1)

Usage

require_once('hPagination.class.php'); // Initialize the class $paginator = new hPagination; // Setting class variables $paginator->page_url = 'http://localhost/members/'; // full page url $paginator->rows_per_page = 10; // Defining rows per page $paginator->total_rows = 100; // Give total number of rows $paginator->current_page = (intval($_GET['pg'])<1)?1:intval($_GET['pg']); // Pass current requested page number (Never let pass 0) always > 1 $paginator->seourl = false; // if you are running seo URL then apply true/false $paginator->parameter = 'pg'; // http://localhost/member/[parameter]/2 | http://localhost/member/?[parameter]=2 // Add at last in query str $paginator->limit() // Display $paginator->display_pagination();

hPagination.class.php

class hPagination { var $page_url; var $rows_per_page = 20; var $total_rows; var $current_page; var $seourl = false; var $parameter = 'page'; var $page_class = 'paginator'; function display_pagination(){ echo '<div class="'.$this->page_class.'">'; echo 'You are in Page: <strong>Pages: '.$this->current_page.' of '.$this->total_pages().'</strong>  '; // first if($this->seourl) $first_page_link = $this->page_url.$this->parameter.'/1/'; else { $first_page_parse = parse_url($this->page_url); if($first_page_parse['query']!=NULL) $first_page_link = $this->page_url.'&'.$this->parameter.'=1'; else $first_page_link = $this->page_url.'?'.$this->parameter.'=1'; } echo "<a href='".$first_page_link."' class='button'>First</a>"; // first // Prev $prev_page_num = $this->current_page-1; if($prev_page_num<1) $prev_page_num = 1; if($this->seourl) $prev_page_link = $this->page_url.$this->parameter.'/'.$prev_page_num.'/'; else { $prev_page_p = parse_url($this->page_url); if($prev_page_p['query']!=NULL) $prev_page_link = $this->page_url.'&'.$this->parameter.'='.$prev_page_num.''; else $prev_page_link = $this->page_url.'?'.$this->parameter.'='.$prev_page_num.''; } echo "<a href='".$prev_page_link."' class='button'>Prev</a>"; // prev if($this->total_pages()>=15){ $differ=7; if(($this->current_page-$differ)>0 && ($this->current_page+$differ)<=$this->total_pages()) {$start=$this->current_page-$differ; $end=$this->current_page+$differ;} elseif(($this->current_page-$differ)<=0 && ($this->current_page+$differ)<=$this->total_pages()) {$start=1; $end=($differ*2)+1;} elseif(($this->current_page-$differ)>0 && ($this->current_page+$differ)>$this->total_pages()) {$start=$this->total_pages()-($differ*2); $end=$this->total_pages();} } else { $start=1; $end=$this->total_pages(); } for($i=$start;$i<=$end;$i++) { if($this->seourl) $c_page_link = $this->page_url.$this->parameter.'/'.$i.'/'; else { $c_page_p = parse_url($this->page_url); if($c_page_p['query']!=NULL) $c_page_link = $this->page_url.'&'.$this->parameter.'='.$i.''; else $c_page_link = $this->page_url.'?'.$this->parameter.'='.$i.''; } echo "<a href='".$c_page_link."' class='button'>$i</a>"; } // Next $next_page_num = $this->current_page+1; if($next_page_num>$this->total_pages()) $next_page_num = $this->total_pages(); if($this->seourl) $next_page_link = $this->page_url.$this->parameter.'/'.$next_page_num.'/'; else { $next_page_p = parse_url($this->page_url); if($next_page_p['query']!=NULL) $next_page_link = $this->page_url.'&'.$this->parameter.'='.$next_page_num.''; else $next_page_link = $this->page_url.'?'.$this->parameter.'='.$next_page_num.''; } echo "<a href='".$next_page_link."' class='button'>Next</a>"; // Next // Last if($this->seourl) $last_page_link = $this->page_url.$this->parameter.'/'.$this->total_pages().'/'; else { $last_page_parse = parse_url($this->page_url); if($last_page_parse['query']!=NULL) $last_page_link = $this->page_url.'&'.$this->parameter.'='.$this->total_pages(); else $last_page_link = $this->page_url.'?'.$this->parameter.'='.$this->total_pages(); } echo "<a href='".$last_page_link."' class='button'>Last</a>"; // Last echo '</div>'; } function total_pages(){ return ceil($this->total_rows/$this->rows_per_page); } function limit(){ $this->current_page = intval($this->current_page); if($this->current_page>1) $start = $this->rows_per_page * $this->current_page - $this->rows_per_page; else $start = 0; return ' LIMIT '.$start.','.$this->rows_per_page; } }
comments powered by Disqus