php Phan trang PHP - MySQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Phan trang PHP - MySQL相关的知识,希望对你有一定的参考价值。

<?php 
include 'database/db.php';

$accounts = [];
$limit = 2;

/**
 * BÀI TOÁN PHÂN TRANG
 * B1: Khai báo $limit (Số bản ghi trên trang)
 * B2: Lấy $page (Trang hiện tại): $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
 * B3: Tính $offset  = ($page - 1) * $limit;
 * B4: Đếm tổng số bản ghi (Có kèm điều kiện hoặc không có điều kiện): $totalRecord
 * B5: Tính $totalPage = ceil($totalRecord/$limit)
 * B6: Lấy danh sách record
 * B7: Dùng vòng lặp hiển thị danh sách bản ghi
 * B8: Dùng vòng lặp để hiện ra số trang theo $totalPage
 */


$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

$offset  = ($page - 1) * $limit;

$totalRecord = 0;

$sql = "SELECT COUNT(*) as total_record FROM accounts LEFT JOIN users ON users.id=accounts.user_id WHERE 1=1";

$username = isset($_GET['user_name']) ? trim($_GET['user_name']) : '';
$accountName = isset($_GET['account_name']) ? trim($_GET['account_name']) : '';

$where = '';

if ($username != '') {
    $where .= " AND users.fullname LIKE '%{$username}%'";
}

if ($accountName != '') {
    $where .= " AND accounts.name LIKE '%{$accountName}%'";
}
$sql .= $where;



$sql = sprintf("SELECT COUNT(*) as total_record FROM accounts LEFT JOIN users ON users.id=accounts.user_id WHERE 1=1");
$query = $db->query($sql);
if ($query) {
    //Lay mot ban ghi
    $totalRecord = $query->fetch_row()[0];
}

$totalPage = ceil($totalRecord/$limit);
//cach 1 %s - %d - %b....
$sql = sprintf("SELECT accounts.*, users.fullname as user_name FROM accounts LEFT JOIN users ON users.id=accounts.user_id WHERE 1=1 LIMIT %d OFFSET %d", $limit, $offset);

$query = $db->query($sql);
if ($query) {
    $accounts = $query->fetch_all(MYSQLI_ASSOC);//key-value
}

?>
<form action="" method="GET">
<input type="text" name="user_name" placeholder="Chủ tài khoản">
<input type="text" name="account_name" placeholder="Tên tài khoản">
<button type="submit" name="search">Tìm kiếm</button>

</form>
<table>
<thead>
    <tr>
        <th>STT</th>
        <th>ID</th>
        <th>Tên tài khoản</th>
        <th>Chủ tài khoản</th>
        <th>Loại tài khoản</th>
        <th>Ghi chú</th>
        <th>Số tiền</th>
        <th>Trạng thái</th>
        <th></th></th>
    </tr>
</thead>
<tbody>
<?php 
if (count($accounts) > 0) :
    $i = 0;
    foreach ($accounts as $item) :
        $i++;
?>
    <tr>
        <td><?php echo $i;?></td>
        <td><?php echo $item['id'];?></td>
        <td><?php echo $item['name'];?></td>
        <td><?php echo $item['user_name'];?></td>
        <td><?php echo $item['type'];?></td>
        <td><?php echo $item['note'];?></td>
        <td><?php echo $item['amount'];?></td>
        <td><?php echo $item['status'];?></td>
        <td></td>
    </tr>    
<?php
    endforeach;
endif;
?>
</tbody>
</table>

<?php
    if ($totalPage > 0) : 
        for ($i = 1; $i <= $totalPage; $i++) :
?>

<a href="account.php?page=<?php echo $i;?>"><?php echo $i;?></a>
<?php 
        endfor;
    endif;
?>

以上是关于php Phan trang PHP - MySQL的主要内容,如果未能解决你的问题,请参考以下文章

markdown 静态PHP7分析与phan和

phpcs,phpmd,phan安装部署

为啥 www.example.com/index.php/my/path/here 由 index.php 处理?

PHP 警告:count():参数必须是在 C:\\htdocs\my.php 中实现 Countable 的数组或对象 [关闭]

my_config.ini 与 my_config.php

PHP,MY SQL错误查询[重复]