TRANG CHỦ (PHẦN 18)
Kể từ phần này tôi sẽ hướng dẫn các bạn thực hiện các trang người dùng (Front).
Tại thư mục gốc (Root), các bạn tạo file index.php để viết mã lệnh xử lý trang chủ.
Nội dung file index.php như sau:
PHP Code:
<?php
//Require các file cần thiết
require 'configs/Config.php';
require 'models/Product.php';
//Khởi tạo đối tượng sản phẩm (Product)
$productModel = new Product();
//Lấy danh sách 6 sản phẩm mới nhất
$productLatestList = $productModel->getLatestList();
//Tiêu đề trang
$title = 'Trang chủ';
//View
$view = 'home/home.tpl.php';
//Require layout
require 'views/front/layout.tpl.php';
?>
Mở file models/Product.php và thêm vào lớp Product khối lệnh mới dưới đây:
PHP Code:
public function getLatestList($limit = 6){
//SQL
$sql = "SELECT * FROM tbl_product ORDER BY product_id DESC LIMIT 0, $limit";
//Query
$this->db->query($sql);
//Tạo mãng lưu trữ
$listProductLatest = array();
//Fetch
while($row = $this->db->fetch()){
//Khởi tạo đối tượng ProductObj
$productObj = new ProductObj();
//Gán thông tin
$productObj->setProductId($row***91;'product_id'***93;);
$productObj->setCategoryId($row***91;'category_id'***93;);
$productObj->setName($row***91;'name'***93;);
$productObj->setDetail($row***91;'detail'***93;);
$productObj->setImage($row***91;'image'***93;);
$productObj->setPrice($row***91;'price'***93;);
$productObj->setStatus($row***91;'status'***93;);
$productObj->setCreated($row***91;'created'***93;);
$productObj->setModified($row***91;'modified'***93;);
//Gán vào mãng lưu trữ
$listProductLatest***91;***93; = $productObj;
}
//Return
return $listProductLatest;
}
Trong thư mục views/front, tạo mới file layout.tpl.php để trình bày layout của website.
Nội dung file views/front/layout.tpl.php như sau:
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Website giới thiệu sản phẩm - <?php echo $title; ?></title>
</head>
<body>
<?php require Config::BASE_PATH . 'views/front/' . $view; ?>
</body>
</html>
Trong thư mục views/front, tạo mới thư mục tên là home để chứa các file trình bày giao diện trang chủ.
Trong thư mục views/front/home, tạo mới file home.tpl.php để trình bày giao diện trang chủ.
Nội dung file views/front/home/home.tpl.php như sau:
Code:
<?php foreach($productLatestList as $productLatest): ?>
<div style="padding:10px;">
<p>
<img src="<?php echo Config::SITE_URL . 'userfiles/' . $productLatest->getImage(); ?>" />
</p>
<h4>
<a href="<?php echo Config::SITE_URL . 'product-detail.php?product_id=' . $productLatest->getProductId(); ?>"><?php echo $productLatest->getName(); ?></a>
</h4>
<p><?php echo number_format($productLatest->getPrice(), 0, '', '.'); ?> VNĐ</p>
</div>
<?php endforeach; ?>
Các bạn truy cập trang chủ theo địa chỉ http://localhost để kiểm tra.
Bookmarks