NenTang.vn
Thiết kế Lập trình Web BackendLập trình căn bản PHP
Tạo chức năng CRUD (Xem-Read, Thêm-Create, Sửa-Edit, Xóa-Delete) cho danh mục cho Liên kết Khóa ngoại
Chương 10 · Bài 8
Dự án thực tế mẫu (PHP thuần) - Trang web bán hàng trực tuyến - Thiết kế Backend
Thời lượng: 70 phútĐọc khoảng: 22 phút
Danh mục hình và bảng
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Chúng ta sẽ cùng tạo chức năng CRUD (Create, Read, Update, Delete) một danh mục phẳng. Cụ thể là danh mục có liên kết khóa ngoại Sản phẩm
Sơ đồ ER các mối quan hệ liên quan đến table
web02_salomon_sanpham_reference-1002x1024
Step 1: tạo cấu trúc thư mục cho chức năng CRUD
ToChucSourceCodeChucNangCRUD-PHP
Step 2: tạo chức năng
Step 2.2: bổ sung thư viên
Step 3: tạo chức năng
Step 4: tạo chức năng
Step 5: tạo chức năng
Step 5.2: bổ sung popup cảnh báo khi xóa, sử dụng thư viện
Sơ đồ ER các mối quan hệ liên quan đến table sanpham
- Trong table
sanphamcó 3 khóa ngoại (Foreign key) liên kết đến 3 tableloaisanpham,nhasanxuat,khuyenmai. Do đó, thông thường trong các chức năng CRUD của tablesanpham, chúng ta cũng cần phải lấy dữ liệu của những table cha (loaisanpham, nhasanxuat, khuyenmai).
Step 1: tạo cấu trúc thư mục cho chức năng CRUD Sản phẩm
- Để tiện cho việc quản lý, ta sẽ tạo cấu trúc thư mục tương ứng với tên của từng chức năng
- Tạo thư mục
/backend/functions/sanphamdùng để quản lý các chức năng CRUD của danh mục Nhà cung cấp
Step 2: tạo chức năng index dùng để hiển thị màn hình danh sách các Sản phẩm có trong database
- Tạo file
/backend/functions/sanpham/index.phpđể xử lý logic/nghiệp vụ - Workflow của chức năng này:
- Cần phải
JOINđến các table cha liên quan để lấy thông tin - Cần phải
định dạng (format)để hiển thị dữ liệu một cách trực quan (như ngày tháng, số tiền, ...)
- Cần phải
- Nội dung file:
php
1<!-- Nhúng file cấu hình để xác định được Tên và Tiêu đề của trang hiện tại người dùng đang truy cập -->2<?php include_once(__DIR__ . '/../../layouts/config.php'); ?>3 4<!DOCTYPE html>5<html>6 7<head>8 <!-- Nhúng file quản lý phần HEAD -->9 <?php include_once(__DIR__ . '/../../layouts/head.php'); ?>10</head>11 12<body class="d-flex flex-column h-100">13 <!-- header -->14 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>15 <!-- end header -->16 17 <div class="container-fluid">18 <div class="row">19 <!-- sidebar -->20 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>21 <!-- end sidebar -->22 23 <main role="main" class="col-md-10 ml-sm-auto px-4 mb-2">24 <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">25 <h1 class="h2">Danh sách</h1>26 </div>27 28 <!-- Block content -->29 <?php30 // Truy vấn database để lấy danh sách31 // 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn32 include_once(__DIR__ . '/../../../dbconnect.php');33 34 // 2. Chuẩn bị câu truy vấn $sql35 // Sử dụng HEREDOC của PHP để tạo câu truy vấn SQL với dạng dễ đọc, thân thiện với việc bảo trì code36 $sql = <<<EOT37 SELECT sp.*38 , lsp.lsp_ten39 , nsx.nsx_ten40 , km.km_ten, km.km_noidung, km.km_tungay, km.km_denngay41 FROM `sanpham` sp42 JOIN `loaisanpham` lsp ON sp.lsp_ma = lsp.lsp_ma43 JOIN `nhasanxuat` nsx ON sp.nsx_ma = nsx.nsx_ma44 LEFT JOIN `khuyenmai` km ON sp.km_ma = km.km_ma45 ORDER BY sp.sp_ma DESC46EOT;47 48 // 3. Thực thi câu truy vấn SQL để lấy về dữ liệu49 $result = mysqli_query($conn, $sql);50 51 // 4. Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng52 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT53 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về54 $formatedData = [];55 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {56 $km_tomtat = '';57 if (!empty($row['km_ten'])) {58 // Sử dụng hàm sprintf() để chuẩn bị mẫu câu với các giá trị truyền vào tương ứng từng vị trí placeholder59 $km_tomtat = sprintf(60 "Khuyến mãi %s, nội dung: %s, thời gian: %s-%s",61 $row['km_ten'],62 $row['km_noidung'],63 // Sử dụng hàm date($format, $timestamp) để chuyển đổi ngày thành định dạng Việt Nam (ngày/tháng/năm)64 // Do hàm date() nhận vào là đối tượng thời gian, chúng ta cần sử dụng hàm strtotime() 65 // để chuyển đổi từ chuỗi có định dạng 'yyyy-mm-dd' trong MYSQL thành đối tượng ngày tháng66 date('d/m/Y', strtotime($row['km_tungay'])), //vd: '2019-04-25'67 date('d/m/Y', strtotime($row['km_denngay']))68 ); //vd: '2019-05-10'69 }70 $formatedData[] = array(71 'sp_ma' => $row['sp_ma'],72 'sp_ten' => $row['sp_ten'],73 // Sử dụng hàm number_format(số tiền, số lẻ thập phân, dấu phân cách số lẻ, dấu phân cách hàng nghìn) 74 // để định dạng số khi hiển thị trên giao diện. 75 // Vd: 15800000 -> format thành 15,800,000.66 vnđ76 'sp_gia' => number_format($row['sp_gia'], 2, ".", ",") . ' vnđ',77 'sp_giacu' => number_format($row['sp_giacu'], 2, ".", ",") . ' vnđ',78 'sp_mota_ngan' => $row['sp_mota_ngan'],79 'sp_mota_chitiet' => $row['sp_mota_chitiet'],80 'sp_ngaycapnhat' => date('d/m/Y H:i:s', strtotime($row['sp_ngaycapnhat'])),81 'sp_soluong' => number_format($row['sp_soluong'], 0, ".", ","),82 'lsp_ma' => $row['lsp_ma'],83 'nsx_ma' => $row['nsx_ma'],84 'km_ma' => $row['km_ma'],85 // Các cột dữ liệu lấy từ liên kết khóa ngoại86 'lsp_ten' => $row['lsp_ten'],87 'nsx_ten' => $row['nsx_ten'],88 'km_tomtat' => $km_tomtat,89 );90 }91 ?>92 93 <!-- Nút thêm mới, bấm vào sẽ hiển thị form nhập thông tin Thêm mới -->94 <a href="create.php" class="btn btn-primary">95 Thêm mới96 </a>97 <table class="table table-bordered table-hover mt-2">98 <thead class="thead-dark">99 <tr>100 <th>Mã Sản phẩm</th>101 <th>Tên Sản phẩm</th>102 <th>Giá</th>103 <th>Giá cũ</th>104 <th>Mô tả</th>105 <th>Ngày cập nhật</th>106 <th>Số lượng</th>107 <th>Loại sản phẩm</th>108 <th>Nhà sản xuất</th>109 <th>Khuyến mãi</th>110 <th>Hành động</th>111 </tr>112 </thead>113 <tbody>114 <?php foreach($formatedData as $sanpham): ?>115 <tr>116 <td><?= $sanpham['sp_ma'] ?></td>117 <td><?= $sanpham['sp_ten'] ?></td>118 <td><?= $sanpham['sp_gia'] ?></td>119 <td><?= $sanpham['sp_giacu'] ?></td>120 <td>121 <?= $sanpham['sp_mota_ngan'] ?>122 <p>123 <?= $sanpham['sp_mota_chitiet'] ?>124 </p>125 </td>126 <td><?= $sanpham['sp_ngaycapnhat'] ?></td>127 <td><?= $sanpham['sp_soluong'] ?></td>128 <td><?= $sanpham['lsp_ten'] ?></td>129 <td><?= $sanpham['nsx_ten'] ?></td>130 <td><?= $sanpham['km_tomtat'] ?></td>131 <td>132 <!-- Nút sửa, bấm vào sẽ hiển thị form hiệu chỉnh thông tin dựa vào khóa chính `sp_ma` -->133 <a href="edit.php?sp_ma=<?= $sanpham['sp_ma'] ?>" class="btn btn-warning">134 Sửa135 </a>136 137 <!-- Nút xóa, bấm vào sẽ xóa thông tin dựa vào khóa chính `sp_ma` -->138 <a href="delete.php?sp_ma=<?= $sanpham['sp_ma'] ?>" class="btn btn-danger">139 Xóa140 </a>141 </td>142 </tr>143 <?php endforeach; ?>144 </tbody>145 </table>146 <!-- End block content -->147 </main>148 </div>149 </div>150 151 <!-- footer -->152 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>153 <!-- end footer -->154 155 <!-- Nhúng file quản lý phần SCRIPT JAVASCRIPT -->156 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>157 158 <!-- Các file Javascript sử dụng riêng cho trang này, liên kết tại đây -->159 <!-- <script src="..."></script> -->160</body>161 162</html>Step 2.2: bổ sung thư viên DataTables để tạo hiệu ứng phân trang
- Download thư viện
DataTable: https://datatables.net/ - Giải nén vào thư mục quản lý thư viện Frontend:
/assets/vendor/DataTables - Liên kết thư viện vào trang Danh sách, hiệu chỉnh file
/backend/functions/sanpham/index.php
php
1<!DOCTYPE html>2<html lang="en">3<head>4 <meta charset="UTF-8">5 <meta name="viewport" content="width=device-width, initial-scale=1.0">6 <meta http-equiv="X-UA-Compatible" content="ie=edge">7 <title>Danh sách Hình thức thanh toán</title>8 9 <?php include_once(__DIR__ . '/../../layouts/styles.php'); ?>10 11 <!-- DataTable CSS -->12 <link href="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.css" type="text/css" rel="stylesheet" />13 <link href="/duanweb-CP20SCF04/assets/vendor/DataTables/Buttons-1.6.3/css/buttons.bootstrap4.min.css" type="text/css" rel="stylesheet" />14</head>15<body>16 17 <!-- header -->18 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>19 <!-- end header -->20 21...22 23 <!-- footer -->24 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>25 <!-- end footer -->26 27 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>28 29 <!-- DataTable JS -->30 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.min.js"></script>31 32</body>33</html>- Viết code áp dụng DataTables vào danh sách của bạn.
- Lưu ý: cần viết theo đúng chuẩn của Table HTML, phải có thẻ
thead,tbody
php
1<!DOCTYPE html>2<html lang="en">3<head>4 <meta charset="UTF-8">5 <meta name="viewport" content="width=device-width, initial-scale=1.0">6 <meta http-equiv="X-UA-Compatible" content="ie=edge">7 <title>Danh sách Hình thức thanh toán</title>8 9 <?php include_once(__DIR__ . '/../../layouts/styles.php'); ?>10 11 <!-- DataTable CSS -->12 <link href="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.css" type="text/css" rel="stylesheet" />13</head>14<body>15 16 <!-- header -->17 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>18 <!-- end header -->19 20 21 <div class="container">22 <div class="row">23 <div class="col-md-4">24 <!-- sidebar -->25 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>26 <!-- end sidebar -->27 </div>28 <div class="col-md-8">29 <h1>Danh sách Sản phẩm</h1>30 ...31 32 <a href="create.php" class="btn btn-primary">Thêm mới</a>33 <table id="tblDanhsach" class="table table-bordered">34 <thead>35 <tr>36 <th>Mã sản phẩm</th>37 <th>Tên sản phẩm</th>38 <th>Giá sản phẩm</th>39 <th>Loại sản phẩm</th>40 <th>Nhà sản xuất</th>41 <th>Khuyến mãi</th>42 <th>hành động</th>43 </tr>44 </thead>45 <tbody>46 <?php foreach($data as $sp): ?>47 <tr>48 <td><?php echo $sp['sp_ma'] ?></td>49 <td><?= $sp['sp_ten'] ?></td>50 <td><?= $sp['sp_gia'] ?></td>51 <td><?= $sp['lsp_ten'] ?></td>52 <td><?= $sp['nsx_ten'] ?></td>53 54 <td><?= $sp['km_tomtat'] ?></td>55 56 <td>57 <a href="edit.php?sp_ma=<?php echo $sp['sp_ma'] ?>" class="btn btn-success">Sửa</a>58 <a href="delete.php?sp_ma=<?php echo $sp['sp_ma'] ?>" class="btn btn-danger">Xóa</a>59 </td>60 </tr>61 <?php endforeach; ?>62 </tbody>63 </table>64 </div>65 </div>66 </div>67 68 <!-- footer -->69 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>70 <!-- end footer -->71 72 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>73 74 <!-- DataTable JS -->75 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.min.js"></script>76 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/Buttons-1.6.3/js/buttons.bootstrap4.min.js"></script>77 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/pdfmake-0.1.36/pdfmake.min.js"></script>78 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/pdfmake-0.1.36/vfs_fonts.js"></script>79 <script>80 $('#tblDanhsach').DataTable({81 dom: 'Blfrtip',82 buttons: [83 'copy', 'excel', 'pdf'84 ]85 });86 </script>87</body>88</html>Step 3: tạo chức năng create dùng để hiển thị FORM thêm mới Sản phẩm
- Tạo file
/backend/functions/sanpham/create.phpđể xử lý logic/nghiệp vụ - Workflow của chức năng này:
- Cần phải
SELECTdữ liệu của các table cha liên quan (loaisanpham,nhasanxuat,khuyenmai) - Trình diễn các table cha này bằng
SELECT OPTIONtrong HTML
- Cần phải
- Nội dung file:
php
1<!-- Nhúng file cấu hình để xác định được Tên và Tiêu đề của trang hiện tại người dùng đang truy cập -->2<?php include_once(__DIR__ . '/../../layouts/config.php'); ?>3 4<!DOCTYPE html>5<html>6 7<head>8 <!-- Nhúng file quản lý phần HEAD -->9 <?php include_once(__DIR__ . '/../../layouts/head.php'); ?>10</head>11 12<body class="d-flex flex-column h-100">13 <!-- header -->14 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>15 <!-- end header -->16 17 <div class="container-fluid">18 <div class="row">19 <!-- sidebar -->20 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>21 <!-- end sidebar -->22 23 <main role="main" class="col-md-10 ml-sm-auto px-4 mb-2">24 <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">25 <h1 class="h2">Thêm mới</h1>26 </div>27 28 <!-- Block content -->29 <?php30 // Truy vấn database31 // 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn32 include_once(__DIR__ . '/../../../dbconnect.php');33 34 /* --- 35 --- 2.Truy vấn dữ liệu Loại sản phẩm 36 --- 37 */38 // Chuẩn bị câu truy vấn Loại sản phẩm39 $sqlLoaiSanPham = "select * from `loaisanpham`";40 41 // Thực thi câu truy vấn SQL để lấy về dữ liệu42 $resultLoaiSanPham = mysqli_query($conn, $sqlLoaiSanPham);43 44 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng45 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT46 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về47 $dataLoaiSanPham = [];48 while ($rowLoaiSanPham = mysqli_fetch_array($resultLoaiSanPham, MYSQLI_ASSOC)) {49 $dataLoaiSanPham[] = array(50 'lsp_ma' => $rowLoaiSanPham['lsp_ma'],51 'lsp_ten' => $rowLoaiSanPham['lsp_ten'],52 'lsp_mota' => $rowLoaiSanPham['lsp_mota'],53 );54 }55 /* --- End Truy vấn dữ liệu Loại sản phẩm --- */56 57 /* --- 58 --- 3. Truy vấn dữ liệu Nhà sản xuất 59 --- 60 */61 // Chuẩn bị câu truy vấn Nhà sản xuất62 $sqlNhaSanXuat = "select * from `nhasanxuat`";63 64 // Thực thi câu truy vấn SQL để lấy về dữ liệu65 $resultNhaSanXuat = mysqli_query($conn, $sqlNhaSanXuat);66 67 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng68 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT69 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về70 $dataNhaSanXuat = [];71 while ($rowNhaSanXuat = mysqli_fetch_array($resultNhaSanXuat, MYSQLI_ASSOC)) {72 $dataNhaSanXuat[] = array(73 'nsx_ma' => $rowNhaSanXuat['nsx_ma'],74 'nsx_ten' => $rowNhaSanXuat['nsx_ten'],75 );76 }77 /* --- End Truy vấn dữ liệu Nhà sản xuất --- */78 79 /* --- 80 --- 4. Truy vấn dữ liệu Khuyến mãi81 --- 82 */83 // Chuẩn bị câu truy vấn Khuyến mãi84 $sqlKhuyenMai = "select * from `khuyenmai`";85 86 // Thực thi câu truy vấn SQL để lấy về dữ liệu87 $resultKhuyenMai = mysqli_query($conn, $sqlKhuyenMai);88 89 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng90 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT91 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về92 $dataKhuyenMai = [];93 while ($rowKhuyenMai = mysqli_fetch_array($resultKhuyenMai, MYSQLI_ASSOC)) {94 $km_tomtat = '';95 if (!empty($rowKhuyenMai['km_ten'])) {96 // Sử dụng hàm sprintf() để chuẩn bị mẫu câu với các giá trị truyền vào tương ứng từng vị trí placeholder97 $km_tomtat = sprintf(98 "Khuyến mãi %s, nội dung: %s, thời gian: %s-%s",99 $rowKhuyenMai['km_ten'],100 $rowKhuyenMai['km_noidung'],101 // Sử dụng hàm date($format, $timestamp) để chuyển đổi ngày thành định dạng Việt Nam (ngày/tháng/năm)102 // Do hàm date() nhận vào là đối tượng thời gian, chúng ta cần sử dụng hàm strtotime() để chuyển đổi từ chuỗi có định dạng 'yyyy-mm-dd' trong MYSQL thành đối tượng ngày tháng103 date('d/m/Y', strtotime($rowKhuyenMai['km_tungay'])), //vd: '2019-04-25'104 date('d/m/Y', strtotime($rowKhuyenMai['km_denngay']))105 ); //vd: '2019-05-10'106 }107 $dataKhuyenMai[] = array(108 'km_ma' => $rowKhuyenMai['km_ma'],109 'km_tomtat' => $km_tomtat,110 );111 }112 /* --- End Truy vấn dữ liệu Khuyến mãi --- */113 ?>114 115 <form name="frmsanpham" id="frmsanpham" method="post" action="">116 <div class="form-group">117 <label for="sp_ten">Tên Sản phẩm</label>118 <input type="text" class="form-control" id="sp_ten" name="sp_ten" placeholder="Tên Sản phẩm" value="">119 </div>120 <div class="form-group">121 <label for="sp_gia">Giá Sản phẩm</label>122 <input type="text" class="form-control" id="sp_gia" name="sp_gia" placeholder="Giá Sản phẩm" value="">123 </div>124 <div class="form-group">125 <label for="sp_giacu">Giá cũ Sản phẩm</label>126 <input type="text" class="form-control" id="sp_giacu" name="sp_giacu" placeholder="Giá cũ Sản phẩm" value="">127 </div>128 <div class="form-group">129 <label for="sp_mota_ngan">Mô tả ngắn</label>130 <input type="text" class="form-control" id="sp_mota_ngan" name="sp_mota_ngan" placeholder="Mô tả ngắn Sản phẩm" value="">131 </div>132 <div class="form-group">133 <label for="sp_mota_chitiet">Mô tả chi tiết</label>134 <input type="text" class="form-control" id="sp_mota_chitiet" name="sp_mota_chitiet" placeholder="Mô tả chi tiết Sản phẩm" value="">135 </div>136 <div class="form-group">137 <label for="sp_ngaycapnhat">Ngày cập nhật</label>138 <input type="text" class="form-control" id="sp_ngaycapnhat" name="sp_ngaycapnhat" placeholder="Ngày cập nhật Sản phẩm" value="">139 </div>140 <div class="form-group">141 <label for="sp_soluong">Số lượng</label>142 <input type="text" class="form-control" id="sp_soluong" name="sp_soluong" placeholder="Số lượng Sản phẩm" value="">143 </div>144 <div class="form-group">145 <label for="lsp_ma">Loại sản phẩm</label>146 <select class="form-control" id="lsp_ma" name="lsp_ma">147 <?php foreach ($dataLoaiSanPham as $loaisanpham) : ?>148 <option value="<?= $loaisanpham['lsp_ma'] ?>"><?= $loaisanpham['lsp_ten'] ?></option>149 <?php endforeach; ?>150 </select>151 </div>152 <div class="form-group">153 <label for="nsx_ma">Nhà sản xuất</label>154 <select class="form-control" id="nsx_ma" name="nsx_ma">155 <?php foreach ($dataNhaSanXuat as $nhasanxuat) : ?>156 <option value="<?= $nhasanxuat['nsx_ma'] ?>"><?= $nhasanxuat['nsx_ten'] ?></option>157 <?php endforeach; ?>158 </select>159 </div>160 <div class="form-group">161 <label for="km_ma">Khuyến mãi</label>162 <select class="form-control" id="km_ma" name="km_ma">163 <option value="">Không áp dụng khuyến mãi</option>164 <?php foreach ($dataKhuyenMai as $khuyenmai) : ?>165 <option value="<?= $khuyenmai['km_ma'] ?>"><?= $khuyenmai['km_tomtat'] ?></option>166 <?php endforeach; ?>167 </select>168 </div>169 <button class="btn btn-primary" name="btnSave">Lưu dữ liệu</button>170 </form>171 172 <?php173 // 2. Nếu người dùng có bấm nút Đăng ký thì thực thi câu lệnh UPDATE174 if (isset($_POST['btnSave'])) {175 // Lấy dữ liệu người dùng hiệu chỉnh gởi từ REQUEST POST176 $ten = $_POST['sp_ten'];177 $gia = $_POST['sp_gia'];178 $giacu = $_POST['sp_giacu'];179 $motangan = $_POST['sp_mota_ngan'];180 $motachitiet = $_POST['sp_mota_chitiet'];181 $ngaycapnhat = $_POST['sp_ngaycapnhat'];182 $soluong = $_POST['sp_soluong'];183 $lsp_ma = $_POST['lsp_ma'];184 $nsx_ma = $_POST['nsx_ma'];185 $km_ma = (empty($_POST['km_ma']) ? 'NULL' : $_POST['km_ma']);186 187 // Câu lệnh INSERT188 $sql = "INSERT INTO `sanpham` (sp_ten, sp_gia, sp_giacu, sp_mota_ngan, sp_mota_chitiet, sp_ngaycapnhat, sp_soluong, lsp_ma, nsx_ma, km_ma) VALUES ('$ten', $gia, $giacu, '$motangan', '$motachitiet', '$ngaycapnhat', $soluong, $lsp_ma, $nsx_ma, $km_ma);";189 190 // Thực thi INSERT191 mysqli_query($conn, $sql);192 193 // Đóng kết nối194 mysqli_close($conn);195 196 // Sau khi cập nhật dữ liệu, tự động điều hướng về trang Danh sách197 echo "<script>location.href = 'index.php';</script>";198 }199 ?>200 <!-- End block content -->201 </main>202 </div>203 </div>204 205 <!-- footer -->206 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>207 <!-- end footer -->208 209 <!-- Nhúng file quản lý phần SCRIPT JAVASCRIPT -->210 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>211 212 <!-- Các file Javascript sử dụng riêng cho trang này, liên kết tại đây -->213 <!-- <script src="..."></script> -->214</body>215 216</html>Step 4: tạo chức năng edit dùng để hiển thị FORM hiệu chỉnh Sản phẩm
- Tạo file
/backend/functions/sanpham/edit.phpđể xử lý logic/nghiệp vụ - Workflow của chức năng này:
- Cần phải
SELECTdữ liệu của các table cha liên quan (loaisanpham,nhasanxuat,khuyenmai) - Trình diễn các table cha này bằng
SELECT OPTIONtrong HTML - Cần phải
SELECT dữ liệu của dòng cần hiệu chỉnh (dựa theo ID) - Lắp đầy các trường
field/controltrong FORM bằng dữ liệu cũ
- Cần phải
- Nội dung file:
php
1<!-- Nhúng file cấu hình để xác định được Tên và Tiêu đề của trang hiện tại người dùng đang truy cập -->2<?php include_once(__DIR__ . '/../../layouts/config.php'); ?>3 4<!DOCTYPE html>5<html>6 7<head>8 <!-- Nhúng file quản lý phần HEAD -->9 <?php include_once(__DIR__ . '/../../layouts/head.php'); ?>10</head>11 12<body class="d-flex flex-column h-100">13 <!-- header -->14 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>15 <!-- end header -->16 17 <div class="container-fluid">18 <div class="row">19 <!-- sidebar -->20 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>21 <!-- end sidebar -->22 23 <main role="main" class="col-md-10 ml-sm-auto px-4 mb-2">24 <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">25 <h1 class="h2">Hiệu chỉnh</h1>26 </div>27 28 <!-- Block content -->29 <?php30 // Truy vấn database31 // 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn32 include_once(__DIR__ . '/../../../dbconnect.php');33 34 /* --- 35 --- 2.Truy vấn dữ liệu Loại sản phẩm 36 --- 37 */38 // Chuẩn bị câu truy vấn Loại sản phẩm39 $sqlLoaiSanPham = "select * from `loaisanpham`";40 41 // Thực thi câu truy vấn SQL để lấy về dữ liệu42 $resultLoaiSanPham = mysqli_query($conn, $sqlLoaiSanPham);43 44 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng45 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT46 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về47 $dataLoaiSanPham = [];48 while ($rowLoaiSanPham = mysqli_fetch_array($resultLoaiSanPham, MYSQLI_ASSOC)) {49 $dataLoaiSanPham[] = array(50 'lsp_ma' => $rowLoaiSanPham['lsp_ma'],51 'lsp_ten' => $rowLoaiSanPham['lsp_ten'],52 'lsp_mota' => $rowLoaiSanPham['lsp_mota'],53 );54 }55 /* --- End Truy vấn dữ liệu Loại sản phẩm --- */56 57 /* --- 58 --- 3. Truy vấn dữ liệu Nhà sản xuất 59 --- 60 */61 // Chuẩn bị câu truy vấn Nhà sản xuất62 $sqlNhaSanXuat = "select * from `nhasanxuat`";63 64 // Thực thi câu truy vấn SQL để lấy về dữ liệu65 $resultNhaSanXuat = mysqli_query($conn, $sqlNhaSanXuat);66 67 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng68 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT69 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về70 $dataNhaSanXuat = [];71 while ($rowNhaSanXuat = mysqli_fetch_array($resultNhaSanXuat, MYSQLI_ASSOC)) {72 $dataNhaSanXuat[] = array(73 'nsx_ma' => $rowNhaSanXuat['nsx_ma'],74 'nsx_ten' => $rowNhaSanXuat['nsx_ten'],75 );76 }77 /* --- End Truy vấn dữ liệu Nhà sản xuất --- */78 79 /* --- 80 --- 4. Truy vấn dữ liệu Khuyến mãi81 --- 82 */83 // Chuẩn bị câu truy vấn Khuyến mãi84 $sqlKhuyenMai = "select * from `khuyenmai`";85 86 // Thực thi câu truy vấn SQL để lấy về dữ liệu87 $resultKhuyenMai = mysqli_query($conn, $sqlKhuyenMai);88 89 // Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng90 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT91 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về92 $dataKhuyenMai = [];93 while ($rowKhuyenMai = mysqli_fetch_array($resultKhuyenMai, MYSQLI_ASSOC)) {94 $km_tomtat = '';95 if (!empty($rowKhuyenMai['km_ten'])) {96 // Sử dụng hàm sprintf() để chuẩn bị mẫu câu với các giá trị truyền vào tương ứng từng vị trí placeholder97 $km_tomtat = sprintf(98 "Khuyến mãi %s, nội dung: %s, thời gian: %s-%s",99 $rowKhuyenMai['km_ten'],100 $rowKhuyenMai['km_noidung'],101 // Sử dụng hàm date($format, $timestamp) để chuyển đổi ngày thành định dạng Việt Nam (ngày/tháng/năm)102 // Do hàm date() nhận vào là đối tượng thời gian, chúng ta cần sử dụng hàm strtotime() để chuyển đổi từ chuỗi có định dạng 'yyyy-mm-dd' trong MYSQL thành đối tượng ngày tháng103 date('d/m/Y', strtotime($rowKhuyenMai['km_tungay'])), //vd: '2019-04-25'104 date('d/m/Y', strtotime($rowKhuyenMai['km_denngay']))105 ); //vd: '2019-05-10'106 }107 $dataKhuyenMai[] = array(108 'km_ma' => $rowKhuyenMai['km_ma'],109 'km_tomtat' => $km_tomtat,110 );111 }112 /* --- End Truy vấn dữ liệu Khuyến mãi --- */113 114 /* --- 115 --- 5. Truy vấn dữ liệu Sản phẩm theo khóa chính116 --- 117 */118 // Chuẩn bị câu truy vấn $sqlSelect, lấy dữ liệu ban đầu của record cần update119 // Lấy giá trị khóa chính được truyền theo dạng QueryString Parameter key1=value1&key2=value2...120 $sp_ma = $_GET['sp_ma'];121 $sqlSelect = "SELECT * FROM `sanpham` WHERE sp_ma=$sp_ma;";122 123 // Thực thi câu truy vấn SQL để lấy về dữ liệu ban đầu của record cần update124 $resultSelect = mysqli_query($conn, $sqlSelect);125 $sanphamRow = mysqli_fetch_array($resultSelect, MYSQLI_ASSOC); // 1 record126 /* --- End Truy vấn dữ liệu Sản phẩm theo khóa chính --- */127 ?>128 129 <form name="frmsanpham" id="frmsanpham" method="post" action="edit.php?sp_ma=<?= $sanphamRow['sp_ma'] ?>">130 <div class="form-group">131 <label for="sp_ma">Mã Sản phẩm</label>132 <input type="text" class="form-control" id="sp_ma" name="sp_ma" placeholder="Mã Sản phẩm" readonly value="<?= $sanphamRow['sp_ma'] ?>">133 <small id="sp_maHelp" class="form-text text-muted">Mã Sản phẩm không được hiệu chỉnh.</small>134 </div>135 <div class="form-group">136 <label for="sp_ten">Tên Sản phẩm</label>137 <input type="text" class="form-control" id="sp_ten" name="sp_ten" placeholder="Tên Sản phẩm" value="<?= $sanphamRow['sp_ten'] ?>">138 </div>139 <div class="form-group">140 <label for="sp_gia">Giá Sản phẩm</label>141 <input type="text" class="form-control" id="sp_gia" name="sp_gia" placeholder="Giá Sản phẩm" value="<?= $sanphamRow['sp_gia'] ?>">142 </div>143 <div class="form-group">144 <label for="sp_giacu">Giá cũ Sản phẩm</label>145 <input type="text" class="form-control" id="sp_giacu" name="sp_giacu" placeholder="Giá cũ Sản phẩm" value="<?= $sanphamRow['sp_giacu'] ?>">146 </div>147 <div class="form-group">148 <label for="sp_mota_ngan">Mô tả ngắn</label>149 <input type="text" class="form-control" id="sp_mota_ngan" name="sp_mota_ngan" placeholder="Mô tả ngắn Sản phẩm" value="<?= $sanphamRow['sp_mota_ngan'] ?>">150 </div>151 <div class="form-group">152 <label for="sp_mota_chitiet">Mô tả chi tiết</label>153 <input type="text" class="form-control" id="sp_mota_chitiet" name="sp_mota_chitiet" placeholder="Mô tả chi tiết Sản phẩm" value="<?= $sanphamRow['sp_mota_chitiet'] ?>">154 </div>155 <div class="form-group">156 <label for="sp_ngaycapnhat">Ngày cập nhật</label>157 <input type="text" class="form-control" id="sp_ngaycapnhat" name="sp_ngaycapnhat" placeholder="Ngày cập nhật Sản phẩm" value="<?= $sanphamRow['sp_ngaycapnhat'] ?>">158 </div>159 <div class="form-group">160 <label for="sp_soluong">Số lượng</label>161 <input type="text" class="form-control" id="sp_soluong" name="sp_soluong" placeholder="Số lượng Sản phẩm" value="<?= $sanphamRow['sp_soluong'] ?>">162 </div>163 <div class="form-group">164 <label for="lsp_ma">Loại sản phẩm</label>165 <select class="form-control" id="lsp_ma" name="lsp_ma">166 <?php foreach ($dataLoaiSanPham as $loaisanpham) : ?>167 <?php if ($loaisanpham['lsp_ma'] == $sanphamRow['lsp_ma']) : ?>168 <option value="<?= $loaisanpham['lsp_ma'] ?>" selected><?= $loaisanpham['lsp_ten'] ?></option>169 <?php else : ?>170 <option value="<?= $loaisanpham['lsp_ma'] ?>"><?= $loaisanpham['lsp_ten'] ?></option>171 <?php endif; ?>172 <?php endforeach; ?>173 </select>174 </div>175 <div class="form-group">176 <label for="nsx_ma">Nhà sản xuất</label>177 <select class="form-control" id="nsx_ma" name="nsx_ma">178 <?php foreach ($dataNhaSanXuat as $nhasanxuat) : ?>179 <?php if ($nhasanxuat['nsx_ma'] == $sanphamRow['nsx_ma']) : ?>180 <option value="<?= $nhasanxuat['nsx_ma'] ?>" selected><?= $nhasanxuat['nsx_ten'] ?></option>181 <?php else : ?>182 <option value="<?= $nhasanxuat['nsx_ma'] ?>"><?= $nhasanxuat['nsx_ten'] ?></option>183 <?php endif; ?>184 <?php endforeach; ?>185 </select>186 </div>187 <div class="form-group">188 <label for="km_ma">Khuyến mãi</label>189 <select class="form-control" id="km_ma" name="km_ma">190 <option value="">Không áp dụng khuyến mãi</option>191 <?php foreach ($dataKhuyenMai as $khuyenmai) : ?>192 <?php if ($khuyenmai['km_ma'] == $sanphamRow['km_ma']) : ?>193 <option value="<?= $khuyenmai['km_ma'] ?>" selected><?= $khuyenmai['km_tomtat'] ?></option>194 <?php else : ?>195 <option value="<?= $khuyenmai['km_ma'] ?>"><?= $khuyenmai['km_tomtat'] ?></option>196 <?php endif; ?>197 <?php endforeach; ?>198 </select>199 </div>200 <button class="btn btn-primary" name="btnSave">Cập nhật</button>201 </form>202 203 <?php204 // 2. Nếu người dùng có bấm nút Đăng ký thì thực thi câu lệnh UPDATE205 if (isset($_POST['btnSave'])) {206 // Lấy dữ liệu người dùng hiệu chỉnh gởi từ REQUEST POST207 $ten = $_POST['sp_ten'];208 $gia = $_POST['sp_gia'];209 $giacu = $_POST['sp_giacu'];210 $motangan = $_POST['sp_mota_ngan'];211 $motachitiet = $_POST['sp_mota_chitiet'];212 $ngaycapnhat = $_POST['sp_ngaycapnhat'];213 $soluong = $_POST['sp_soluong'];214 $lsp_ma = $_POST['lsp_ma'];215 $nsx_ma = $_POST['nsx_ma'];216 $km_ma = empty($_POST['km_ma']) ? 'NULL' : $_POST['km_ma'];217 218 // Câu lệnh INSERT219 $sql = "UPDATE `sanpham` SET sp_ten='$ten', sp_gia=$gia, sp_giacu=$giacu, sp_mota_ngan='$motangan', sp_mota_chitiet='$motachitiet', sp_ngaycapnhat='$ngaycapnhat', sp_soluong=$soluong, lsp_ma=$lsp_ma, nsx_ma=$nsx_ma, km_ma=$km_ma WHERE sp_ma=$sp_ma;";220 221 // Thực thi INSERT222 mysqli_query($conn, $sql);223 224 // Đóng kết nối225 mysqli_close($conn);226 227 // Sau khi cập nhật dữ liệu, tự động điều hướng về trang Danh sách228 echo "<script>location.href = 'index.php';</script>";229 }230 ?>231 <!-- End block content -->232 </main>233 </div>234 </div>235 236 <!-- footer -->237 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>238 <!-- end footer -->239 240 <!-- Nhúng file quản lý phần SCRIPT JAVASCRIPT -->241 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>242 243 <!-- Các file Javascript sử dụng riêng cho trang này, liên kết tại đây -->244 <!-- <script src="..."></script> -->245</body>246 247</html>Step 5: tạo chức năng delete dùng để xóa Sản phẩm
- Tạo file
/backend/functions/sanpham/delete.phpđể xử lý logic/nghiệp vụ - Workflow của chức năng này:
- Cần lấy
IdtheoRequest GETđược truyền tới từ chức năng Danh sách (index) - Thực thi câu lệnh
DELETEđể xóa dữ liệu
- Cần lấy
- Nội dung file:
php
1<?php2// Truy vấn database3// 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn4include_once(__DIR__.'/../../../dbconnect.php');5 6// 2. Chuẩn bị câu truy vấn $sql7// Lấy giá trị khóa chính được truyền theo dạng QueryString Parameter key1=value1&key2=value2...8$sp_ma = $_GET['sp_ma'];9$sql = "DELETE FROM `sanpham` WHERE sp_ma=" . $sp_ma;10 11// 3. Thực thi câu lệnh DELETE12$result = mysqli_query($conn, $sql);13 14// 4. Đóng kết nối15mysqli_close($conn);16 17// Sau khi cập nhật dữ liệu, tự động điều hướng về trang Danh sách18header('location:index.php');Step 5.2: bổ sung popup cảnh báo khi xóa, sử dụng thư viện SweetAlert
- Download thư viện: https://sweetalert.js.org/guides/
- Copy vào thư mục:
/assets/vendor/sweetalert/sweetalert.min.js - Liên kết Thư viện JS trong chức năng Danh sách. Hiệu chỉnh file
/backend/functions/sanpham/index.php
php
1<!-- SweetAlert -->2<script src="/duanweb-CP20SCF04/assets/vendor/sweetalert/sweetalert.min.js"></script>Step 5.2: xử lý cảnh báo khi xóa
- Đặt các nút xóa áp dụng chung 1 định dạng class, ví dụ
.btnDelete - Bổ sung thuộc tính
custom attribute HTMLtên làdata-sp_ma="gia tri..." - Đăng ký sự kiện click cho các nút xóa đang áp dụng class
.btnDelete - Sử dụng SweetAlert để hiển thị popup cảnh báo
php
1<!DOCTYPE html>2<html lang="en">3<head>4 <meta charset="UTF-8">5 <meta name="viewport" content="width=device-width, initial-scale=1.0">6 <meta http-equiv="X-UA-Compatible" content="ie=edge">7 <title>Danh sách Hình thức thanh toán</title>8 9 <?php include_once(__DIR__ . '/../../layouts/styles.php'); ?>10 11 <!-- DataTable CSS -->12 <link href="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.css" type="text/css" rel="stylesheet" />13 <link href="/duanweb-CP20SCF04/assets/vendor/DataTables/Buttons-1.6.3/css/buttons.bootstrap4.min.css" type="text/css" rel="stylesheet" />14</head>15<body>16 17 <!-- header -->18 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>19 <!-- end header -->20 21 22 <div class="container-fluid">23 <div class="row">24 <div class="col-md-4">25 <!-- sidebar -->26 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>27 <!-- end sidebar -->28 </div>29 <div class="col-md-8">30 <h1>Danh sách Sản phẩm</h1>31 <?php32 // Truy vấn database để lấy danh sách33 // 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn34 // C:\xampp\htdocs\web02\35 include_once(__DIR__ . '/../../../dbconnect.php');36 37 // 2. Chuẩn bị QUERY38 // HERE DOC39 $sql = <<<EOT40 SELECT sp.*41 , lsp.lsp_ten42 , nsx.nsx_ten43 , km.km_ten, km.km_noidung, km.km_tungay, km.km_denngay44 FROM `sanpham` sp45 JOIN `loaisanpham` lsp ON sp.lsp_ma = lsp.lsp_ma46 JOIN `nhasanxuat` nsx ON sp.nsx_ma = nsx.nsx_ma47 LEFT JOIN `khuyenmai` km ON sp.km_ma = km.km_ma48 ORDER BY sp.sp_ma DESC49EOT;50 51 // 3. Yêu cầu PHP thực thi QUERY52 $result = mysqli_query($conn, $sql);53 54 // 4. Khi thực thi các truy vấn dạng SELECT, dữ liệu lấy về cần phải phân tích để sử dụng55 // Thông thường, chúng ta sẽ sử dụng vòng lặp while để duyệt danh sách các dòng dữ liệu được SELECT56 // Ta sẽ tạo 1 mảng array để chứa các dữ liệu được trả về57 $data = [];58 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {59 60 $km_ten = $row['km_ten'];61 $km_noidung = $row['km_noidung'];62 63 // Sử dụng hàm date($format, $timestamp) để chuyển đổi ngày thành định dạng Việt Nam (ngày/tháng/năm)64 // Do hàm date() nhận vào là đối tượng thời gian, chúng ta cần sử dụng hàm strtotime() 65 // để chuyển đổi từ chuỗi có định dạng 'yyyy-mm-dd' trong MYSQL thành đối tượng ngày tháng66 $km_tungay = date('d/m/Y', strtotime($row['km_tungay'])); //vd: '2019-04-25'67 $km_denngay = date('d/m/Y', strtotime($row['km_denngay'])); //vd: '2019-04-25'68 69 $km_tomtat = '';70 if( !empty($km_ten) ) {71 $km_tomtat = sprintf("Khuyến mãi %s, nội dung: %s, thời gian: %s - %s",72 $km_ten,73 $km_noidung,74 $km_tungay,75 $km_denngay);76 }77 78 79 $data[] = array(80 'sp_ma' => $row['sp_ma'],81 'sp_ten' => $row['sp_ten'],82 // Sử dụng hàm number_format(số tiền, số lẻ thập phân, dấu phân cách số lẻ, dấu phân cách hàng nghìn) 83 // để định dạng số khi hiển thị trên giao diện. 84 // Vd: 15800000 -> format thành 15,800,000.66 vnđ85 'sp_gia' => number_format($row['sp_gia'], 0, ".", ",") . ' vnđ',86 87 'lsp_ten' => $row['lsp_ten'],88 'nsx_ten' => $row['nsx_ten'],89 90 'km_tomtat' => $km_tomtat91 );92 }93 94 // print_r($data);95 ?>96 97 <a href="create.php" class="btn btn-primary">Thêm mới</a>98 <table id="tblDanhsach" class="table table-bordered">99 <thead>100 <tr>101 <th>Mã sản phẩm</th>102 <th>Tên sản phẩm</th>103 <th>Giá sản phẩm</th>104 <th>Loại sản phẩm</th>105 <th>Nhà sản xuất</th>106 <th>Khuyến mãi</th>107 <th>hành động</th>108 </tr>109 </thead>110 <tbody>111 <?php foreach($data as $sp): ?>112 <tr>113 <td><?php echo $sp['sp_ma'] ?></td>114 <td><?= $sp['sp_ten'] ?></td>115 <td><?= $sp['sp_gia'] ?></td>116 <td><?= $sp['lsp_ten'] ?></td>117 <td><?= $sp['nsx_ten'] ?></td>118 119 <td><?= $sp['km_tomtat'] ?></td>120 121 <td>122 <a href="edit.php?sp_ma=<?php echo $sp['sp_ma'] ?>" class="btn btn-success">Sửa</a>123 <button class="btn btn-danger btnDelete" data-sp_ma="<?= $sp['sp_ma'] ?>">Xóa</button>124 </td>125 </tr>126 <?php endforeach; ?>127 </tbody>128 </table>129 </div>130 </div>131 </div>132 133 <!-- footer -->134 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>135 <!-- end footer -->136 137 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>138 139 <!-- DataTable JS -->140 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/datatables.min.js"></script>141 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/Buttons-1.6.3/js/buttons.bootstrap4.min.js"></script>142 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/pdfmake-0.1.36/pdfmake.min.js"></script>143 <script src="/duanweb-CP20SCF04/assets/vendor/DataTables/pdfmake-0.1.36/vfs_fonts.js"></script>144 145 <!-- SweetAlert -->146 <script src="/duanweb-CP20SCF04/assets/vendor/sweetalert/sweetalert.min.js"></script>147 <script>148 $(document).ready(function() {149 // xử lý table danh sách150 $('#tblDanhsach').DataTable({151 dom: 'Blfrtip',152 buttons: [153 'copy', 'excel', 'pdf'154 ]155 });156 157 // Cảnh báo khi xóa158 // 1. Đăng ký sự kiện click cho các phần tử (element) đang áp dụng class .btnDelete159 $('.btnDelete').click(function() {160 // Click hanlder161 // Hiện cảnh báo khi bấm nút xóa162 swal({163 title: "Bạn có chắc chắn muốn xóa?",164 text: "Một khi đã xóa, không thể phục hồi....",165 icon: "warning",166 buttons: true,167 dangerMode: true,168 })169 .then((willDelete) => {170 debugger;171 if (willDelete) { // Nếu đồng ý xóa172 173 // 2. Lấy giá trị của thuộc tính (custom attribute HTML) 'sp_ma'174 // var sp_ma = $(this).attr('data-sp_ma');175 var sp_ma = $(this).data('sp_ma');176 var url = "delete.php?sp_ma=" + sp_ma;177 178 // Điều hướng qua trang xóa với REQUEST GET, có tham số sp_ma=...179 location.href = url;180 181 } else {182 swal("Cẩn thận hơn nhé!");183 }184 });185 186 187 188 });189 });190 </script>191</body>192</html>Step 6: bổ sung menu chức năng Quản lý Sản phẩm bên sidebar
- Hiệu chỉnh file
/backend/layouts/partials/sidebar.php - Nội dung file:
php
1<nav class="col-md-2 d-none d-md-block sidebar">2 <div class="sidebar-sticky">3 <ul class="nav flex-column">4 <!-- #################### Menu các trang Quản lý #################### -->5 <li class="nav-item sidebar-heading"><span>Quản lý</span></li>6 <li class="nav-item">7 <a href="/backend/pages/dashboard.php">Bảng tin <span class="sr-only">(current)</span></a>8 </li>9 <hr style="border: 1px solid red; width: 80%;" />10 <!-- #################### End Menu các trang Quản lý #################### -->11 12 <!-- #################### Menu chức năng Danh mục #################### -->13 <li class="nav-item sidebar-heading">14 <span>Danh mục</span>15 </li>16 <!-- Menu Loại sản phẩm -->17 <li class="nav-item">18 <a href="#loaisanphamSubMenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">19 Loại sản phẩm20 </a>21 <ul class="collapse" id="loaisanphamSubMenu">22 <li class="nav-item">23 <a href="/backend/functions/loaisanpham/index.php">Danh sách</a>24 </li>25 <li class="nav-item">26 <a href="/backend/functions/loaisanpham/create.php">Thêm mới</a>27 </li>28 </ul>29 </li>30 <!-- End Loại sản phẩm -->31 32 <!-- Menu Sản phẩm -->33 <li class="nav-item">34 <a href="#sanphamSubMenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">35 Sản phẩm36 </a>37 <ul class="collapse" id="sanphamSubMenu">38 <li class="nav-item">39 <a href="/backend/functions/sanpham/index.php">Danh sách</a>40 </li>41 <li class="nav-item">42 <a href="/backend/functions/sanpham/create.php">Thêm mới</a>43 </li>44 </ul>45 </li>46 <!-- End Sản phẩm -->47 48 <!-- #################### End Menu chức năng Danh mục #################### -->49 </ul>50 </div>51</nav>
