Post by [dmsuperman] on Apr 14, 2006 0:41:27 GMT
This a pagination function for PHP:
/*
Function.....: paginate
Description..: Takes a SQL statement and generates output for pagination. <limit> is the number of rows to display
per page, <url> is a url to pass to it for links (it will auto append the page number, be sure to strip that out),
<get> is the query string key it will use, defaulting to "p"
Syntax.......: paginate(<sql>[, <limit>[, <url>[, <get>]]]);
Example......: paginate("SELECT * FROM messages ORDER BY time DESC") --> array("links" => "Previous 1 2 [3] 4 5 Next",
"rows" => array_of_rows_for_page);
Input........: Formats: string
Output.......: Format: array(string, array)
Author.......: [dmsuperman]
Contributors.:
Date(s)......: 4/13/06
Version......: 1.0
*/
function paginate($sql, $limit=10, $url=false, $get="p"){
//If a URL hasn't been given, just make one with filename?querystring
$url = !$url ? $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"] : $url;
//See how many total rows there are
$res_all = mysql_query($sql);
$numrows = mysql_num_rows($res_all);
$numpages = ceil($numrows / $limit);
//Get the current page if it hasn't been defined
$page = (is_numeric($_GET[$get]) && $_GET[$get]!="") ? $_GET[$get] : 1;
//Add the LIMIT for the current page only
$sql .= "LIMIT " . $page . ", " . $limit . ";";
$res = mysql_query($sql);
//Take the current page's rows and store it in $rows for return
$rows = array();
while(($row=mysql_fetch_assoc($res))!==false){
$rows[] = $row;
}
$links = "";
//Remove ? and & at the end of the query string
$url = strpos(strrev($url), "&")===0 ? $url : (strpos(strrev($url), "?")===0 ? $url : $url."?");
//We're not on the first page, display a previous link
if($page > 1){
$links .= "<a href='" . $url . $get . "=" . ($page-1) . "'>Previous</a> ";
}
//Now display all the pages, unlinking and putting brackets around the current page
for($x = 1; $x <= $pages; $x++){
if($x != $page){
$links .= "<a href='" . $url . $get . "=" . $x . "'>" . $x . "</a> ";
}else{
$links .= "[" . $x . "] ";
}
}
//We're not on the last page, display a next link
if($page < $pages){
$nextPage = $page + 1;
$links .= "<a href='" . $url . $get . "=" . $nextPage . "'>Next</a>";
}
return(array("links" => $links, "rows" => $rows));
}
You pass it a SQL query that gets all the rows you'll want to display, it gets them and generates links for pagination, and returns only the rows for that page. You can optionally pass a limit, which is how many rows to show per page, defaulting to 10, a url, which it uses to build the links for pagination (it builds its own of the current page if you don't pass one, but it doesn't strip out the page number, which I was too lazy to implement), and a query string key, defaulting to "p".
Use it how you want...I just know I hated having to rebuild my pagination for every time I needed it.
/*
Function.....: paginate
Description..: Takes a SQL statement and generates output for pagination. <limit> is the number of rows to display
per page, <url> is a url to pass to it for links (it will auto append the page number, be sure to strip that out),
<get> is the query string key it will use, defaulting to "p"
Syntax.......: paginate(<sql>[, <limit>[, <url>[, <get>]]]);
Example......: paginate("SELECT * FROM messages ORDER BY time DESC") --> array("links" => "Previous 1 2 [3] 4 5 Next",
"rows" => array_of_rows_for_page);
Input........: Formats: string
Output.......: Format: array(string, array)
Author.......: [dmsuperman]
Contributors.:
Date(s)......: 4/13/06
Version......: 1.0
*/
function paginate($sql, $limit=10, $url=false, $get="p"){
//If a URL hasn't been given, just make one with filename?querystring
$url = !$url ? $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"] : $url;
//See how many total rows there are
$res_all = mysql_query($sql);
$numrows = mysql_num_rows($res_all);
$numpages = ceil($numrows / $limit);
//Get the current page if it hasn't been defined
$page = (is_numeric($_GET[$get]) && $_GET[$get]!="") ? $_GET[$get] : 1;
//Add the LIMIT for the current page only
$sql .= "LIMIT " . $page . ", " . $limit . ";";
$res = mysql_query($sql);
//Take the current page's rows and store it in $rows for return
$rows = array();
while(($row=mysql_fetch_assoc($res))!==false){
$rows[] = $row;
}
$links = "";
//Remove ? and & at the end of the query string
$url = strpos(strrev($url), "&")===0 ? $url : (strpos(strrev($url), "?")===0 ? $url : $url."?");
//We're not on the first page, display a previous link
if($page > 1){
$links .= "<a href='" . $url . $get . "=" . ($page-1) . "'>Previous</a> ";
}
//Now display all the pages, unlinking and putting brackets around the current page
for($x = 1; $x <= $pages; $x++){
if($x != $page){
$links .= "<a href='" . $url . $get . "=" . $x . "'>" . $x . "</a> ";
}else{
$links .= "[" . $x . "] ";
}
}
//We're not on the last page, display a next link
if($page < $pages){
$nextPage = $page + 1;
$links .= "<a href='" . $url . $get . "=" . $nextPage . "'>Next</a>";
}
return(array("links" => $links, "rows" => $rows));
}
You pass it a SQL query that gets all the rows you'll want to display, it gets them and generates links for pagination, and returns only the rows for that page. You can optionally pass a limit, which is how many rows to show per page, defaulting to 10, a url, which it uses to build the links for pagination (it builds its own of the current page if you don't pass one, but it doesn't strip out the page number, which I was too lazy to implement), and a query string key, defaulting to "p".
Use it how you want...I just know I hated having to rebuild my pagination for every time I needed it.