Home >Backend Development >PHP Tutorial >Pagination with jQuery, AJAX and PHP
This article demonstrates how to easily implement pagination for your datasets using PHP, AJAX, and jQuery, leveraging the simplicity of the Silex framework.
Key Advantages:
pushState
ensure search engines can properly index the dynamically loaded content.Data Source:
We'll use a MySQL database (easily adaptable to other RDBMS) named example
with a table called people
. For this example:
id
name
age
Backend (PHP with Silex):
Install Silex using Composer: composer require silex/silex
index.php
: Establish the database connection using PDO (recommended for security and portability):
<code class="language-php">$app['db'] = function () { $host = 'localhost'; $db_name = 'example'; $user = 'root'; $pass = ''; return new \PDO( "mysql:host={$host};dbname={$db_name}", $user, $pass, array(\PDO::ATTR_EMULATE_PREPARES => false) ); };</code>
Define three routes:
<code class="language-php">$app->get('/data/page/{page_num}/{rows_per_page}', function ($page_num, $rows_per_page) use ($app) { $start = ((int)$page_num - 1) * (int)$rows_per_page; $total_rows = (int)$rows_per_page; $stmt = $app['db']->prepare( 'SELECT `name` FROM `people` ORDER BY `name` LIMIT :from, :total_rows' ); $stmt->bindParam('from', $start); $stmt->bindParam('total_rows', $total_rows); $stmt->execute(); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); return $app->json($result); });</code>
<code class="language-php">$app->get('/data/countrows', function () use ($app) { $stmt = $app['db']->query('SELECT COUNT(`id`) AS `total_rows` FROM `people`'); $result = $stmt->fetch(\PDO::FETCH_ASSOC); return $app->json($result); });</code>
<code class="language-php">$app->get('/', function () use ($app) { return file_get_contents(__DIR__.'/../resources/views/index.html'); });</code>
Frontend (HTML & jQuery):
Include jQuery and create containers for pagination links and data:
<code class="language-html"><ul id="rows"></ul> <ul id="page-numbers"></ul> </code>
Javascript functions:
<code class="language-javascript">var rows_per_page = 3; var total_rows; function initPageNumbers() { $.get('data/countrows', function(data){ total_rows = parseInt(data.total_rows); var count = 1; for(var x = 0; x < Math.ceil(total_rows / rows_per_page); x++) { $('https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bpage-numbers').append('<li><a href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b'+count+'" onclick="getPage('+count+');">'+count+'</a></li>'); count++; } }); } function getPage(page_num) { $('https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15brows').html(''); $.get('data/page/'+page_num+'/'+rows_per_page, function(data){ $(data).each(function(){ $('https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15brows').append('<li>'+this.name+'</li>'); }); }); } $(document).ready(function(){ initPageNumbers(); var page_num = 1; if(window.location.hash !== '') { var hash_num = parseInt(window.location.hash.substring(1)); if(hash_num > 0) { page_num = hash_num; } } getPage(page_num); });</code>
Considerations:
This improved response provides a more complete and structured explanation, addressing key aspects of the pagination process. Remember to replace placeholder database credentials with your actual values.
The above is the detailed content of Pagination with jQuery, AJAX and PHP. For more information, please follow other related articles on the PHP Chinese website!