制作一个商品的列表与详情模板
首页:
栏目页:
详情页:
index.php文件
<?php // 加载公共头部 include __DIR__ . "/inc/header.php"; foreach ($cates as $cate) { echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; foreach ($foods as $food) { // 判断当前所属栏目是否与当前栏目id相同? if ($cate['cate_id'] === $food['cate_id']) { echo "<li><a href='detail.php?mov_id=" .$food['food_id']. "'>{$food['name']}</a></li>"; } } echo '</ol>'; } // 加载公共底部 include __DIR__ . '/inc/footer.php';
点击 "运行实例" 按钮查看在线实例
list.php文件
<?php // 加载公共头部 include __DIR__ . '/inc/header.php'; $cate_id = $_GET['cate_id']; // 注意: url中的内容都是字符串, 转换类型 $cate_id = (int)$cate_id; foreach ($cates as $cate) { if ($cate['cate_id'] === $cate_id) { echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; foreach ($foods as $food) { if ($cate['cate_id'] == $food['cate_id']) { echo "<li><a href='detail.php?food_id=" .$food['food_id']. "'>{$food['name']}</a></li>"; } } echo '</ol>'; }; } // 加载公共底部 include __DIR__ . '/inc/footer.php';
点击 "运行实例" 按钮查看在线实例
detail.php文件
<?php // 加载公共头部 include __DIR__ . '/inc/header.php'; $food_id = intval($_GET['food_id']); foreach ($foods as $food) { if ($food['food_id'] === $food_id) { echo "<h3>{$food['name']}</h3>"; echo '<img src="../static/images/'.$food['image'].'" alt="" width="300">'; echo "<p style='text-indent: 2em'>{$food['detail']}</p>"; } }; // 加载公共底部 include __DIR__ . '/inc/footer.php';
点击 "运行实例" 按钮查看在线实例