In this article, we’re going to explain how easy it is to paginate your data set using PHP and AJAX via jQuery. We’re also going to use the Silex framework for simplicity.
The data source
First off, we need some data to paginate!
id | name | age |
---|---|---|
1 | Jamie | 43 |
2 | Joe | 24 |
3 | Fred | 23 |
4 | Clive | 92 |
5 | Roy | 73 |
6 | Geoff | 24 |
7 | Ray | 12 |
8 | John | 9 |
9 | Pete | 32 |
10 | Ralph | 34 |
For the purpose of this example we’re going to use MySQL, but of course we can swap this out easily for SQLite or any other RDBMS. We’re going to name our database example
and the table will be named people
.
The backend
As we’re going to build our application on Silex, we need to install Silex using Composer first. Install it through composer with the command composer require silex/silex
.
Next we need set up our index.php
file, connect to the data source and select the database. We’re going to use PDO for this as it is easy to change to other database software later, and it also handles escaping user input (thus preventing SQL injection attacks). If you’re still stuck on the mysqli
or even worse the mysql
extension, see this tutorial. We’re going to put the connection in the $app
container so it’s easy to use later in our routes.
Continue reading %Pagination with jQuery, AJAX and PHP%
by Jamie Shields via SitePoint
No comments:
Post a Comment