Easy way to Create Go to Next Page and Previous Page in Php

In this article i am gone to share How you can create, Easy way to Create Go to Next Page and Previous Page in Php with you..

Create Go to Next Page and Previous Page


First of all you need to know how the Php logic works with MySQL database

when we click on any Button on page like Next Post and Previous these Sql quires will executed by Php interpreter , the code simply says select all the data using id and filter using increasing or decreasing and  put limit only only.. as you see in codes..

 

Go on Previous Post this SQL Query used

$id=5;
select * from artical where id < '$id' order by id DESC LIMIT 1

 

For go on Next Post this SQL Query used

$id=5;
select * from artical where id > '$id' order by id ASC LIMIT 1

Create Go to Next Page and Previous Page

Implementation in code…

Full Code: For go to Previous Post

This is the full code which is used in a backend for , when a user click on Previous Button or link this code will execute..

$sql="select * from artical where id < '$id' order by id DESC LIMIT 1";
$result= mysqli_query($link, $sql);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($resultpr)){
$pr_id = $row['id'];
$pr_title = $row['title'];
echo ' <a href="post.php?artical_id='.$pr_id.'" > $pr_title</a> ';
}
}else{
echo '<span>No More Post</span>';
}

Create Go to Next Page and Previous Page

Full Code: For go to Next Post

This is the full code which is used in a backend for , when a user click on Next Button or link this code will execute..

$sqln="select * from artical where id >'$id' order by id ASC LIMIT 1";
$resultn= mysqli_query($link, $sqln);
if(mysqli_num_rows($resultn)>0){
while($row = mysqli_fetch_assoc($resultn)){
$nx_id = $row['id'];
$nx_title = $row['title'];

echo ' <a href="post.php?artical_id='.$nx_id.'" > $nx_title</a> ';

}
}else{
echo '<span>No More Post</span>';
}

I hope this code Works in Your Future Projects..

 

Leave a Comment