选择连续的下一个可用数字

If i have a database like this one

[ id  -    post  -  num ]
[  1        P1       1  ]
[  2        P2       2  ]
[  3        P3       3  ]
[  4        P4       5  ]
[  5        P5       7  ]

Now i have a code like this

$max_number = 7; // AFTER QUERY
$current_number = $_GET['num']; // = 1

if($current_number < $max_number){
echo "<a href='#'>The {$current_number++} in rating Post</a>";
}

the problem in this code that if the topic number is 3 and clicked it would give me number 4 which doesn't exist, Is there a possible way to do it better? using array if possible.

A solution could be (in sql, you will have to implement it):

SELECT MIN(num) FROM tab WHERE num>$current_number;

This would give you your previous ($current_number++) value.

Store all available number in an array something like this

$available_numbers= arrray(1,2,3,5,6,7);

Change some code according to your need.

<?php
$available_numbers= array(1,2,3,5,6,7);
$max_number = 7; // AFTER QUERY
$current_number = 1; // = 1
do{
    if(in_array($current_number, $available_numbers)){
        echo "<a href='#'>The {$current_number} in rating Post</a>";
    }
    $current_number++;
}while($current_number < $max_number);
?>