how do you solve this age old problem of rounding off a number
$pages = $returned_rows/$items_per_page
when i use php functions to round the result off i end up with 1 for example if i have 7 results and 5 items on every page
this happens from time to time, whenever uneven numbers come up, so how do you guys solve this? i was going to look at other products such as vbulletin but i figured this could be a good topic as a break from all the meme spam
Name:
Anonymous2008-05-18 14:51
lol i think i solved it with ceil() but i'm sure more situations will arise where odd numbers cause problems with paging
Here's a shitty class I wrote to handle pagination:
<?php
/*
| Currently, the paginator assumes we're using
| $_GET['perpage']
| $_COOKIE['perPage']
| An option to change this may be added in future versions.
|
| Also, when showing 'All' rows, we limit to 2 ^ 20 rows (roughly 1m)
| We could simply omit the 'limit' statement in SQL, but this is
| slightly safer, as if there are >1m rows, the page will actually stop
| loading at some point.
*/
class paginator {
// object - cookie management
var $cookie;
// array - list of possible values per page
var $perPageArray;
// int - max results per page
var $maxPerPage;
// int - page start (row)
var $pageStart;
// int - current page
var $currentPage;
// automatically updates local vars
function config(&$cookie,$perPageArray,$maxPerPage,$pageNum) {
$this->cookie = $cookie;
$this->perPageArray = $perPageArray;
$this->maxPerPage = $this->setPerPage($maxPerPage);
// we have to account for the fact that pages are displayed starting at 1
// but in the code, start at 0
$this->currentPage = ($pageNum > 0) ? $pageNum - 1 : 0;
$this->pageStart = $this->currentPage * $this->maxPerPage;
}
// sets max results per page
// basically, this gets its own function to keep config() from getting too messy
function setPerPage($maxPerPage) {
// this is a hard default, since $maxPerPage should come from a config variable
$maxPerPage = ($maxPerPage > 0) ? $maxPerPage : 20;
// we assume $_GET uses a non-camel-case perPage, due to URL consistency
// perPage has a new value
if($_GET['perpage'] != '') {
if($_GET['perpage'] == 'All') {
$perPage = pow(2,20); // 2 ^ 20
$this->cookie->create('perPage',$_GET['perpage']);
} elseif($_GET['perpage'] > 0) {
$perPage = $_GET['perpage'];
$this->cookie->create('perPage',$_GET['perpage']);
}
}
// checking for perPage as set by a cookie
// we can keep this separate from the above, since
// the cookie class automatically assigns new variables to $_COOKIE
if($_COOKIE['perPage'] != '') {
if($_COOKIE['perPage'] == 'All') {
$perPage = pow(2,20); // 2 ^ 20
} elseif($_COOKIE['perPage'] > 0) {
$perPage = $_COOKIE['perPage'];
}
// default perPage value
} else {
// we set a cookie incase the default somehow changes between pages
$perPage = $maxPerPage;
$this->cookie->create('perPage',$maxPerPage);
}
return $perPage;
}
// int - array of pages
function getPageArray($totalRows) {
$numberOfPages = ($this->maxPerPage > 0) ? ceil($totalRows / $this->maxPerPage) : 0;
if($numberOfPages < 1) $numberOfPages = 1;
return range(1,$numberOfPages);
}
// int - page start (row)
function getPageStart() { return $this->pageStart; }
// int or string - rows per page ('All' is represented as a string)
function getPerPage($returnAsString = false) {
if($returnAsString == false) {
return $this->maxPerPage;
} else {
return ($_GET['perpage'] == 'All' || $_COOKIE['perPage'] == 'All') ? 'All' : $this->maxPerPage;
}
}
// array - list of possible values per page
function getPerPageArray() { return $this->perPageArray; }
// int - current page (incremented by 1 to account for the display offset)
function getCurrentPage() { return $this->currentPage + 1; }
}
?>
Name:
Anonymous2008-05-19 4:57
>>11
Before anyone comes to HAX MY ANUS, I don't use this anymore.
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy