Hi,
I'm trying to populate the previous and next links with an id from mysql. The code below works but also displays ids that do not exist in the database.
I want the code to only show me the rows that exist not the ones that do not exist. For example, at this time I have 5 rows. The ids of the rows are 1, 2, 3, 4, 6 when I get to say, id 6 and click on next it displays id 7 instead of going back to 1 or greying out next - meaning there's no more to view.
Can someone help?
<?php
include('connection.php');
if(isset($_GET['id'])){
$start = $_GET['id'];
}else{
$start = 0;
}
$sql = mysql_num_rows(mysql_query("SELECT * FROM thetable"));
$result = mysqli_query($con,$sql);
$rows = mysql_fetch_array($result);
echo $rows['thetable'];
if($start == 0){
echo "Previous «";
}else{
echo "<a href=\"./thepage.php?id=" . ($start - 1) . "\">« Previous </a>";
}
if($start == $sql-1){
echo "Next »";
}else{
echo "<a href=\"./thepage.php?id=" . ($start + 1) . "\">Next »</a>";
}
?>
<?php
// End while loop.
mysqli_close($con);
?>