<?php
require 'Pager.php';
$dsn = 'pgsql://postgres@localhost/erp';
if (DB::isError($con
= DB::connect($dsn))){
die (DB::errorMessage($con));
}
$sql = "select
* from clients";
if (DB::isError($res = $con->query($sql))){
die (DB::errorMessage($res));
}
//
$from คือตัวแปรที่รับค่ามาจากการเรียก URL เช่น
// http://www.host.com/list.php?from=50
// แต่ถ้าเราไม่ได้กำหนดจาก URL โปรแกรมจะกำหนดให้เท่ากับ
0
if (isset($_GET['from'])
{
$from = $_GET['from'];
}else{
$from = 0;
}
//
$limit คือ ตัวแปรจำนวนแถวข้อมูลที่เราต้องการแสดงในแต่ละหน้า
$limit = 10;
$pager = new DB_Pager ($res, $from, $limit);
//
$data ตรงนี้จะมีข้อมูลทั้งหมดที่จะใช้สร้าง page
// เพื่อทำ link และแสดงทางหน้าจอให้ผู้ใช้เห็น
$data = $pager->build();
if (DB::isError($data)){
die (DB::errorMessage($data));
}
if (!$data) {
die ('There were no results');
}
//
ตัวอย่างการแสดงข้อมูล
echo '<html><body>';
echo $data['numrows'] . ' Results found';
echo 'Page '. $data['current'] . ' of ' . $data['numpages'];
echo $data['limit'] . ' results per page';
//
ปุ่ม Previous
echo '<a href="'.$_SERVER['PHP_SELF']."?from=".$data['prev'].'">
'.' <-prev</a>';
//
สร้าง link ไปที่หน้าต่างๆ
foreach ($data['pages']
as $page => $start_row) {
echo '| <a href="'.$_SERVER['PHP_SELF']."?from=".$start_row.'">'.$page.'</a>';
}
//
ปุ่ม Next
echo '<a href="'.$_SERVER['PHP_SELF']."?from=".$data['next'].'">
'.' next-></a>';
//
fetch เฉพาะข้อมูลที่แสดงในหน้านี้อย่างอัตโนมัติ
// สามารถใช้ function fetchInto() ได้เช่นกัน
while ($row = $pager->fetchRow(DB_FETCHMODE_ASSOC)){
echo $row['id'] . "\n";
}
echo '</body></html>';
?> |