You are on page 1of 13

1.

Tạo thư mục web theo cấu trúc sau:

2. Tạo tệp config.php khia báo biến mảng $config[]

3. Tạo thư mục class và trong thư mục class tạo các tệp class.db.php và class.db.theloai.php

3.1. Tạo lớp DB từ tệp class.db.php như sau:


<?php

class DB{

private $link;

/**

* DB Class's constructor

* @global type $config

*/

function __construct() {

global $config;

$this->link = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['data'])


or die("Can't connect");

mysqli_query($this->link, "set names 'utf8'");

/**

* Execute a query

* @param string $query

* @return result of execute

*/

public function db_query($query){

$result = mysqli_query($this->link, $query);

return $result;

/**

* Fetch row

* @param resource $result

* @return boolean

*/

public function db_fetch($result){

$row = mysqli_fetch_assoc($result);

return $row;

?>

3.2 Tạo lớp DB_TheLoai từ tệp class.db.theloai.php


<?php

include 'class.db.php';

class DB_TheLoai extends DB{

function __construct() {

parent::__construct();

/**

* Lay danh sach the loai

* @return array $theloai

*/

public function get_theloai($id = -1){

$query = "SELECT * FROM theloai";

if($id != -1) $query .= " WHERE idTL=$id";

$result = $this->db_query($query);

$theloai = array();

$i = 0;

while($row = $this->db_fetch($result)){

$theloai[$i++] = $row;

return $theloai;

/**

* @param string $name ten the loai


* @param tinyint $sort sap xep

* @param tinyint $show an hien

* @return true on success, false on fail

*/

public function them_theloai($name, $sort = 0, $show = 1){

$query = "INSERT INTO theloai(TenTL, ThuTu, AnHien) VALUES('$name', '$sort', '$show')";

$result = $this->db_query($query);

return $result;

/**

* Sua the loai

* @param int $id id cua the loai

* @param string $name ten the loai

* @param tinyint $sort sap xep

* @param tinyint $show an hien

* @return true on success, false on fail

*/

public function sua_theloai($id, $name, $sort, $show){

$query = "UPDATE theloai SET TenTL='$name', ThuTu='$sort', AnHien='$show' WHERE idTL=$id";

$result = $this->db_query($query);

return $result;

/**

* Xoa the loai co id la $id

* @param int $id cua the loai

* @return true on success, false on fail

*/
public function xoa_theloai($id){

$query = "DELETE FROM theloai WHERE idTL=$id";

$result = $this->db_query($query);

return $result;

?>

4. Tạo trang theloai.php như sau


<?php

include 'config.php';

include 'class/class.db.theloai.php';

$db_theloai = new DB_TheLoai();

$theloai_arr = $db_theloai->get_theloai();

$theloai_html = "";

if(count($theloai_arr) == 0){

$theloai_html = 'Không có thể loại

<a href="theloai_them.php">THÊM THỂ LOẠI</a>';

}else{

$theloai_html = '<h1>Quản trị thể loại</h1>

<table width="100%" id="bang_quantri" cellpadding

="10" cellspacing="0">

<tr><th>Tên thể loại</th><th>Thứ tự</th>

<th>Ẩn hiện</th><th colspan="2">


<a href="theloai_them.php">Thêm Thể Loại</a></th>

</tr>';

foreach($theloai_arr as $item){

$theloai_html .= '<tr>';

$theloai_html .= '<td>'.$item['TenTL'].'</td>';

$theloai_html .= '<td>'.$item['ThuTu'].'</td>';

$theloai_html .= '<td>'.$item['AnHien'].'</td>';

$theloai_html .= '<td><a href="sua_theloai.php?id=

'.$item['idTL'].'">Sửa</a></td>';

$theloai_html .= '<td><a href="delete.php?id=

'.$item['idTL'].

'" onclick="return confirm(\'Bạn có thực

sự muốn xóa\');">Xóa</a></td>';

$theloai_html .= '</tr>';

$theloai_html .= '</table>';

?>

<!DOCTYPE html>

<html>

<head>

<title>THỂ LOẠI | QUẢN TRỊ</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>

<body>

<div class="container">

<?php echo $theloai_html; ?>

</div>

</body>

</html>

5. TẠo trang theloai_them.php như sau

<?php

include 'config.php';

include 'class/class.db.theloai.php';

if(isset($_POST['submit'])){

$db_theloai = new DB_TheLoai();

$ten_theloai = (isset($_POST['ten_theloai']))? $_POST['ten_theloai'] : "";

$thu_tu = (isset($_POST['thu_tu']))? $_POST['thu_tu'] : 0;

$an_hien = (isset($_POST['an_hien']))? $_POST['an_hien'] : "";

$result = $db_theloai->them_theloai($ten_theloai, $thu_tu, $an_hien);

if($result){

echo '<script>alert("Thêm thành công!"); window.location="theloai.php";</script>';

}else{

echo '<script>alert("Thêm không thành công!"); window.location="theloai.php";</script>';

?>

<!DOCTYPE html>
<html>

<head>

<title>THỂ LOẠI | QUẢN TRỊ</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>

<body>

<div class="container">

<h1>Thêm thể loại</h1>

<form name="them_theloai" method="POST">

<table width="100%">

<tr>

<td>Tên thể loại: </td>

<td><input type="text" name="ten_theloai"></td>

</tr>

<tr>

<td>Thứ tự: </td>

<td><input type="text" name="thu_tu" value="0"></td>

</tr>

<tr>

<td>Ẩn/Hiện: </td>

<td>

<select name="an_hien">

<option value="1" selected="selected">Hiện</option>

<option value="0">Ẩn</option>

</select>
</td>

</tr>

<tr>

<td colspan="2"><input type="submit" value="Thêm thể loại" name="submit"></td>

</tr>

</table>

</form>

</div>

</body>

</html>

6. TẠo trang sửa thể loại như sau: sua_theloai.php


<?php

include 'config.php';

include 'class/class.db.theloai.php';

if(isset($_GET['id'])){

$db_theloai = new DB_TheLoai();

$theloai_arr = $db_theloai->get_theloai($_GET['id']);

$ten_theloai = $theloai_arr[0]['TenTL'];

$thu_tu = $theloai_arr[0]['ThuTu'];

$an_hien = $theloai_arr[0]['AnHien'];

if(isset($_POST['submit'])){

$id = (isset($_POST['id']))? $_POST['id'] : -1;

$ten_theloai = (isset($_POST['ten_theloai']))? $_POST['ten_theloai'] : "";

$thu_tu = (isset($_POST['thu_tu']))? $_POST['thu_tu'] : 0;


$an_hien = (isset($_POST['an_hien']))? $_POST['an_hien'] : "";

$result = $db_theloai->sua_theloai($id, $ten_theloai, $thu_tu, $an_hien);

if($result){

echo '<script>alert("Cập nhật thành công!"); window.location="theloai.php";</script>';

}else{

echo '<script>alert("Cập nhật không thành công!"); window.location="theloai.php";</script>';

?>

<!DOCTYPE html>

<html>

<head>

<title>THỂ LOẠI | QUẢN TRỊ</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>

<body>

<div class="container">

<h1>Cập nhật thể loại</h1>

<form name="sua_theloai" method="POST">

<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

<table width="100%">
<tr>

<td>Tên thể loại: </td>

<td><input type="text" name="ten_theloai" value="<?php echo $ten_theloai; ?>"></td>

</tr>

<tr>

<td>Thứ tự: </td>

<td><input type="text" name="thu_tu" value="<?php echo $thu_tu; ?>"></td>

</tr>

<tr>

<td>Ẩn/Hiện: </td>

<td>

<select name="an_hien">

<option value="1">Hiện</option>

<option value="0" <?php echo ($an_hien == 0)?'selected="selected"':''; ?


>>Ẩn</option>

</select>

</td>

</tr>

<tr>

<td colspan="2"><input type="submit" value="Cập nhật thể loại" name="submit"></td>

</tr>

</table>

</form>

</div>

</body>

</html>

<?php

7. Tạo trang delete.php như sau


include 'config.php';

include 'class/class.db.theloai.php';

if(isset($_GET['id'])){

$db_theloai = new DB_TheLoai();

$id = $_GET['id'];

$result = $db_theloai->xoa_theloai($id);

if($result){

echo '<script>alert("Xóa thành công!"); window.location="theloai.php";</script>';

}else{

echo '<script>alert("Xóa không thành công!"); window.location="theloai.php";</script>';

?>

<!DOCTYPE html>

<html>

<head>

<title>THỂ LOẠI | QUẢN TRỊ</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>

<body>

<div class="container">

<h1>Xóa thể loại</h1>

</div>
</body>

</html>

You might also like