Pagination problem
Date: 03/19/05
(PHP Community) Keywords: mysql, sql
Anyone know of any good pagaination functions - I've tried to code my own and I seem to be going around in cicles. What I've writen is:-
function disp_page($offset, $max_rows, $table, $base_url)
{
mysql_select_db(DB);
if(!isset($offset))
{
$offset=0;
}
$offset=($offset+$max_rows);
// 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"$ltbr>";
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"$ltbr>$ltcenter>No Records to Display$lt/center>";
}
}
function disp_row($row)
{
// This function is to be edited to display the data how needed
echo"$lta href=\"?disp_t=topic&topic_id={$row[0]}\">".$row[2]."$lt/a>$ltbr>";
}
function disp_prev($offset, $base_url, $max_rows)
{
$tmp_offset=($offset-$max_rows);
if($tmp_offset $lt 0)
{
$tmp_offset = 0;
}
$offset=$tmp_offset;
$url=$base_url."&offset=".$offset;
echo"$lta href=".$url.">PREV$lt/a> ";
}
function disp_next($offset, $base_url, $total_rows, $max_rows)
{
$tmp_offset=($offset + $max_rows);
// echo"$ltp>tmp_offset=$tmp_offset
offset=$offset
";
if($tmp_offset > $total_rows)
{
// Nothing Happening Here
}
else
{
$offset=$tmp_offset;
$url=$base_url."&offset=".$offset;
echo"$lta href=".$url.">NEXT$lt/a>$ltp>";
}
}
function disp_page_nums($offset, $base_url, $total_pages, $max_rows)
{
$page=(($max_rows/$offset)+1);
for($i=1;$i<$total_pages;$i+1)
{
if($i==$page)
{
echo "$ltfont size=\"-1\">[".$i."] $lt/font>";
}
else
{
$tmp_offset=($i * $max_rows);
$offset=$tmp_offset;
$url=$base_url."&offset=".$offset;
echo"<a href=".$url.">PREV</a>";
}
}
}
my main issue seems to be that the function disp_page_nums() seems to hang my server up.
Any help or sugestions would be very welcome
Paul
Source: http://www.livejournal.com/community/php/275374.html