Từng bước học lập trình php căn bản qua dự án website giới thiệu sản phẩm – Trang chi tiết sản phẩm

Tại thư mục gốc (Root), các bạn tạo mới file product-detail.php để viết mã lệnh xử lý trang chi tiết sản phẩm.

Nội dung file product-detail.php như sau:

<?php
//Require các file cần thiết
require ‘configs/config.php’;
require ‘libraries/connect.php’;
require ‘models/product.php’;

//Lấy product_id từ URL
$product_id = isset($_GET[‘product_id’]) ? ((int)$_GET[‘product_id’]) : 0;

//Lấy thông tin sản phẩm
$product_active = get_product_active_by_id($product_id);

//Require file giao diện (View)
require ‘views/front/product/detail.tpl.php’;
?>

Mở file models/product.php và viết thêm vào khối lệnh mới dưới đây:

function get_product_active_by_id($product_id){
//SQL
$sql = “SELECT * FROM tbl_product WHERE status = 1 AND product_id = $product_id”;

//Query
$query = mysql_query($sql);

//Fetch và return
return mysql_fetch_assoc($query);
}

Trong thư mục views/front/product, tạo mới file detail.tpl.php để trình bày giao diện trang chi tiết sản phẩm.

Nội dung file views/front/product/detail.tpl.php như sau:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Website giới thiệu sản phẩm – Chi tiết sản phẩm</title>
</head>
<body>

<div style=”padding:10px;”>
<p>
<img src=”<?php echo SITE_URL . ‘userfiles/’ . $product_active[‘image’]; ?>” />
</p>
<h4><?php echo $product_active[‘name’]; ?></h4>
<p><?php echo number_format($product_active[‘price’], 0, ”, ‘.’); ?> VNĐ</p>
</div>

</body>
</html>

Các bạn truy cập trang danh sách sản phẩm, sau đó nhấp vào tên sản phẩm để truy cập trang chi tiết sản phẩm.

Tác giả: Lê Trung Hiếu

Viết một bình luận