展現商品列表
- 建立文件
C:\Users\xujunhao\Desktop\shop\backend\shop\index.php
- 掐頭去尾
- 查詢數據庫
<?php
$sql = "select * from {$prefix}product order by created_at desc";
$products = queryAll($sql);
?>
複製代碼
省略代碼...
<div class="row">
<?php foreach($products as $product):?>
<div class="col-md-3 single__pro col-lg-3 cat--1 col-sm-4 col-xs-12">
<div style="margin-top: 20px" class="product foo">
<div class="product__inner">
<div class="pro__thumb">
<a href="product_details.php?id=<?php echo $product['id']; ?>">
<img src="<?php echo $product['pic']; ?>" alt="product images" />
</a>
</div>
<div class="product__hover__info">
<ul class="product__action">
<li>
<a title="加入購物車" href="cart.html"><span class="ti-shopping-cart"></span></a>
</li>
<li>
<a title="查看詳情" href="product_details.php?id=<?php echo $product['id']; ?>" >查看詳情</a >
</li>
</ul>
</div>
</div>
<div class="product__details">
<h2>
<a href="product_details.php?id=<?php echo $product['id']; ?>" ><?php echo $product['name']; ?></a >
</h2>
<ul class="product__price">
<li class="new__price">¥<?php echo $product['price']; ?></li>
</ul>
</div>
</div>
</div>
<?php endforeach;?>
</div>
省略代碼...
複製代碼
- 把id寫在圖片連接, 標題中, 這樣在跳轉詳情的時候, 能夠經過id查詢數據
<div class="pro__thumb">
<a href="product_details.php?id=<?php echo $product['id']; ?>">
<img src="<?php echo $product['pic']; ?>" alt="product images" />
</a>
</div>
省略代碼...
<li>
<a title="查看詳情" href="product_details.php?id=<?php echo $product['id']; ?>" >查看詳情</a >
</li>
省略代碼...
<h2>
<a href="product_details.php?id=<?php echo $product['id']; ?>" ><?php echo $product['name']; ?></a >
</h2>
複製代碼
展現商品詳情
- 建立商品詳情頁
C:\Users\xujunhao\Desktop\shop\backend\shop\product_details.php
- 掐頭去尾
- 經過id查詢數據並展現
<?php
$id = $_GET['id'];
$sql = "select * from {$prefix}product where id = $id";
$product = queryOne($sql);
?>
複製代碼
<div class="pro__details">
<p><?php echo mb_substr($product['description'],0,30,'utf8').'...'; ?></p>
</div>
複製代碼