Jump to content

pagination class needs to be modified


TheTitans

Recommended Posts

I have a pagination system for my website. It works perfectly for my news section. However, I would like to use it for another area of my webite (image gallery). A required id $_GET variable is required for the gallery to work. Because my news section does not required that variable, it is not included when building the link in the createPageNumbers() function. My question is how can I make this work for my image gallery?

 

The class:

 

class Pagination {

private $rows_per_page;
private $pageno;

public function __construct($rows, $page) {
	$this->rows_per_page = $rows;
	$this->pageno = $page;
}

public function pageExists() {
	if (!isset($this->pageno)) {
	   $this->pageno = 1;
	}
	return $this->pageno;
}

public function integerCheck($lastpage) {
	$this->pageno = (int)$this->pageno;

	if ($this->pageno > $lastpage) {
	   $this->pageno = $lastpage;
	}

	if ($this->pageno < 1) {
	   $this->pageno = 1;
	}
	return $this->pageno;
}

public function roundFraction($numrows) {
	return ceil($numrows/$this->rows_per_page);
}

public function createQuery() {
	return 'LIMIT ' . ($this->pageno - 1) * $this->rows_per_page .',' . $this->rows_per_page;
}

public function createPageNumbers($lastpage) {
	if ($this->pageno == 1) {
	   echo " <<PREV ";
	}
	else {
	   $prevpage = $this->pageno - 1;
	   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'> <<PREV </a> ";
	}
	?>
	-
	<?php
	for ($i = 1; $i < $lastpage + 1; $i++) {
		echo '<a href="?pageno=' . $i . '">[' . $i . ']</a> ';
	}
	?>
	-
	<?php

	if ($this->pageno == $lastpage) {
	   echo " NEXT>> ";
	}
	else {
	   $nextpage = $this->pageno + 1;
	   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT>></a> ";
	}
}
}

 

And to use it...

 

include('Pagination.php');
$pagination = new Pagination(10,$_GET['pageno']);

$result = $db->query("SELECT count(*) FROM news");
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$lastpage = $pagination->roundFraction($numrows);

// Checks to make sure $pageno is an integer between 1 and $lastpage
$pagination->integerCheck($lastpage);

$limit = $pagination->createQuery();

$result2 = $db->query("SELECT * FROM news ORDER BY id DESC $limit");

echo '<p style="text-align:center">';
$pagination->createPageNumbers($lastpage);
echo '</p>';

Link to comment
https://forums.phpfreaks.com/topic/134565-pagination-class-needs-to-be-modified/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.