px | top | add code | search | signup | login | help |
<?php
/*--------------------------------------------------------------------
This is a set of functions to paginate results from a database.
Coded by Paul Mann 2005. (nite2quake@yahoo.co.uk)
Usage:
disp_page($max_rows, $table, $base_url);
$max_rows - Number of results per page
$table - The table used with any where clause eg:
"posts where user_id = '$user_id'"
$base_url - The url being called with any variables
necessary eg:
"index.php?foo=bar&bar=$foo"
The function disp_row($row) is to be edited by the user to format
the results as needed
If the results of disp_row($row) are to be returned in a tabel then but
the function "disp_page($max_rows, $table, $base_url);" between the <table>
and </table> tags.
*/----------------------------------------------------------------------
// Just in case not done - comment out if not needed
if(!mysql_connect("localhost", "root", ""))
{
die('failed to connect');
}
define(DB, "lbb");
mysql_select_db(DB);
// Set of functions for pagination
function disp_page($max_rows, $table, $base_url)
{
$offset =$_GET['offset'];
mysql_select_db(DB);
if(!isset($offset))
{
$offset=0;
}
// Define the SQL calls
$sql="select * from $table limit $offset, $max_rows";
$sql_tmp="select * from $table";
// Make the SQL calls
$result=mysql_db_query(DB, $sql);
$tmp=mysql_db_query(DB, $sql_tmp);
$total_rows=@mysql_num_rows($tmp);
// Define other variables
$total_pages=(ceil($total_rows/$max_rows));
paginate($offset, $total_rows, $base_url, $max_rows, $total_pages);
// Display the results
while($row=@mysql_fetch_row($result))
{
disp_row($row);
}
echo"<br>";
// paginate($offset, $total_rows, $base_url, $max_rows, $total_pages);
}
function paginate($offset, $total_rows, $base_url, $max_rows, $total_pages)
{
// PREV link
if($offset > 0)
{
disp_prev($limit, $base_url, $max_rows);
}
// Page Numbers
if($total_pages > 1)
{
disp_page_nums($offset, $base_url, $total_pages, $max_rows);
}
// Next link
if(($offset + $max_rows) < $total_rows)
{
disp_next($offset, $base_url, $total_rows, $max_rows);
}
// Empty Results set
if($total_rows==0)
{
echo"<br><center>No Records to Display</center><p>";
}
}
function disp_prev($offset, $base_url, $max_rows)
{
$offset=$_GET['offset'];
$tmp_offset=($offset-$max_rows);
if($tmp_offset < 0)
{
$tmp_offset = 0;
}
$offset=$tmp_offset;
$url=$base_url."offset=".$offset;
echo"<font size='-1'><a href=".$url.">PREV</a> ";
}
function disp_next($offset, $base_url, $total_rows, $max_rows)
{
$tmp_offset=($offset + $max_rows);
// echo"<p>tmp_offset=$tmp_offset<br>offset=$offset<p>";
if($tmp_offset > $total_rows)
{
// Nothing Happening Here
}
else
{
$offset=$tmp_offset;
$url=$base_url."offset=".$offset;
echo"<font size='-1'><a href=".$url.">NEXT</a><br>";
}
}
function disp_page_nums($offset, $base_url, $total_pages, $max_rows)
{
$page=(($offset/$max_rows)+1);
for($i=1;$i<=$total_pages;$i++)
{
if($i==$page)
{
echo "<font size=\"-1\">[".$i."] </font>";
}
else
{
$tmp_offset=(($i * $max_rows)-$max_rows);
$offset=$tmp_offset;
$url=$base_url."offset=".$tmp_offset;
echo"<font size=\"-1\"><a href=".$url.">[$i]</a> ";
}
}
}
function disp_row($row)
{
// Edit this function to format the results as required
}
?>
Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.