By GokiSoft.com|
20:31 11/03/2022|
Học PHP
[Video] Tìm hiểu Class Object trong PHP/MySQL - C2108L
#add.php
<?php
require_once('product.php');
if(!empty($_POST)) {
$p = new Product();
$p->processForm();
$p->insert();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add Product Page</title>
<!-- Bootstrap -> thiet ke GUI -->
<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.5.1/jquery.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label>Title</label>
<input required type="text" name="title" class="form-control">
</div>
<div class="form-group">
<label>Thumbnail</label>
<input required type="text" name="thumbnail" class="form-control">
</div>
<div class="form-group">
<label>Price</label>
<input required type="num" name="price" class="form-control">
</div>
<div class="form-group">
<label>Content</label>
<input required type="text" name="content" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Save</button>
</div>
</form>
</div>
</body>
</html>
#config.php
<?php
define('HOST', 'localhost');
define('DATABASE', 'BT2290');
define('USERNAME', 'root');
define('PASSWORD', '');
#dbhelper.php
<?php
require_once('config.php');
//create database
function initDB() {
// Mo ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
mysqli_set_charset($conn, 'utf8');
// Thuc hien query -> insert du lieu vao database
// $query = "insert into Users(fullname, email, password, address) values ('$fullname', '$email', '$pwd', '$address')";
// echo $query;
mysqli_query($conn, SQL_CREATE_DATABASE);
// Dong ket noi CSDL
mysqli_close($conn);
}
/**
* Ham nay se su dung cho TH cau truy van: insert, update, delete
*/
function execute($query) {
// Mo ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// Thuc hien query -> insert du lieu vao database
// $query = "insert into Users(fullname, email, password, address) values ('$fullname', '$email', '$pwd', '$address')";
// echo $query;
mysqli_query($conn, $query);
// Dong ket noi CSDL
mysqli_close($conn);
}
function executeResult($query, $isSingle = false) {
// Mo ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// Thuc hien query -> insert du lieu vao database
// $query = "insert into Users(fullname, email, password, address) values ('$fullname', '$email', '$pwd', '$address')";
// echo $query;
// $query = "select * from Users where email = '$email' and password = '$pwd'";
$resultset = mysqli_query($conn, $query);
if($isSingle) {
$data = mysqli_fetch_array($resultset, 1);
} else {
$data = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
}
// Dong ket noi CSDL
mysqli_close($conn);
return $data;
}
#product.php
<?php
require_once('utility.php');
require_once('dbhelper.php');
class Product {
//Khai bao cac thuoc tinh
public $id;
public $title;
public $thumbnail;
public $price;
public $content;
public $createdAt;
public $updatedAt;
public function processForm() {
//Doc du lieu tu $_POST -> cai dat len thuoc tinh
$this->title = getPost('title');
$this->thumbnail = getPost('thumbnail');
$this->price = getPost('price');
$this->content = getPost('content');
$this->createdAt = date('Y-m-d H:i:s');
$this->updatedAt = date('Y-m-d H:i:s');
}
public function insert() {
$sql = "insert into products(title, price, thumbnail, content, created_at, updated_at) values ('".$this->title."', '".$this->price."', '".$this->thumbnail."', '".$this->content."', '$".$this->createdAt."', '".$this->updatedAt."')";
execute($sql);
}
public function dumpData() {
//Hien thi noi dung du lieu ra
echo '<br/>Test chuc nang trong Class Object';
echo '<br/>'.$this->id.'-'.$this->title.'-'.$this->content;
}
}
#readme.txt
Nội dung kiến thức học:
- Class Object trong lập trình PHP
Phân tích:
Quản lý thông tin sản phẩm:
- Thuộc tính:
- id: primary key
- title
- thumbnail
- price
- content
- createdAt
- updatedAt
- Phương thức (method) | hàm (function) | hành động -> liên quan tới sản phầm này
- Đọc dữ liệu gửi từ form lên -> thiết lập giá tri cho thuộc tính
- Thêm/sửa/xóa -> dữ liệu -> vào database
...
Bài toán đặt ra: có cách nào -> nhóm thành phần trên -> lại -> làm sao cho dễ sử dụng & quản lý không
- Giải pháp 1: file -> sử dụng function (procedure) -> ???
- Class Object -> Quản lý đc nó
#utility.php
<?php
function getPost($key, $slash = '\'') {
$value = '';
if(isset($_POST[$key])) {
$value = $_POST[$key];
// $value = Sinh Vien ' ABC ' => Sinh Vien \' ABC \'
// \\ -> \
// \' -> '
// \' (Hieu la: ') -> \\\' (Hieu la: \')
$value = str_replace($slash, "\\".$slash, $value);
}
return $value;
}
#vidu.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OOP: Su dung Class Object trong PHP</title>
</head>
<body>
<?php
require_once('product.php');
$p = new Product(); //Ham tao -> constructor
$p->id = 1;
$p->title = 'San pham 1';
$p->thumbnail = 'Link hinh anh';
$p->price = 2000;
$p->content = 'Noi dung bai viet';
$p->createdAt = date('Y-m-d H:i:s');
$p->updatedAt = date('Y-m-d H:i:s');
echo $p->title;
$p->dumpData();
?>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)