I've the following Pagination code :
$rec_limit = 3;
/* Get total number of records */
$query = "SELECT count(id) FROM news";
$result = mysql_query($query);
if(!$result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$query2 = "SELECT * ".
"FROM news ".
"LIMIT $offset, $rec_limit";
$result2 = mysql_query( $query2 );
if(! $result2 )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo " ID :{$row['id']} <br> ".
" News : {$row['title']} <br> ".
" Date : {$row['date']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"{$_SERVER['PHP_SELF']}?page=$last\">Last 3 Records</a> |";
echo "<a href=\"{$_SERVER['PHP_SELF']}?page=$page\">Next 3 Records</a>";
}
else if( $page == 0 )
{
echo "<a href=\"{$_SERVER['PHP_SELF']}?page=$page\">Next 3 Records</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"{$_SERVER['PHP_SELF']}?page=$last\">Last 3 Records</a>";
}
The code works perfectly as needed, except that the a href
part where it shows the Next 3 records link even when there is no news left. By that i mean, it keeps opening up pages with empty records ! How can i remove Next 3 Records link when it reaches the last news ID.
Thank You
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!