Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.
Popular code excerpts may be moved to "Code Snippets" by the moderators.
i found a flaw. the max pages thing is off by 1. i don't get the first record in the db ever. i tweaked it a bit and got the next and prev working right and the first and last working right but its too late to figure out how to get the middle numbers (current page) stuff working right. other than that this is a very handy feature, lovin it.
class pagination {
/*
@@ $query - Your query you need paginated
@@ $perPage - How many records per page
@@ $current - The current page your on
@@ maxPage - The maximum amount of pages viewed at one time
*/
function pagination($query, $perPage, $current, $maxPage = 7, $gallery) {
$this->gallery = $gallery;
$this->query = $query;
$this->perPage = $perPage;
$this->maxPage = $maxPage;
$this->current = $current < 0 ? 0 : $current;
$this->begin = $this->current == 0 ? true : false;
$this->initialize();
}
function initialize() {
if ($result = $this->query()) {
$this->getNumRows();
$this->getNumPages();
$this->current = $this->current > $this->TotalNumPages ? $this->TotalNumPages : $this->current;
}
$this->formatSQL();
$this->getBoundaries();
$this->numbers = $this->buildNumbers();
$this->preFormat();
}
function query() {
$this->result = mysql_query($this->query) or die(mysql_error());
return $this->result;
}
function getNumRows() {
$this->TotalNumRows = mysql_num_rows($this->result);
return $this->TotalNumRows;
}
function formatSQL() {
$this->query .= ' LIMIT '.($this->current * $this->perPage).','.$this->perPage;
}
function getNumPages() {
$this->TotalNumPages = ceil($this->TotalNumRows / $this->perPage);
}
function viewing() {
$this->newLimit = $this->current * $this->perPage + $this->perPage;
return $this->viewing = 'Viewing: [ '.($this->current * $this->perPage) .' - '. ($this->newLimit > $this->TotalNumRows ? ($this->TotalNumRows) : $this->newLimit).' ] of '. $this->TotalNumRows;
}
function getBoundaries() {
$this->BoundaryMin = $this->current - 3;
$this->BoundaryMax = $this->current + 3;
if ($this->BoundaryMin < 1) {
$this->BoundaryMin = 0;
$this->BoundaryMax = $this->maxPage;
}
if ($this->BoundaryMax > $this->TotalNumPages) {
$difference = ($this->BoundaryMax - $this->TotalNumPages);
$this->BoundaryMax = $this->TotalNumPages;
$this->BoundaryMin = ($this->BoundaryMin - $difference);
}
}
function buildNumbers($string = '') {
for ($x = $this->BoundaryMin; $x <= $this->BoundaryMax; $x++) {
if ($x < $this->TotalNumPages) {
if ($this->current == $x) {
$string .= '<span style="font-weight: bold">'.($this->begin ? $x + 1 : $x).'</span> | ';
}
else {
$string .= '<a href=?gallery='.$this->gallery.'&start='.($this->begin ? $x + 1 : $x).'>'.($this->begin ? $x + 1 : $x).'</a> | ';
}
}
}
return $string;
}
function preFormat() {
$this->output = ($this->current == 0 ? 'Prev | ' : '<a href="?gallery='.$this->gallery.'&start='.($this->current - 1).'">Prev</a> | ');
$this->output .= ($this->current == ($this->TotalNumPages) ? 'Next | ' : '<a href="?gallery='.$this->gallery.'&start='.($this->current + 1).'">Next</a> | ');
$this->output .= $this->numbers;
$this->output .= '<a href="?gallery='.$this->gallery.'&start=0">First</a> | ';
$this->output .= '<a href="?gallery='.$this->gallery.'&start='.($this->TotalNumPages - 1).'">Last</a>';
}
function output() {
return array($this->query, $this->output, $this->viewing());
}
}
i added in gallery because i need that for my script but i really just changed some numbers around. the middle page number things are real screwed up right now since i did not have time to finish last night and no time now for i got class. the rest works perfect
ok i have tested this, not extensivly but with different settings. it works nice for me
EDIT: i found some bugs when you try to view page 5 and up. ill fix that then edit this post again
EDIT 2: ehhe, silly me. i did somthing stupid and won't tell you what it was! its fixed now. tested up to 6 pages and working fine
The navigation isn't correct for pages where there is on 1 page of results or 2 pages of results.
Dang, one of these days I need to test this class, I didn't anticipate so many problems off the shelves. Sometime this weekend I'll run some rigorous tests...