| 
<?php
include '../Pager.php';
 
 $itemsPerPage = 10;
 $currentPage = (isset($_GET['p']) && $_GET['p'] != '') ? (int)$_GET['p'] : 1;
 
 $dbh = new PDO('mysql:host=localhost;dbname=pager_test', 'root', '');
 
 $countStmt = 'SELECT count(*) as cnt FROM data';
 $sth = $dbh->prepare($countStmt);
 $sth->execute();
 
 $res = $sth->fetch();
 
 $totalCount = $res['cnt'];
 
 $pager = new Pager($totalCount, $currentPage);
 
 $offset = $pager->getOffset();
 
 if ($offset != -1) {
 $pageStmt = 'SELECT country, cc_alpha2, cc_alpha3, cc_num FROM data LIMIT '
 . $offset . ', ' . $itemsPerPage;
 
 $sth = $dbh->prepare($pageStmt);
 $sth->execute();
 
 $pageData = $sth->fetchAll(PDO::FETCH_ASSOC);
 } else {
 $pageData = array();
 }
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Pager - MySQL example</title>
 <link rel="stylesheet" type="text/css" href="example.css">
 </head>
 <body>
 <?php if (!empty($pageData)) { ?>
 
 <table class="data">
 <?php foreach ($pageData as $data) { ?>
 <tr><td><?php echo implode('</td><td>', $data); ?></td></tr>
 <?php } ?>
 </table>
 
 <?php } else { ?>
 <div>No records found</div>
 <?php } ?>
 
 <?php echo $pager; ?>
 </body>
 </html>
 |