| 26 | Crelea | Seven | 79 | male |
| 27 | Big Nose | Three | 81 | female |
| 28 | Rojj Base | Seven | 86 | female |
| 29 | Tess Played | Seven | 55 | male |
| 30 | Reppy Red | Six | 79 | female |
| Previous | 1 2 3 4 5 6 7 | Next |
$offset=$_GET['offset'];// Collect data from Query string
if(strlen($offset) ==0){$offset=0;}
Using this $offset value we will set the fresh value for the $offset for previous page and next page by adding and subtracting $limit to this $offset value.
$limit = 5; // No of records to be shown per page.
$next = $offset + $limit; // offset value for Next page
$back = $offset - $limit; // offset value for Previous page
We also assigned page name to a variable so we can mangage the same from a single point while generating links for navigation.
$page_name="display-paging.php"; // Pange name for linking
Here we are using Prepared statements to execute the query and getting the rows from the table. This paramterized query is used to prevent the injection attack.
$sql="SELECT * FROM student LIMIT :offset, :limit"; // query with place holders
$stmt = $my_conn->prepare($sql);
$stmt->bindParam(':offset', $offset, SQLITE3_INTEGER);
$stmt->bindParam(':limit', $limit, SQLITE3_INTEGER);
$result=$stmt->execute();
Using the above code we will get Result object, then we will use fetchArray() to get an array of rows of data.
echo "<table>";
while ($row=$result->fetchArray()) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td>
<td>$row[class]</td><td>$row[mark]</td><td>$row[gender]</td></tr>";
}echo "</table>";
$nume=$my_conn->querySingle('SELECT count(*) FROM student');
There are three parts in the navigation.
$back = $offset - $limit; // Starting number of record for Previous page
We will check this value of $back and show the link to navigate to previous page.
if($back >=0) {
print "<a href='$page_name?offset=$back'>Previous</a>";}
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $offset){
echo " <a href='$page_name?offset=$i'>$l</a> ";
}
else { echo "<font size='4' color=red>$l</font>";} /// Current page is not displayed as link and given font color is red
$l=$l+1;
}
if($next < $nume) {
print "<a href='$page_name?offset=$next'>Next</a>";}
The Full code is here
<?php
require 'menu.php';
$my_conn = new SQLite3('my_db.db');// Connect to Database
$offset=$_GET['offset'];// Collect data from Query string
if(strlen($offset) ==0){$offset=0;} // If not available in the Query string
/*******
$offset is the starting row number of the page. For first time it is taken as 0 or from first record .
It must be an integer.
You can add this additional check to ensure that nothing other than an integer is used.
$offset=$_GET['offset'];// Collect data from Query string
if(strlen($offset) > 0 and !is_numeric($offset)){
echo "Data Error";
exit;
}
********/
$limit = 5; // No of records to be shown per page.
$next = $offset + $limit; // Starting number of record for Next page
$back = $offset - $limit; // Starting number of record for Previous page
$page_name="display-paging.php"; // Pange name for linking
$sql="SELECT * FROM student LIMIT :offset, :limit"; // query with place holders
$stmt = $my_conn->prepare($sql);
$stmt->bindParam(':offset', $offset, SQLITE3_INTEGER);
$stmt->bindParam(':limit', $limit, SQLITE3_INTEGER);
$result=$stmt->execute();
echo "<br><center><table>";
while ($row=$result->fetchArray()) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td>
<td>$row[class]</td><td>$row[mark]</td><td>$row[gender]</td></tr>";
}echo "</table></center><br><br>";
/////// Bottom navigation Links /////
$nume=$my_conn->querySingle('SELECT count(*) FROM student'); // Total number of records
echo "<table align = 'center' width='50%'><tr><td
align='left' width='30%'>";
if($back >=0) {
print "<a href='$page_name?offset=$back'>Previous</a>";}
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $offset){
echo " <a href='$page_name?offset=$i'>$l</a> ";
}
else { echo "<font size='4' color=red>$l</font>";} /// Current page is not displayed as link and given font color red
$l=$l+1;
}
echo "</td><td align='right' width='30%'>";
if($next < $nume) {
print "<a href='$page_name?offset=$next'>Next</a>";}
echo "</td></tr></table>";
?>
$sql="SELECT * FROM student ORDER BY mark DESC LIMIT :offset, :limit ";
In ascending order using ASC ( default )
$sql="SELECT * FROM student ORDER BY mark ASC LIMIT :offset, :limit ";
OR
$sql="SELECT * FROM student ORDER BY mark LIMIT :offset, :limit ";
$sql="SELECT * FROM student WHERE class='Three' ORDER BY mark ASC LIMIT :offset, :limit ";
If we are using WHERE condition then we must use the same WHERE condition while finding out total number of rows in Dataset. Otherwise our counting of sub-pages will go wrong.
$nume=$my_conn->querySingle("SELECT count(*) FROM student WHERE class='Three'");
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.