My code came from oracle which doesn't have LIMIT, but I tried to modify it for mysql. Mine's a little simpler (basically the same minus the count and maintains the query string), and sounds like it'd suit your needs. Does "pagination" without the page numbers, just previous and next. When there are still more it shows next, when there are ones before it shows previous. It's also probably a little faster than the above because you don't have the count query (which you'd need to add page numbers, but since you don't there's no need for it).
Basically it adds the querystring prior to &haspages (which is just a place holder) to all of the next and previous links and tacs on limit and offset (offset and pagesize)...
function:
Code: Select all
<?php
function pages($offset, $pagesize, $hasnext) {
global $_SERVER;
global $PHP_SELF;
list($querystring,) = explode("&haspages", $_SERVER["QUERY_STRING"]);
if ($offset > 1) {
echo "<a href=\"" . $PHP_SELF . "?" . $querystring . "&haspages=true&offset=" . ($offset - $pagesize) . "&pagesize=" . $pagesize . "\">< Previous</a>";
}
if ($hasnext) {
echo "<a href=\"" . $PHP_SELF . "?" . $querystring . "&haspages=true&offset=" . ($offset + $pagesize) . "&pagesize=" . $pagesize . "\">Next ></a>";
}
}
?>
use (ADOdb - mySQL (untested)):
Code: Select all
<?php
$offset = (ctype_digit($_GET["offset"])) ? $_GET["offset"] : 1;
$pagesize = (ctype_digit($_GET["pagesize"])) ? $_GET["pagesize"] : 25;
$sql = "select * from table LIMIT ?,?";
unset($binds);
$binds["offset"] = $offset;
$binds["limit"] = $pagesize;
$rs = $db->execute($sql, $binds);
while ($arr = $rs->FetchRow()){
echo "<a href=\"index.php?todo=something&id=" . $arr["id"] . "\">" . $arr["name"] . "</a><br>";
}
if ($rs->RecordCount() - 1 >= $pagesize) {
pages($offset, $pagesize, true);
} else if ($offset > 1) {
pages($offset, $pagesize, false);
}
?>
use (ADOdb - Oracle):
Code: Select all
<?php
$offset = (ctype_digit($_GET["offset"])) ? $_GET["offset"] : 1;
$pagesize = (ctype_digit($_GET["pagesize"])) ? $_GET["pagesize"] : 25;
$sql = "SELECT * FROM (SELECT ROWNUM as LIMIT, T.* FROM (select * from table) T) WHERE LIMIT BETWEEN :THEMIN AND :THEMAX";
unset($binds);
$binds["THEMIN"] = $offset;
$binds["THEMAX"] = ($offset + $pagesize);
$rs = $db->execute($sql, $binds);
while ($arr = $rs->FetchRow()){
echo "<a href=\"index.php?todo=something&id=" . $arr["ID"] . "\">" . $arr["NAME"] . "</a><br>";
}
if ($rs->RecordCount() - 1 >= $pagesize) {
pages($offset, $pagesize, true);
} else if ($offset > 1) {
pages($offset, $pagesize, false);
}
?>