By GokiSoft.com| 19:09 24/05/2022|
Học PHP

[Source Code] Thi kết thúc khóa học PHP - C2110L

[Examination] Thi kết thúc khóa học PHP - Đề 1

#config.php


<?php
define('HOST', 'localhost');
define('DATABASE', 'BT2319');
define('USERNAME', 'root');
define('PASSWORD', '');


#dbhelper.php


<?php
require_once('config.php');

/**
 * Insert, Update, Delete
 */
function execute($sql) {
	// Them du lieu vao CSDL
	// B1. Cach ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2. Luu du lieu
	// $sql = "delete from student where id = $id";
	mysqli_query($conn, $sql);

	// B3. Ngat ket noi toi CSDL
	mysqli_close($conn);
}

/**
 * Select
 */
function executeResult($sql, $isSingle = false) {
	// Them du lieu vao CSDL
	// B1. Cach ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2. Luu du lieu
	$resultset = mysqli_query($conn, $sql);

	if($isSingle) {
		$data = mysqli_fetch_array($resultset, 1);
	} else {
		$data = [];
		while(($row = mysqli_fetch_array($resultset, 1)) != null) {
			$data[] = $row;
		}
	}

	// B3. Ngat ket noi toi CSDL
	mysqli_close($conn);

	return $data;
}


#index.php


<?php
require_once('dbhelper.php');

if(isset($_GET['s'])) {
	$s = $_GET['s'];
	$sql = "select * from book where title like '%$s%'";
} else {
	$sql = "select * from book";
}

$dataList = executeResult($sql);
?>

<!DOCTYPE html>
<html>
<head>
	<title>Book Management</title>
	<meta charset="utf-8">

	<meta name="viewport" content="width=device-width, initial-scale=1">
	<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;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="get">
		<input type="text" name="s" class="form-control" style="max-width: 200px; margin-bottom: 20px;" placeholder="Searching ...">
	</form>
	<table class="table table-bordered">
		<thead>
			<tr>
				<th>No</th>
				<th>Title</th>
				<th>ISBN</th>
				<th>Pub Year</th>
				<th>Available</th>
			</tr>
		</thead>
		<tbody>
<?php
$count = 0;
foreach ($dataList as $item) {
	echo '<tr>
			<td>'.++$count.'</td>
			<td>'.$item['title'].'</td>
			<td>'.$item['isbn'].'</td>
			<td>'.$item['pub_year'].'</td>
			<td>'.number_format($item['available'], 0).'</td>
		</tr>';
}
?>
		</tbody>
	</table>
</div>
</body>
</html>


#readme.txt


Các bước triển khai dự án
1) Tạo CSDL
create database BT2319

create table book (
	bookid int primary key auto_increment,
	authorid int,
	title varchar(255),
	isbn varchar(25),
	pub_year smallint,
	available tinyint
)

fake data

2) Tạo cấu trúc dự án
	-config.php
	-dbhelper.php
	-index.php


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)