Udemy logo

php pagination tutorialPHP, along with HTML, CSS and JavaScript, has helped make the internet what it is today: interactive and fast. PHP helps add a lot of functionality to a website and improves everyone’s browsing experience. So what is PHP exactly? PHP originally stood for Personal Home Pages, but now it’s an acronym for PHP: Hypertext Preprocessor. If you’re familiar with JavaScript, you know that JavaScript code is embedded inside HTML code and it is used to make a page interactive. What JavaScript actually does is allow your browser to make changes to an open page without having to reload it. PHP is similar to JavaScript in that it is embedded in HTML code, and that it is used to perform a task (like collecting user data, interacting with MySQL servers and performing calculations). The only difference between the two is that PHP is executed on the server side instead of the client side. It outputs HTML as a result, which is the only part of the process that is actually visible to you. If you’d like to learn more about PHP, take this introductory PHP course. The course material includes all the basics- it’s easy to pick up even for a newbie.

In this basic PHP pagination tutorial, we’re going to discuss how you can add the pagination functionality to your webpage. You need to be familiar with HTML, MySQL, CSS and PHP to understand what we’re talking about here.  If you’re new to web development, you may want to first take this course to better understand how these languages work together.

What is Pagination?

Pagination refers to the practice of dividing content into separate pages. If you type a search query in Google, many times you receive millions of results. To avoid the information overload, the search results are divided into pages, with the most relevant results on the first page. This is exactly what pagination is. Another example of pagination is when you shop at an online retailer, like Amazon. When you search for a bag to buy, the results show up on discrete pages. Pagination is used to prevent information overload and help make browsing through a large amount of data easier.

Going About Implementing Pagination

If you want to implement pagination on your website, you will need several things. First, you will need a MySQL database which stores all the data that needs to be paginated. Then, you need to connect it to your webpage. This is the MySQL/PHP part of the code. Then, you will need to write code that accepts a query from a browser and sends it to the server. The server will then have to process the query, find the data that best matches the query and return the result to your browser. This is the PHP part of the code. Finally, you need to write HTML/CSS code to actually display your webpage as well as the results. You will also need to create a paging display, which the user can navigate. Sounds quite complicated right? If your website has a large database, it can be somewhat difficult to manage. There’s a reason why website developers are in demand, after all. If you’re new to MySQL or HTML, we recommend you sign up for this courses to learn more about how they interact with PHP. You can also take a look at some of the tutorials we’ve written on the topics before.

Now that we know what to do, we actually have to write the code for it. There are different ways to go about actually implementing pagination. We could write custom classes that connect to the database, find its limit (size) and retrieve the data as necessary. This can be pretty complicated and difficult for PHP newbies to manage, as you need to know more about singleton classes, be familiar with SQL queries as well as CSS.

Another way is to use the LIMIT SQL statement to limit the number of records to retrieve. We could, for example, only display 20 results per page, without accepting a query from a browser. This will prevent the need to actually have to write custom classes that interact with the database. Of course, this is not the best way to implement pagination.

We’ll show you a few code snippets on how to go about implementing pagination with the LIMIT statement. Please note that this is not a working example, as you will have to customize a large part of the code to actually match your database as well as what kind of pagination scheme you actually want to build. To learn how to build a custom pagination scheme, we recommend you sign up for our PHP course.

First, you will have to connect to your database:

$connection = mysql_connect($hostaddress, $useraccess, $password);
if(! $connection )
die('Cannot connect to your database: ' . mysql_error());

Then, you need to select the database:


And now we come to the main part of the code. First, you will need to actually count the number of records in your database:

$sqlstate = "SELECT count(ID) FROM table ";
$retrieve = mysql_query( $sqlstate, $connection );
if(! $retrieve )
die('Data could not be retrieved: ' . mysql_error());

The value that was retrieved will have to bundled in an array:

$rowno = mysql_fetch_array($retrieve, MYSQL_NUM );
$rec_count = $rowno[0];

Then we will have to write a SELECT statement that actually fetches the data and displays it on the page. The LIMIT statement is used in conjunction with the SELECT statement to limit the number of results to retrieve. This is the part of the code that has to be customized for your table. With PHP, you are supposed to fetch the total number of pages (depending on your database), the current page you are on and respond to navigation clicks by the browser. Then you have to actually embed the PHP code into an HTML page.

To learn how to write your own programs in PHP, you can take this course.

Page Last Updated: June 2014

Top courses in PHP (programming language)

PHP From Scratch 2024 | Beginner To Advanced
Brad Traversy
4.6 (887)
APIs in PHP: from Basic to Advanced
Dave Hollingworth
4.7 (1,054)
PHP for Beginners - Become a PHP Master - CMS Project
Edwin Diaz, Coding Faculty Solutions
4.4 (24,743)
PHP with Laravel for beginners - Become a Master in Laravel
Edwin Diaz, Coding Faculty Solutions
4.4 (12,779)
Object Oriented PHP & MVC
Brad Traversy
4.5 (4,974)
PHP for Beginners
Tim Buchalka's Learn Programming Academy, Dave Hollingworth
4.5 (3,850)
PHP Unit Testing with PHPUnit
Dave Hollingworth
4.6 (2,341)

More PHP (programming language) Courses

PHP (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo