You will learn how to use VPS servers correctly via the commands without the need of any interface or control panel....
Virtual servers are widely used to host websites due to the great features they provide and the cheap costs....
By having a PHP database connection for your program, it will be easy to append and retrieve records and save the statuses of your program....
Learn how to use the properties and methods in the object oriented code and how to call the methods from inside and outside the mother object....
It is impossible to view all the records from the database at once on the webpage, they have to be partitioned by using a PHP MySQL paginating which provides an easy and simple way to show the content stored in multiple records in the MySQL database. In this article, we will show you the easiest and shortest way to paginate your data from A to Z :)
We will use the PHP database class which we already explained to establish a connection between the PHP and the MySQL database.
First, we need to create a folder for this small project and give it the name pagination. Our main page will be the index page with a PHP extension, i.e. index.php where we will create an instance of the database class to establish the connection with the MySQL. The index page will be placed inside the pagination folder and its initial content looks like:
You can use the MySQL console or the phpMyAdmin to access/manage your MySQL database. We need to create a database for the pagination inside the MySQL and give it the name pagination, here is the MySQL query:
Pagination database has been created successfully!
Let's now create the cities table which will contain different cities (each city will be in a single row). Cities table will include two columns sequence & name. It is created as below:
The MySQL query in the MySQL console looks like:
Now, we will insert into the table the cities (records) that will be paginated.
Here is the MySQL query in the console interface:
We will create a simple class for the pagination process, the class will be saved in a file that has a PHP extension, let's give it the name class.pagination.php & place it inside the project folder which is pagination. Inside the pagination class, we will create a method (function) called retrieve to get the records from the cities table. The function will take two simple arguments which are current_page & limit_per_page. Our class will have the property next_button to show the next button when we have still coming records in the database and to hide it when no more records to show up.
As per the code, we increase the limit by 1 when we apply the select query on the cities table to check if we have a next page after the current selection. In case the number of records we get by the select is greater than the page limit, then it means that the 'next navigation button' should be viewed on the page, otherwise, we will hide it. The retrieved cities are appended inside an array array_cities that we will pop from it one element in case there is more data to show up after the current selection because the number of records to be viewed should match the limit per page number.
In our PHP MySQL pagination example, the order of the records extracted is descending which means the most recent entries will be shown on the top of the page. You can change the order of the records extracted to ascending order by replacing the DESC with the ASC statement in the MySQL select query.
We are back to the index page index.php as we need to include the pagination class before we initiate its instance and to use the GET parameter to grab the current page from the URL in the browser address bar. Also, we will show the HTML buttons (next & previous) to nagivate between the records that are extracted from the cities table. Here is the complete code inside the index page:
As you notice in the code above, we stop the execution of our program by using the return statement when there are no results to be extracted from the database. For instance, if you forgot to add records inside the cities table or if you didn't create the table, the return statement will prevent from showing any exception/error on the homepage.
We didn't add any CSS styles because we need to point out the main matter which is the PHP MySQL pagination, you can add your own CSS styles that suits your interface and also extend the code if you want to add extra functions. The output of our code looks like:
Notice that we have no previous button on the first page and also no next button on the last page because there is nothing left in the cities table to show up.
We've provided a very simple and basic PHP MySQL pagination example that will help you to partition the data extracted from your database tables. No CSS styles have been added because we wanted to concentrate on the main pagination topic, you can add later your own CSS styles and also extra functions. The code has been tested and it is running 100%, if you are unable to run the example on your machine, then please contact us, we will be very happy to provide you with the source code (the project folder and its files) and any assistance you might require :)