Hey PHP-ers,
Vandaag heb ik mijn eerste OOP script geschreven.
Zouden jullie mij feedback kunnen geven op de code? Is het misschien iets voor in het script library?
Hier is het script
<?php
class Pagination {
/*
usage:
$pagination = new Pagination($qry, "index.php?page=overzicht", $itemsPerPage, $numbersLeftNav);
options
$pagination->firstLabel = '<< eerste';
$pagination->prevLabel = '< vorige';
$pagination->nextLabel = 'volgende >';
$pagination->lastLabel = 'laatste >>';
render each item
echo $pagination->renderFirst().' ';
echo $pagination->renderPrev().' ';
foreach($pagination->renderLeftNav() as $key => $val) {
echo $val.' ';
}
echo $pagination->renderCurrentNav().' ';
foreach($pagination->renderRightNav() as $key => $val) {
echo $val.' ';
}
echo $pagination->renderNext().' ';
echo $pagination->renderLast();
or render all items
echo $pagination->render();
foreach ($pagination->getRecords() as $key => $row) {
echo strip($row["naam"]);
}
*/
public $firstLabel = '<< first';
public $lastLabel = 'last >>';
public $nextLabel = 'volgende >';
public $prevLabel = '< vorige';
protected $_sql = '';
protected $_itemsPerPage = '';
protected $_allRecords = '';
protected $_allPages = '';
protected $_append = ''; // items in the nav on the left and right from the current
protected $_currentPage = '';
public function __construct($_sql, $_self, $_items_per_page = 10, $_append = 3) {
$this->_sql = $_sql;
$this->_self = $_self;
$this->_itemsPerPage = $_items_per_page;
$this->_append = $_append;
if (!isset($_GET["pagination"]) || (isset($_GET["pagination"]) && !is_numeric($_GET["pagination"]))) {
if (preg_match("/\?/", $this->_self)) {
header("Location: ".$this->_self."&pagination=1&limit=".$this->_itemsPerPage);
}
else {
header("Location: ".$this->_self."?pagination=1&limit=".$this->_itemsPerPage);
}
}
else {
$this->_currentPage = $_GET["pagination"];
}
}
protected function database() {
include '../inc/connect.php';
$output = array();
$sql = $this->_sql;
$all_records = $db->query($sql);
if ($all_records == TRUE) {
$this->_allRecords = $all_records->num_rows;
$this->_allPages = ceil($this->_allRecords / $this->_itemsPerPage);
}
else {
return 'Error: '.$db->error;
}
$start = $this->_currentPage * $this->_itemsPerPage - $this->_itemsPerPage;
$sql .= "LIMIT ".$start.", ".$this->_itemsPerPage;
$sql = $db->query($sql);
if ($sql == TRUE) {
while ($row = mysqli_fetch_assoc($sql)) {
$output[] = $row;
}
return $output;
}
else {
return 'Error: '.$db->error;
}
}
public function renderFirst() {
if ($this->_currentPage != 1) {
return '<a href="'.$this->_self.'&pagination=1&limit='.$this->_itemsPerPage.'">'.$this->firstLabel.'</a>';
}
}
public function renderPrev() {
$prev = $this->_currentPage - 1;
if ($prev != 0) {
return '<a href="'.$this->_self.'&pagination='.$prev.'&limit='.$this->_itemsPerPage.'">'.$this->prevLabel.'</a>';
}
}
public function renderLeftNav() {
$this->database();
$current = $this->_currentPage;
$navLeft = array();
if ($this->_currentPage > 1) {
$i = 0;
while($i != $this->_append) {
$i++;
$current--;
if ($current >= 1) {
$navLeft[] = '<a href="'.$this->_self.'&pagination='.$current.'&limit='.$this->_itemsPerPage.'">'.$current.'</a>';
}
}
asort($navLeft);
}
return $navLeft;
}
public function renderCurrentNav() {
return $this->_currentPage; // output current page
}
public function renderRightNav() {
$this->database();
$current = $this->_currentPage;
$navRight = array();
if ($this->_currentPage < $this->_allPages) {
$i = 0;
while($i != $this->_append) {
$i++;
$current++;
if (($current -1) != $this->_allPages && ($current -1) < $this->_allPages) {
$navRight[] = '<a href="'.$this->_self.'&pagination='.$current.'&limit='.$this->_itemsPerPage.'">'.$current.'</a>';
}
}
}
return $navRight;
}
public function renderNext() {
$next = $this->_currentPage + 1;
if (($next - 1) != $this->_allPages) {
return '<a href="'.$this->_self.'&pagination='.$next.'&limit='.$this->_itemsPerPage.'">'.$this->nextLabel.'</a>';
}
}
public function renderLast() {
if ($this->_currentPage != $this->_allPages) {
return '<a href="'.$this->_self.'&pagination='.$this->_allPages.'&limit='.$this->_itemsPerPage.'">'.$this->lastLabel.'</a>';
}
}
public function getRecords() {
return $this->database();
}
public function render() {
$output = '';
$output .= $this->renderFirst().' ';
$output .= $this->renderPrev().' ';
foreach($this->renderLeftNav() as $key => $val) {
if ($val <= $this->_allPages) {
$output .= $val.' ';
}
}
$output .= $this->renderCurrentNav();
foreach($this->renderRightNav() as $key => $val) {
if ($val <= $this->_allPages) {
$output .= ' '.$val;
}
}
$output .= ' '.$this->renderNext().' ';
$output .= $this->renderLast();
return $output;
}
}
?>
Bedankt alvast!
3.009 views