Skip to content

Instantly share code, notes, and snippets.

@gianghl1983
Last active June 4, 2018 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gianghl1983/0791010e089d937b8106b8dc7ef85bb0 to your computer and use it in GitHub Desktop.
Save gianghl1983/0791010e089d937b8106b8dc7ef85bb0 to your computer and use it in GitHub Desktop.
<?php
include ('database/connect.php');
//print_r($db_qltc);
/**
* BÀI TOÁN PHÂN TRANG
* B1: Khai báo $limit_per_page (Số bản ghi trên trang)
* B2: Lấy $current_page (Trang hiện tại): $current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
* B3: Tính $offset = ($current_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
* B9: Truy xuất theo điều kiện tìm kiếm
*/
//---------------------------------------------------------------------------------
// Xoa du lieu DELETE FROM TenBang WHERE Dieu_Kien
/**
* Điều kiện xóa:
* Nếu xóa 1 bản ghi: Thường là xóa theo id (id=5)
* + Ví dụ: DELETE FORM accounts WHERE id=5
* Nếu xóa nhiều:
* + Xóa theo trạng thái: DELETE FROM accounts WHERE status = 'Open'
*/
//---------------------------------------------------------------------------------
$username = isset($_GET['user_name']) ? trim($_GET['user_name']) : '';
$account_name = isset($_GET['account_name']) ? trim($_GET['account_name']) : '';
//B1-Khai báo limit trên mỗi trang
$limit_per_page = 5;
//B2: Lấy $current_page (Trang hiện tại)
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;//hàm (int) convert về int 1a ->1, a2 ->0
//B3: Tính $offset = ($current_page - 1) * $limit_per_page;
$offset = ($current_page - 1)* $limit_per_page;
//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
$totalRecord = 0;
$sql = 'SELECT COUNT(*) as total_record FROM accounts LEFT JOIN users ON users.id=accounts.user_id WHERE 1=1';//gán truy vấn vào biến $sql
if ($username != '') {
$sql .= " AND users.fullname LIKE '%$username%'";
}
if ($account_name != '') {
$sql .= " AND accounts.name LIKE '%$account_name%'";
}
$query = $db_qltc->query($sql);//[!!!!] Thực hiện truy vấn như myPHPAdin > Trả về 1 bảng Table ảo, dùng fetch lấy???/
if ($query) {
$totalRecord = $query->fetch_row()[0];//lấy 1 bản ghi?
//echo $totalRecord;
}
//B5: Tính $totalPage = ceil($totalRecord/$limit_per_page) - làm tròn trên
$totalPage = ceil($totalRecord/$limit_per_page);
//B9: Truy xuất theo điều kiện tìm kiếm
//B10: Lọc theo điều kiện
$sql = 'SELECT accounts.*, users.fullname as user_name FROM accounts LEFT JOIN users ON users.id = accounts.user_id WHERE 1=1';
$money_order = (isset($_GET['arrange']) && $_GET['arrange'] == 'money' && $_GET['money_order'] == 'ASC') ? $money_order = 'DESC' : 'ASC';
if ($username != '') {
$sql .= " AND users.fullname LIKE '%$username%'";
}
if ($account_name != '') {
$sql .= " AND accounts.name LIKE '%$account_name%'";
}
if (isset($_GET['arrange']) && $money_order == 'ASC') {
$sql .= " ORDER BY accounts.amount ASC";
}
if (isset($_GET['arrange']) && $money_order == 'DESC') {
$sql .= " ORDER BY accounts.amount DESC";
}
$sql .= sprintf(" LIMIT %d OFFSET %d", $limit_per_page, $offset);//Lưu ý nếu sử dụng $var trong Sprintf cần tránh xung đột tham số %...
//B6: Lấy danh sách record - Theo điều kiện
$query = $db_qltc->query($sql);
//$query luôn tồn tại, có thể false do ko lấy dữ liệu, lỗi...
if ($query) {
$accounts = $query->fetch_all(MYSQLI_ASSOC);//mảng liên hợp Key -> Value
//print_r($accounts);
}
?>
<html>
<head>
<title>Danh sách Tài khoản</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<a href="account.add.php">Thêm mới Tài khoản</a><br>
<a href="province.add.php">Thêm Tỉnh</a>
<hr>
<!--Form tìm kiếm -->
<form>
<input type="text" name="user_name" value="<?php echo $username;?>" placeholder="Chủ tài khoản"></input>
<input type="text" name="account_name" value="<?php echo $account_name;?>" placeholder="Tên tài khoản"></input>
<input type="submit" name="submit" value="Tìm kiếm"></input>
</form>
<hr>
<!--Sắp xếp bản ghi -->
<a href="account_qltc.php?arrange=money&money_order=<?php echo $money_order; ?>">
<button>Xếp theo Tiền
<?php
if ($money_order == 'ASC') {
echo '▲';
}
if ($money_order == 'DESC') {
echo '▼';
} else {
echo '';
}
?>
</button>
</a>
<hr>
<!--Bảng dữ liệu -->
<hr>
<table>
<tr>
<th>STT</th>
<th>ID</th>
<th>Chủ tài khoản</th>
<th>Tên tài khoản</th>
<th>Loại tài khoản</th>
<th>Số tiền</th>
<th>Trạng thái</th>
<th>Ghi chú</th>
<th></th>
</tr>
<!--Vòng lặp hiện các bản ghi -->
<?php
//B7: Dùng vòng lặp hiển thị danh sách bản ghi
if (count($accounts) > 0):
$i = 0;
foreach ($accounts as $account):
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $account['id']; ?></td>
<td><?php echo $account['user_name']; ?></td>
<td><?php echo $account['name']; ?></td>
<td><?php echo $account['type']; ?></td>
<td><?php echo $account['amount']; ?></td>
<td><?php echo $account['status']; ?></td>
<td><?php echo $account['note']; ?></td>
<td>
<a href="account.del.php?id=<?php echo $account['id']; ?>">Xóa</a>
<a href="account.edit.php?id=<?php echo $account['id']; ?>">Sửa</a>
</td>
</tr>
<?php
//$i++;
endforeach;
endif;
?>
</table>
<hr>
<!--Vòng lặp hiện chuyển trang-->
<?php
if ($totalPage > 1):
for ($i = 1; $i <= $totalPage; $i++):
?>
<a href="account_qltc.php?page=<?php echo $i; ?>&user_name=<?php echo $username;?>&account_name=<?php echo $account_name;?>"><?php echo $i; ?></a>
<?php
endfor;
endif;
?>
<hr>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment