<?php
/**
* Sample Use of PaginationLinks with PDO and SQLite
* Works with other databases like MySQL, MSSQL etc. too!
*
* @package PaginantionLinks
*/
require_once '../src/PaginationLinks.php';
use mirazmac\PaginationLinks;
// Database file path
$db_file = __DIR__ .'/demoDataBase.sqlite';
// Connect to database using PDO
$db = New PDO('sqlite:'.$db_file);
// Current page number
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
// Number of items/rows per page
$items_per_page = 10;
// Fetch Number of total items from database
$sql = "SELECT COUNT(id) AS total_count FROM demoTable";
$query = $db->prepare($sql);
$query->execute();
$total_items = $query->fetch()['total_count'];
// PaginationLinks options
$options = ['url' => '?page=%_PAGE%'];
// Create pagination instance
$pagination = New PaginationLinks($options);
// Build the pagination links
$pagination->buildPages($total_items, $current_page, $items_per_page);
// Get offset number
$start = $pagination->getStart();
// Query the database to fetch paginated rows
$sql = "SELECT * FROM `demoTable` LIMIT $start, $items_per_page";
$query = $db->prepare($sql);
$query->execute();
while ($row = $query->fetch()) {
echo '<pre>';
echo $row['id'].'.';
echo $row['name'];
echo '</pre>';
}
echo '<hr/>';
// Display the pagination links ( Whoa! that was easy!! )
echo $pagination->getPages();
/** Thats all folks! */
|