PHP Classes

File: examples/PDOExample.php

Recommend this page to a friend!
  Classes of Miraz Mac   PHP Pagination Links   examples/PDOExample.php   Download  
File: examples/PDOExample.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Pagination Links
Display links to browse pages of entry listings
Author: By
Last change: Added renderHtml() method and updated examples
Initial Commit
Date: 1 year ago
Size: 1,190 bytes
 

Contents

Class file image Download
<?php

/**
 * Sample Use of PaginationLinks with PDO and SQLite
 * Works with other databases like MySQL, MSSQL etc. too!
 *
 * @package PaginantionLinks
 */

require_once '../src/Pagination.php';

use
MirazMac\Pagination\Pagination;

// Database file path
$db_file = __DIR__ .'/demoDataBase.sqlite';

// Connect to database using PDO
$db = new PDO('sqlite:'.$db_file);

// Current page number
$current_page = (int)@$_GET['page'];

// 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'];

// Create pagination instance
$pagination = new Pagination($total_items, $current_page);
$pages = $pagination->parse();

// Get offset number
$start = $pagination->offset();
// 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/>';

echo
$pagination->renderHtml();