pagination class help
Posted: Thu Nov 17, 2005 3:47 am
I have taken the below Pagination Class from a tut
at http://www.ecreate.co.uk/diy/pagination2.php
and have one problem with getting it to correctly work with my current code.
That problem is... how to get the prev/next links to actually show
the prev/next article.
As of now I can get them to show, but not get them to
move articles up and down.
As I am still learning how to correctly use classes,
I am sure this is a simple error on my part :p
displaylinkednews:
Paginate Class:
at http://www.ecreate.co.uk/diy/pagination2.php
and have one problem with getting it to correctly work with my current code.
That problem is... how to get the prev/next links to actually show
the prev/next article.
As of now I can get them to show, but not get them to
move articles up and down.
As I am still learning how to correctly use classes,
I am sure this is a simple error on my part :p
displaylinkednews:
Code: Select all
function displaylinkednews($url) {
//START PAGINATE CLASS
$paginate = new Paginate(1, 10, "http://localhost/showtest.php", 2);
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
//DISPLAY PAGINATE LINKS
echo $paginate->displayTable();
$newposts = array ( "Title=> $row['title'], "Submitted By" => $row['name'], "News" => $row['news'] );
foreach ( $newposts as $postfield => $postinput )
{
$postinput = submitTags($postinput, 1, 1, $siteurl, $directory, $imgalt, $vf);
echo "<b>{$postfield}</b> {$postinput}<br />";
}
}
}Code: Select all
class Paginate{
//Set some default values and define the variables being used by the class
var $start = 1;
var $limit = 10;
var $num = 10;
var $url = "";
var $maxPages = 10;
var $numPages = 10;
var $currentPage = 1;
/**
* @param $limit - The number of records per page,
* @param $num - The total number of records.
* @param $url - The url that underpins the links (with parameters)
*/
function Paginate($limit, $num, $url, $maxPages){
//We get the start number from the URL. Casting to int makes it safe
if (isset($_GET['start'])){
$this->start = (int) $_GET['start'];
}
//$this->start = 11;
$this->limit = (int) $limit;
$this->num = (int) $num;
$this->url = $url;
$this->maxPages = (int) $maxPages; //set to a default value of 20
//The URL might not have any parameters, but since we need to add one,
//we must know whether to append a ? or a &
$this->interpretUrl();
$this->calculate_numPages();
$this->calculateCurrent();
}
/**
* Check if the URL already has parameters. Set an instance variable
* with ? or & accordingly
*/
function interpretUrl(){
//Check if we need to add ? or & to the end of the url to add start
//parameter.
if (strstr ($this->url, "?")){
$this->urlAppendString = "?";
} else {
$this->urlAppendString = "?";
}
}
/**
* Determine number of pages that should be displayed.
*/
function calculate_numPages(){
$this->numPages = ceil($this->num / $this->limit);
}
/**
* Determine if current page is greater than page 1
*/
function calculateCurrent(){
if ($this->start > $this->limit){
$this->currentPage = ceil($this->start / $this->limit);
}
}
/**
* Return html link for Previous Page
* @return String (html)
*/
function getPreviousPage(){
if ($this->currentPage > 1){
return "<a href='".$this->url.$this->urlAppendString."start=".($this->start-$this->limit)."' title='Previous page'><Previous </a>";
}
}
/**
* Return html link for Next Page
* @return String (html)
*/
function getNextPage(){
if ($this->currentPage < $this->numPages){
return "<a href='".$this->url.$this->urlAppendString."start=".($this->start+$this->limit)."' title='Next page'>>Next</a>";
}
}
/**
* getFirstPage
* @return String (html) Start is always 1.
*/
function getFirstPage(){
if ($this->currentPage > 1){//If not on first page already
return "<a href='".$this->url.$this->urlAppendString."start=1' title='First page'><<First</a>";
}
}
/**
* getLastPage
* @return String (html)
*/
function getLastPage(){
/*
* This uses fmod to work out the modulo (remainder) of dividing the
* number of records by the max records per page - which is what would
* appear on the last page - the last page would not normally display
* the max records per page. Then, it subtracts this remainder
* from the number of records and adds 1 to arrive at the start value
*/
if ($this->currentPage < $this->numPages){//If not on last page already
$start_val = (($this->num - fmod($this->num, $this->limit)) + 1);
if ($start_val > $this->num){
$start_val = $start_val - $this->limit;
}
return "<a href='".$this->url.$this->urlAppendString."start=".$start_val."' title='Last page'>>>Last</a>";
}
}
/**
* Main method that returns an array of links that are laid out elsewhere
* @return Array (html strings)
*/
function getPaginationLinks(){
//Don't get carried away with paginating large sets of data.
//This sets reasonable limits
$ar = array();
//Set lower limit
//- so we don't start at page 1 link if we're at page 50 of 60
if(($this->currentPage-ceil($this->maxPages/2))>1){
$pageStart =($this->currentPage-ceil($this->maxPages/2));
} else {
$pageStart = 1;
}
//Set upper limit
//- so we don't show page 60 link if we're on page 1
if (($this->currentPage+ceil($this->maxPages/2))<$this->numPages){
$pageEnd =($this->currentPage+ceil($this->maxPages/2));
} else {
$pageEnd = $this->numPages;
}
/*
* Create an associative array of links within lower and upper limits
* We use an associative array because if we create an ordered list
* for navigation, we need to know which number to seed the ol with
*/
for ($i=$pageStart;$i<=$pageEnd;$i++){
if ($i == $this->currentPage){
$ar[$i] = "[".$i."]";
} else {
$ar[$i] = "<a href='".$this->url.$this->urlAppendString.
"start=".((($i-1)*$this->limit)+1)."'
title='Go to page ". $i ."'>".$i."</a>";
}
}
return $ar;
}
/**
* Returns HTML for navigation.
* @return String (html)
*/
function displayUl(){
/*
* Get the various html fragments and array of page links. Comment out
* first, last, previous or next as desired.
*/
$first = $this->getFirstPage();
$last = $this->getLastPage();
$previous = $this->getPreviousPage();
$next = $this->getNextPage();
$arLinks = $this->getPaginationLinks();
//Start constructing output
$output = "<ul>";
if (isset($first) && $first != ""){
$output .= "<li>".$first."</li>";
}
if (isset($previous) && $previous != ""){
$output .= "<li>".$previous."</li>";
}
/*
* Using while in this way is the most efficient way to traverse
* the array
*/
while(list($i, $v) = each($arLinks)){
$output .= "<li>".$arLinks[$i]."</li>";
}
if (isset($next) && $next != ""){
$output .= "<li>".$next."</li>";
}
if (isset($last) && $last != ""){
$output .= "<li>".$last."</li>";
}
$output .= "</ul>";
return $output;
}
/**
* Returns HTML for navigation.
* @return String (html)
*/
function displayOl(){
/*
* Get the various html fragments and array of page links. Comment out
* first, last, previous or next as desired.
*/
$first = $this->getFirstPage();
$last = $this->getLastPage();
$previous = $this->getPreviousPage();
$next = $this->getNextPage();
$arLinks = $this->getPaginationLinks();
//Start constructing output
$output = "<ol start='".key($arLinks)."'>";
reset($arLinks);
if (isset($first) && $first != ""){
$output .= "<div class='navLink'>".$first."</div>";
}
if (isset($previous) && $previous != ""){
$output .= "<div class='navLink'>".$previous."</div>";
}
/*
* Using while in this way is the most efficient way to traverse
* the array
*/
while(list($i, $v) = each($arLinks)){
$output .= "<li>".$arLinks[$i]."</li>";
}
if (isset($next) && $next != ""){
$output .= "<div class='navLink'>".$next."</div>";
}
if (isset($last) && $last != ""){
$output .= "<div class='navLink'>".$last."</div>";
}
$output .= "</ol>";
return $output;
}
/**
* Returns HTML for navigation.
* @return String (html)
*/
function displayTable(){
/*
* Get the various html fragments and array of page links. Comment out
* first, last, previous or next as desired.
*/
$first = $this->getFirstPage();
$last = $this->getLastPage();
$previous = $this->getPreviousPage();
$next = $this->getNextPage();
$arLinks = $this->getPaginationLinks();
//Start constructing output
$output = "<table summary='Pagination table'><tr>";
reset($arLinks);
if (isset($first) && $first != ""){
$output .= "<td>".$first."</td>";
}
if (isset($previous) && $previous != ""){
$output .= "<td>".$previous."</td>";
}
/*
* Using while in this way is the most efficient way to traverse
* the array
*/
while(list($i, $v) = each($arLinks)){
$output .= "<td>".$arLinks[$i]."</td>";
}
if (isset($next) && $next != ""){
$output .= "<td>".$next."</td>";
}
if (isset($last) && $last != ""){
$output .= "<td>".$last."</td>";
}
$output .= "</tr></table>";
return $output;
}
}