By GokiSoft.com|
14:57 27/04/2022|
Học PHP
[Video] Kiểm Tra 60 phút - Test nhanh - Khóa học PHP - C2110I
[Examination] Kiểm Tra 60 phút - Test nhanh - Khóa học PHP
#readme.txt
Các bước triển khai dự án:
B1) Tạo CSDL
create database if not exists BT2289
create table if not exists product (
id int primary key auto_increment,
title varchar(150),
price float,
thumbnail varchar(500),
content text,
created_at datetime,
updated_at datetime
)
B2) Xây dựng khung dự án
B3) Viết modules chương trình
B4) Kỹ thuật phân trang
- Xác định số sản phẩm hiển thị trong 1 trang: 10
- Yêu tố xác định trang đang hiển thị -> page = ? -> trên URL -> Lấy thông quan GET
- page 1: 0 -> 9
- page 2: 10 -> 19
- page 3: 20 -> 29
...
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'BT2289');
#dbhelper.php
<?php
require_once('config.php');
function execute($sql) {
// B1) Tao ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Thuc hien cau insert into
// echo $sql;
// die();
// insert, update, delete
mysqli_query($conn, $sql);
// B3) Dong ket noi
mysqli_close($conn);
}
function executeResult($sql, $isSingle = false) {
// B1) Tao ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Thuc hien cau insert into
$resultset = mysqli_query($conn, $sql);
$data = null;
if($isSingle) {
$data = mysqli_fetch_array($resultset, 1);
} else {
$data = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row; //$row -> array key & value
}
}
// B3) Dong ket noi
mysqli_close($conn);
return $data;
}
#utility.php
<?php
// Viet cau truy van theo cau truc: $sql = "???";
function getPost($key) {
$value = '';
if(isset($_POST[$key])) {
$value = $_POST[$key];
$value = str_replace("'", "\\'", $value);
}
return $value;
}
// Viet cau truy van theo cau truc: $sql = "???";
function getGet($key) {
$value = '';
if(isset($_GET[$key])) {
$value = $_GET[$key];
$value = str_replace("'", "\\'", $value);
}
return $value;
}
function getTimeFormat($str) {
$mydate = new DateTime($str);
return $mydate->format('H:i d/m/Y');
}
#detail.php
<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');
$id = getGet('id');
$sql = "select * from product where id = '$id'";
$product = executeResult($sql, true);
if($product == null) {
header('Location: product.php');
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?=$product['title']?> Page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
.card {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<p>
<a href="product.php">Product List</a>
</p>
<div class="row">
<div class="col-md-5">
<img src="<?=$product['thumbnail']?>" style="width: 100%">
</div>
<div class="col-md-7">
<h2><?=$product['title']?></h2>
<h3>Price: <?=number_format($product['price'], 0)?> VND</h3>
<p><?=$product['content']?></p>
</div>
</div>
</div>
</body>
</html>
#product.php
<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');
$s = getGet('s');
// Xác định trang
$limit = 5;
$page = getGet('page');
if(empty($page) || $page <= 0) {
$page = 1;
}
$index = ($page - 1) * $limit;
// Xac dinh so page trong trang: START
if(empty($s)) {
$sql = "select count(*) total from product";
} else {
$sql = "select count(*) total from product where title like '%$s%'";
}
$data = executeResult($sql, true);
$total = $data['total'];
$pageNum = ceil($total / $limit);
// Xac dinh so page trong trang: END
if(empty($s)) {
$sql = "select * from product limit $index, $limit";
} else {
$sql = "select * from product where title like '%$s%' limit $index, $limit";
}
$productList = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Product Management Page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
.card {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<form method="get">
<input type="text" name="s" placeholder="Searching ..." class="form-control mb-4" style="width: 200px; float: right;">
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
<th>Updated At</th>
</tr>
</thead>
<tbody>
<?php
// $index = 0;
foreach($productList as $item) {
echo '<tr>
<td>'.(++$index).'</td>
<td><a href="detail.php?id='.$item['id'].'"><img src="'.$item['thumbnail'].'" style="width: 120px" /></a></td>
<td>'.$item['title'].'</td>
<td>'.number_format($item['price'], 0).'</td>
<td>'.getTimeFormat($item['updated_at']).'</td>
</tr>';
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<?php
if($page > 1) {
echo '<a href="?page='.($page - 1).'"><button class="btn btn-warning" style="float: left">Previous</button></a>';
}
if(count($productList) == $limit) {
echo '<a href="?page='.($page + 1).'"><button class="btn btn-warning" style="float: right">Next</button></a>';
}
?>
</div>
</div>
<div class="row">
<div class="col-md-12 mt-5">
<ul class="pagination">
<?php
if($page > 1) {
echo '<li class="page-item"><a class="page-link" href="?page='.($page - 1).'">Previous</a></li>';
}
for ($i=1; $i <= $pageNum; $i++) {
if($i == $page) {
echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"?page=$i\">$i</a></li>";
} else {
echo "<li class=\"page-item\"><a class=\"page-link\" href=\"?page=$i\">$i</a></li>";
}
}
if(count($productList) == $limit) {
echo '<li class="page-item"><a class="page-link" href="?page='.($page + 1).'">Next</a></li>';
}
?>
</ul>
</div>
</div>
</div>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)