Pagination Class

This is the ultimate pagination class that allows you to easily deliver your content in bite-sized chunks. Go ahead and play around.


The code for the above is written thus:

$page->link('pagination.css'); 
$links = new Pagination (1000); 
$html .= $links->display();

Two excellent resources are at:

object Pagination ( string $query [, integer $display= 10 ] )

This is the constructor for the class, and it creates a pagination object.

$query A MySQL query that returns the total number of records, or if you already know then you can just put the number here.
$display How many records you would like to display at a time.
Example
$links = new Pagination ("SELECT COUNT(*) FROM table");

string limit ( )

This method will return the appropriate ' LIMIT start, display' string for your query to return the desired records.

Returns A string to finish off your MySQL query.
Example
$result = db_query ("SELECT * FROM table" . $links->limit());

string display ( [ integer $split= 5 ] )

This is the paginator method. Put it where you want your links to be displayed.

$split How many links you would like to be on either side of the current page.
Returns A string of code to add to your $html.
Example
$html .= $links->display();
Click to Download the PHP Pagination Class

 Subscribe to our feed

Pagination.php

<?php 
 
class Pagination { 
 
  private $num_pages = 1; 
  private $start = 0; 
  private $display; 
  private $start_display; 
 
  function __construct ($query, $display=10) { 
    if (!empty($query)) { 
      $this->display = $display; 
      if (isset($_GET['display']) && is_numeric($_GET['display'])) $this->display = (int) $_GET['display']; 
      if (isset($_GET['np']) && is_numeric($_GET['np']) && $_GET['np'] > 0) {  
        $this->num_pages = (int) $_GET['np']; 
      } else { 
        if (is_numeric($query)) { 
          $num_records = $query; 
        } else { 
          $result = db_query ($query); 
          if ($result->num_rows > 1 || strstr($query, 'COUNT') === false) { 
            $num_records = $result->num_rows; 
          } else { 
            $row = $result->fetch_row(); 
            $num_records = $row[0]; 
          } 
        } 
        if ($num_records > $this->display) $this->num_pages = ceil ($num_records/$this->display); 
      }  
      if (isset($_GET['s']) && is_numeric($_GET['s']) && $_GET['s'] > 0) $this->start = (int) $_GET['s']; 
      $this->start_display = " LIMIT {$this->start}, {$this->display}"; 
    } 
  } 
 
  public function display ($split=5) { 
    global $page; 
    $html = ''; 
    if ($this->num_pages <= 1) return $html; 
    // $page->link('pagination.css'); 
    $url = $page->url ('add', '', 'np', $this->num_pages); 
    $current_page = ($this->start/$this->display) + 1; 
    $begin = $current_page - $split; 
    $end = $current_page + $split; 
    if ($begin < 1) { 
      $begin = 1; 
      $end = $split * 2; 
    } 
    if ($end > $this->num_pages) { 
      $end = $this->num_pages; 
      $begin = $end - ($split * 2); 
      $begin++; // add one so that we get double the split at the end 
      if ($begin < 1) $begin = 1; 
    } 
    if ($current_page != 1) { 
      $html .= '<a class="first" title="First" href="' . $page->url('add', $url, 's', 0) . '">&laquo;</a>'; 
      $html .= '<a class="prev" title="Previous" href="' . $page->url('add', $url, 's', $this->start - $this->display) . '">Previous</a>'; 
    } else { 
      $html .= '<span class="disabled first" title="First">&laquo;</span>'; 
      $html .= '<span class="disabled prev" title="Previous">Previous</span>'; 
    } 
    for ($i=$begin; $i<=$end; $i++) { 
      if ($i != $current_page) { 
        $html .= '<a title="' . $i . '" href="' . $page->url('add', $url, 's', ($this->display * ($i - 1))) . '">' . $i . '</a>'; 
      } else { 
        $html .= '<span class="current">' . $i . '</span>'; 
      } 
    } 
    if ($current_page != $this->num_pages) { 
      $html .= '<a class="next" title="Next" href="' . $page->url('add', $url, 's', $this->start + $this->display) . '">Next</a>'; 
      $last = ($this->num_pages * $this->display) - $this->display; 
      $html .= '<a class="last" title="Last" href="' . $page->url('add', $url, 's', $last) . '">&raquo;</a>'; 
    } else { 
      $html .= '<span class="disabled next" title="Next">Next</span>'; 
      $html .= '<span class="disabled last" title="Last">&raquo;</span>'; 
    } 
    return '<div class="pagination">' . $html . '</div>'; 
  } 
 
  public function limit () { 
    return $this->start_display; 
  } 
 
} 
 
?>

pagination.css

div.pagination { 
	padding: 3px; 
	margin: 3px; 
	text-align: center; 
	font-family: Tahoma,Helvetica,sans-serif; 
	} 
	 
div.pagination a { 
	color: #0645ad; 
	border: 1px solid #a9b8dd; 
	margin-right: 3px; 
	padding: 2px 6px; 
	background-position: bottom; 
	text-decoration: none; 
	} 
	 
div.pagination a:hover, div.pagination a:active { 
	border: 1px solid #3c61a5; 
	background-image: none; 
	background-color: #6b92d7; 
	color: #fff; 
	} 
	 
div.pagination span.current { 
	color: #000; 
	font-weight: bold; 
	margin-right: 3px; 
	padding: 2px 6px; 
	} 
	 
div.pagination span.disabled { 
	border: 1px solid #ddd; 
	color: #bbb; 
	margin-right: 3px; 
	padding: 2px 6px; 
	} 
	 
div.pagination .next { 
	margin: 0 3px 0 8px; 
	} 
	 
div.pagination .prev { 
	margin: 0 8px 0 0; 
	}

comments powered by Disqus
Copyright © 2011 PHP-Ease.com - PHP Made Easy