PHP Pagination Tutorial – Pagination in PHP Demo example script

Are you looking for simple PHP Pagination for a very beginner level then i think this PHP pagination example is very much useful for you. Pagination is a technique to divide content into several pages. Here we can assign each page a separate URL. By Clicking that URL or Page Number, user can view this Page. For every page we assign a incremental Page number. You can free download simple PHP pagination script or PHP pagination code from here. Before that go through once with pagination in php demo link. Here I used php script with mysqli. Hope this php pagination example or php pagination tutorial using PHP MYSQL will help you to get the basic knowledge about the pagination as a beginner in php.
Take a quick look at this live demo to understand the flow of the simple pagination using php and mysql. Also you can download this simple pagination using php mysql script from here and can try in your localhost or live server where you want to do.

Creating the Database

CREATE TABLE user
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE
);

create dbconfig.php for connecting with the database

<?php
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$dbconfig = mysqli_connect($host, $username, $password, $database);
?>

Now we have to create the main page for creating simple pagination demo script using php and mysql. It contains the html and php.

Creating PHP pagination using php mysql demo script

index.php

<div id="content">
<?php
include("dbconfig.php");
$start=0;
$limit=10;

if(isset($_GET['id']))
{
	$id=$_GET['id'];
	$start=($id-1)*$limit;
}
else{
	$id=1;
}
//Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items. 
$query=mysqli_query($dbconfig,"select * from user LIMIT $start, $limit");
?>
<ul>
<?php
//print 10 items
while($result=mysqli_fetch_array($query))
{
	echo "<li>".$result['username']."</li>";
}
?>
</ul>
<?php
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($dbconfig,"select * from user"));
//calculate total page number for the given table in the database 
$total=ceil($rows/$limit);
if($id>1)
{
	//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
	echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>";
}
if($id!=$total)
{
	////Go to previous page to show next 10 items.
	echo "<a href='?id=".($id+1)."' class='button'>NEXT</a>";
}
?>
<ul class='page'>
<?php
//show all the page link with page number. When click on these numbers go to particular page. 
		for($i=1;$i<=$total;$i++)
		{
			if($i==$id) { echo "<li class='current'>".$i."</li>"; }
			
			else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
		}
?>
</ul>
</div>

Thanks for watching this tutorial. You can try yourself. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming. If you think this post is useful for beginner then please share it.

40 responses to “PHP Pagination Tutorial – Pagination in PHP Demo example script”

  1. Kenneth says:

    Great guide, worked like a charm 🙂

    Can you tell me how to get the next, previous,1,2,3,4 etc. buttons out in a row instead of a list?

  2. Guest says:

    Nice post,it is useful to me and others,please just keep it on….

  3. Anonymous says:

    Touche. Sound arguments. Keep up the great effort.

  4. Asics says:

    Great article and straight to the point.

  5. karen says:

    My husband and i have been very thrilled that Albert managed to deal with his homework from the precious recommendations he acquired from your own blog. It is now and again perplexing to just find yourself making a gift of procedures many others may have been trying to sell. And we all keep in mind we have you to appreciate for this. The entire explanations you made, the easy site navigation, the relationships your site assist to promote – it is everything fabulous, and it’s really facilitating our son in addition to our family reckon that this concept is amusing, which is seriously pressing. Many thanks for all!

  6. Anonymous says:

    Thanks for informative web site. The place where I am getting that type of info written in such an ideal way. I’ve a mission that I am simply now running on, and I’ve been on the look out for such info.

  7. Hiya says:

    Hiya, I’m really glad I have found this information. Today bloggers publish only about gossips and net and this is really annoying. A good site with interesting content, that’s what I need. Thank you for keeping this web-site, I’ll be visiting it. Do you do newsletters? Cant find it.

  8. Guest says:

    Itˇs really a nice and useful piece of information. I am satisfied that you simply shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

  9. Delvh says:

    Hello, I log on to your new stuff regularly. Your writing style is witty, keep up the good work!

  10. Asirifi E.K-Ghana says:

    Very useful and easy to understand code. I am very happy to have this as part of my codes in PHP.

  11. Anthony says:

    How do you set a max parameter so they can input a value higher than the max value such as. 20 Results, 5 per page, 4 pages via pagination equal
    url.php/id?=4 is max page. How to prevent them from changing that 4 to 5 and showing no results.

    • You can do with a simple if condition. Bcz when a particular query run with a limit and offset after max it will return zero rows. Or you can also keep their a check that the user given input is greater than the max page number or not.

  12. SOFT SUNRISE says:

    it seems good but if there should be any demo online or files to be downloaded then it will be best thanks for providing such a good script

  13. ummulshifa says:

    it seems good but if there should be any demo online or files to be downloaded then it will be best thanks for providing such a good script if anyone can forward me it as an email

  14. shinkayori says:

    How to add a number of data? Thanks for the nice article.

  15. Anthony says:

    Can you do an example of what I asked earlier? I’ve tried and it hasn’t worked or it just repeats the same query

  16. ummulshifa says:

    very nice demo it helped me to improve myself

  17. Torsten says:

    Great Job! Thanks!!
    how can I code on 10 limit ? Otherwise I have below 300 numbers (pages) available .

  18. Torsten says:

    I did not mean: item show per page.
    I meant: PREVIOUS 1 2 3 4 5 6……. NEXT
    where you can change the set does not show all pages . These are otherwise too many and looks not good. Greetings

  19. steven wilson says:

    I give you salute for this its so simple one.

  20. Ummulshifa says:

    Thanks for providing a good contents and support i have found the results needed that’s great keep it up.

  21. Swapnil says:

    Very nice article , nothing annoying, very useful for me, Thanks.

  22. آموزش PHP says:

    Thanks for sharing nice tutorials.

  23. SwamHtetMin says:

    Thanks a lot.

  24. Ihify says:

    Very nice and simple article, the pagination idea is so simple and easy, thanks for kind sharing, please keep sharing such things.

  25. Melvin says:

    From where you are grabbing the $id?

  26. Rahul Pamnani says:

    Thank You very much.

  27. Pedro says:

    can you explain the logic behind the line 10? ” $start=($id-1)*$limit;”

    I don’t understand why you subtract and then multiply. what are you doing there? Thanks!!

  28. Hasbi says:

    Thanks for the nice tutorial, it’s so easy to help me find the solution with your explanations.

  29. sandilyan says:

    Nice !!!

  30. Guest says:

    Wow i have been searching all day for a simple pagination script. But this one very simple well understood. i appreciate a lot

Leave a Reply

Your email address will not be published. Required fields are marked *