Mega Code Archive
MySQL Paging Class
»"
**/
function pageList($curpage, $pages)
{
$page_list = "";
/* Print the first and previous page links if necessary */
if (($curpage != 1) && ($curpage))
{
$page_list .= " « ";
}
if (($curpage-1) > 0)
{
$page_list .= "< ";
}
/* Print the numeric page list; make the current page unlinked and bold */
for ($i=1; $i<=$pages; $i++)
{
if ($i == $curpage)
{
$page_list .= "".$i."";
}
else
{
$page_list .= "".$i."";
}
$page_list .= " ";
}
/* Print the Next and Last page links if necessary */
if (($curpage+1) <= $pages)
{
$page_list .= "> ";
}
if (($curpage != $pages) && ($pages != 0))
{
$page_list .= "» ";
}
$page_list .= "\n";
return $page_list;
}
/*
* string nextPrev (int curpage, int pages)
* Returns "Previous | Next" string for individual pagination (it's a word!)
*/
function nextPrev($curpage, $pages)
{
$next_prev = "";
if (($curpage-1) <= 0)
{
$next_prev .= "Previous";
}
else
{
$next_prev .= "Previous";
}
$next_prev .= " | ";
if (($curpage+1) > $pages)
{
$next_prev .= "Next";
}
else
{
$next_prev .= "Next";
}
return $next_prev;
}
}
?>
Example on how to use:
findStart($limit);
/* Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query */
$count = mysql_num_rows(mysql_query("SELECT field FROM table"));
/* Find the number of pages based on $count and $limit */
$pages = $p->findPages($count, $limit);
/* Now we use the LIMIT clause to grab a range of rows */
$result = mysql_query("SELECT * FROM table LIMIT ".$start.", ".$limit);
/* Now get the page list and echo it */
$pagelist = $p->pageList($_GET['page'], $pages);
echo $pagelist;
/* Or you can use a simple "Previous | Next" listing if you don't want the numeric page listing */
//$next_prev = $p->nextPrev($_GET['page'], $pages);
//echo $next_prev;
/* From here you can do whatever you want with the data from the $result link. */
?>