បង្កើត Pagination ជាមួយ PHP និង Mysql

វិធីបំបែកទំព័រចេញជាផ្នែកៗ ជាគំនិតមួយដែលកំពុងពេញនិយម សំរាប់អ្នកបង្កើត និង រចនា គេហទំព័រ។ វិធីនេះភាកច្រើន គេប្រើនៅពេលដែលទាញទិន្នន័យចេញពី Database ហើយទិន្នន័យនោះមានចំនួនច្រើន គេត្រូវបំបែកវាដាក់ជាច្រើនទំព័រ ដើម្បីងាយមើល និងធ្វើអោយគេហទំព័រដំណើរការលឿនទៀតផង។
ខាងក្រោមនេះជាការបង្ហាញពីការ បំបែកទំព័រជាផ្នែកៗ ជាមួយភាសា PHP និង Mysql ដែលខ្ញុំបានប្រើជាមួយ គំរោងរបស់ខ្ញុំកន្លងមក។
១.បង្កើត Database និង Table
ដំបូងយើងត្រូវមាន Database រូចហើយត្រូវបង្កើត Table ។ ខ្ញុំបង្កើតដាតាបេស មួយឈ្មោះ learnphp ហើយ table ឈ្មោះ product
CREATE TABLE `product` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(50) NOT NULL default ” ,
`price` decimal(10,2) NOT NULL default ‘0.00′,
`on_promotion` tinyint(4) NOT NULL default ‘0′,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=57 ;
រូចបញ្ចូលទិន្នន័យខាងក្រោមនេះ(សូមបញ្ជាក់លោកអ្នកអាចបញ្ចួលវាអោយច្រើនជានេះក៏បានដែរ)
INSERT INTO `product` (`id`, `name`, `price`, `on_promotion`) VALUES
(1, ‘Santa Costume’, ‘14.99′, 0),
(2, ‘Medieval Lady’, ‘49.99′, 1),
(3, ‘Caveman’, ‘12.99′, 0),
(4, ‘Costume Ghoul’, ‘18.99′, 0);
២. បង្កើត file ‘config.php’
file នេះបានកំនត់ Constant មួយចំនូនដែលចាំបាច់ភ្ជាប់ទៅកាន់ Database
<?php
// defines database connection data
define('DB_HOST', 'localhost');//host name
define('DB_USER', 'root');// Database user
define('DB_PASSWORD', '');//database password
define('DB_DATABASE', 'learnphp'); //database name
// defines the number of visible rows in grid
define('ROWS_PER_VIEW', 10);//ចំនូនដែលត្រូវបង្ហាញក្នុង ទំព័រនីមួយៗ
?>
៣. បង្កើត Class ‘database.class.php’
class នេះមានទូនាទីទាញយក ទិន្នន័យពី database, កំនត់ចំនួន Product ដែលត្រូវបង្ហាញ និងចំនួនទំព័រដែលត្រូវបំបែកចេញ។
ប្រហែលប្លែកពីអ្នករាល់គ្នាធ្លាប់ប្រើហើយមើលទៅ ចំពោះការភ្ជាប់ កាន់ database ខ្ញុំបានប្រើ class Mysqli ដើម្បីភ្ជាប់ទៅកាន database។ ព្រោះ Mysqli
មានលក្ខណះជា OOP ហើយងាយស្រូលប្រើ វា support តែ PHP5 ឡើងទៅ។
<?php
/**
*Database class to connect to database
*author: Prak Sophy
*email:sophy.prak@gmail.com
*phone:011735918
*date:28-01-2009
*/
require_once('config.php');
class Database
{
/*
* ចំនួនទំព័រសរុប
*/
public $totalPages;
/*
* ចំនួន product
*/
public $itemCount;
/*
* ទំព័រដែលត្រូវផ្តល់អោយ
*/
public $returnedPage;
/*
* database handler
* to use Mysqli Class support php 5.x
*/
private $mMysqli;
/*
* Table name to use
*/
private $tableName;
/*
* Class Contructor
*/
function __construct($table)
{
// create the MySQL connection
$this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,
DB_DATABASE);
$this->tableName = $table;
// call countAllRecords to get the number of grid records
$this->itemCount = $this->countAllRecords();
}
// class destructor, closes database connection
function __destruct()
{
$this->mMysqli->close();
}
/*
* count all products
* @return number of product rows
*/
private function countAllRecords()
{
// Query select count product
$query = "SELECT COUNT(*) FROM $this->tableName";
if ($result = $this->mMysqli->query($query))
{
$row = $result->fetch_row();
$result->close();
}
return $row[0];
}
public function readPages($page)
{
// create the SQL query that returns a page of products
$query = $this->createSubpageQuery("SELECT * FROM $this->tableName", $page);
if ($result = $this->mMysqli->query($query))
{
$i=0;
while($rows = $result->fetch_assoc())
{
$row[$i]['id'] = $rows['id'];
$row[$i]['name'] = $rows['name'];
$row[$i]['price'] = $rows['price'];
$row[$i]['on_promotion'] = $rows['on_promotion'];
$i++;
}
//return row of product
return $row;
$result->close();
}
}
private function createSubpageQuery($query,$pageNo)
{
// if we have few products then we don't implement pagination
if ($this->itemCount <= ROWS_PER_VIEW)
{
$pageNo = 1;
$this->totalPages = 1;
}
// else we calculate number of pages and build new SELECT query
else
{
$this->totalPages= ceil($this->itemCount / ROWS_PER_VIEW);
$start_page = ($pageNo - 1) * ROWS_PER_VIEW;
$query.= ' LIMIT ' . $start_page . ',' . ROWS_PER_VIEW;
}
// save the number of the returned page
$this->returnedPage = $pageNo;
// returns the new query string
return $query;
}
}
?>
៤. បង្កើត file ‘product.php’ ដើម្បី test class ‘database.class.php’
<style type="text/css">
a span{
margin-left:2px;
}
</style>
<?php
require_once 'database.class.php';
//request page number
$page = intval($_REQUEST['page']);
//page number 0 or less than 0
if($page <= 0)
$page =1;
$products = new Database('product');
//read page and get products
$rows = $products->readPages($page);
//Write pagination
echo 'Pages:';
for($i=1; $i <= $products->totalPages; $i++)
{
echo '<a href="products.php?page='.$i.'"><span>'.$i.'</span></a>';
}
?>
<table border="1" cellspacing="0" width="400">
<thead>
<th>Id</th>
<th>Name</th>
<th>Price</th>
</thead>
</tbody>
<?php
if(count($rows )>0)
{
foreach ($rows as $row )
{
echo '<tr><td>'.$row['id'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['price'].'</td></tr>';
}
}
?>
</tbody>
</table>




7 វិចារ