PHP Pagination Last id record

$0
=
$0
+
$0
No specific Bitcoin Bounty has been announced by author. Still, anyone could send Bitcoin Tips to those who provide a good answer.
0

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

Tags: , ,
Good question :)

0 answers - total earned - 0 BTC ($0 USD)

0
=
0
=
$0
Everyone, even non-registered users could send Bitcoin Tips to YOU if they are inspired by your answer!

Post Answer


Will Bitcoin Tipping become a new Internet Culture?


Please share our story! THANK YOU!