Phân trang trong PHP hay còn gọi là PHP pagination, là 1 việc rất quan trọng đối với mọi trang web có trang danh sách (trang list).
Với 1 danh sách dài dằng dặc, ví dụ một danh sách liệt kê tất cả các nhân sự của 1 công ty lớn nào đó.
Giả sử công ty đó có 1000 nhân viên. Nếu liệt kê tất cả thông tin của các nhân viên trên cùng 1 trang web thì thật là kinh khủng. Chính vì lẽ đó, việc phân trang trong PHP (PHP pagination) đã ra đời.
Để minh họa cho bài viết hướng dẫn ngay sau đây, mình đã tạo ra 1 project mới tên là Pagination, mô tả lại cách thức phân trang như thế nào trong PHP.
Các bạn có thể tải về source code của project này tại đây!
Thư mục dự án Pagination có cấu trúc như hình dưới:
1) Trang index.php là trang hiển thị danh sách. (Ex: danh sách các thành viên)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Human Resource Management</title>
</head>
<body>
<br />
<center>
<table border=1>
<tr>
<th>MemberID</th>
<th>MemberName</th>
<th>Birthday</th>
<th>Place</th>
<th>Phone</th>
<th>Email</th>
</tr>
<?php
include_once ‘BizManager/ConnectionManager.php’;
include_once ‘BizManager/PagingManager.php’;
// Show Members List
$paging_instance = new PagingManager();
if (isset($_GET[‘p’]))
{
// Get start record
$start_row = ($_GET[‘p’] * PagingManager::$pagesize) – PagingManager::$pagesize;
}
else
{
$start_row = 0;
}
$result = $paging_instance->getData(“Select * from Members limit $start_row,” . PagingManager::$pagesize);
$select_count = $paging_instance->getData(“Select count(*) from Members”);
$page_count = $select_count/PagingManager::$pagesize;
// Display data in html view
while ($row = mysql_fetch_array($result))
{
echo “<tr>
<td>” . $row[‘MemberID’] . “</td>
<td>” . $row[‘MemberName’] . “</td>
<td>” . $row[‘Birthday’] . “</td>
<td>” . $row[‘Place’] . “</td>
<td>” . $row[‘Phone’] . “</td>
<td>” . $row[‘Email’] . “</td>
</tr>”;
}
echo “<tr>
<td colspan=6>”;
for($i = 1; $i <= ceil($page_count);$i++)
{
echo “<b><a href=” . “?p=$i” . ” style=\”text-decoration:none\”>$i </a></b>”;
}
echo “</td>
</tr>
</table></center>”;
?>
</table>
</center>
</body>
</html>
2) Trang ConnectionManager.php thiết lập thông tin kết nối với mySQL
<?php
/**
* This class contains functions of implementing pagination
*
* @author Luu Van Phong <luuvanphong@outlook.com>
*/
class ConnectionManager {
public static $db_hostname = “localhost”;
public static $db_database = “hrm”; // Human Resource Management
public static $db_username = “root”;
public static $db_password = “”;
}
?>
3) Trang PagingManager.php kết nối và xử lý dữ liệu truyền về từ mySQL
<?php
/**
* This class contains functions of implementing pagination
*
* @author Luu Van Phong <luuvanphong@outlook.com>
*/
class PagingManager {
// PageSize default value
public static $pagesize = 3;
// Connect to mySQL
public function openConnection($hostname, $database, $username, $password)
{
// Connect to the server
$server_connect = mysql_connect($hostname, $username, $password);
if (!$server_connect) die(“Can not connect to the server.<br/>”);
// Connect to get data from database
$db_connect = mysql_select_db($database);
if (!$db_connect) die(“Can not connect to the database.<br/>”);
}
// Close database connection
public function closeConnection()
{
mysql_close();
}
// Select data from the database
public function getData($query)
{
$this->openConnection(ConnectionManager::$db_hostname, ConnectionManager::$db_database,
ConnectionManager::$db_username, ConnectionManager::$db_password);
$result = mysql_query($query);
$this->closeConnection();
return $result;
}
}
?>
À còn file .sql nữa nhỉ?!
Database phục vụ cho project Pagination là hrm
Các bạn tải về tại đây nhé!