展现商品列表
- 建立文件
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>
复制代码