<?
$db = mysql_connect("localhost", "", "");
mysql_select_db("bestof");
//Set this to the number per page
$limit = 10;
//This sets the offset to 0 if it isn't set
if (!isset($offset)) $offset = 0;
//Do a COUNT query to get the total records that we will be working with
$countsql = "SELECT COUNT(*) as totalnum from listings WHERE something = '$something'";
$countresult = mysql_query($countsql);
$countrow = mysql_fetch_array($countresult);
$totalnum = $countrow["totalnum"];
//Do the real query
$realsql = "SELECT * from listings WHERE something = '$something' ORDER BY date DESC LIMIT $offset, $limit";
$result = mysql_query($realsql);
while($row = mysql_fetch_array($result)) {
print $row["Name"]."<br>";
}
//Create the new offset by adding the current offset to the limit
$newoffset = $offset + $limit;
//Check to see if the total number of records is greater than the limit if it is create the next link by creating a new offset which is the current plus the limit and make the link remembering to send the criteria data off to each page in this case $something
if ($totalnum > $newoffset) {
printf('<a href="%s?offset=%s&something=%s">next >></a><br>', $PHP_SELF, $newoffset, $something);
}
Smae as above except for subtracting the offset to create the back button if the current offset is greater than the limit so it won't be on the first page
if ($offset >= $limit) {
$newoffset = $offset - $limit;
printf('<a href="%s?offset=%s&something=%s">prev <<</a>', $PHP_SELF, $newoffset, $something);
}
?>
Bookmarks