Correction status:qualified
Teacher's comments:其实这个作业是相当容易的, 只不过给网站换个数据来源
先贴个目录结构图和db的图
第一个是header.php
<?php //连接数据库 require __DIR__.'\connect.php'; //写查询语句 $sql='select `name`,`kecheng_id`,`image`,`detail`, `categroy_id`from `kecheng`'; //预处理对象 $stmt=$pdo->prepare($sql); //执行sql语句,得到一个查询出来的课程数组 $stmt->execute(); $kecheng=$stmt->fetchAll(PDO::FETCH_ASSOC); //写查询语句 $sql='select `categroy_id`,`name`,`alias` from `categroy`'; //预处理对象 $stmt=$pdo->prepare($sql); //执行sql语句,得到一个查询出来的数组 $stmt->execute(); $categroy=$stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>神仙不在</title> <link rel="stylesheet" href="./static/css/index.css"> </head> <body> <!--网站的头部--> <div class="header"> <!--导航--> <ul class="nav"> <li><a href="index.php">首页</a></li> <!-- foreach遍历出导航--> <?php foreach ($categroy as $value): ?> <!-- a链接的地址是个列表模板--> <li><a href=" list.php?categroy_id=<?php echo $value['categroy_id'] ?>"><?php echo $value['alias'] ?></a></li> <?php endforeach;?> </ul> </div>
点击 "运行实例" 按钮查看在线实例
index.php
<!--引入公共头部--> <?php include __DIR__.'\inc\header.php' ?> <!--网站的主体--> <!--遍历栏目--> <?php foreach ($categroy as $cate){ echo "<h2>{$cate['alias']}</h2>"; // 遍历对应的栏目信息 echo '<ol>'; foreach ($kecheng as $kc) if ($kc['categroy_id']==$cate['categroy_id']){ // a链接指向对应的详情页模板 echo "<ol><a href='detail.php?kecheng_id={$kc['kecheng_id']}'>{$kc['name']}</a></ol>"; } echo '</ol>'; } ?> <?php include __DIR__.'\inc\footer.php' ?>
点击 "运行实例" 按钮查看在线实例
list.php
<!--引入公共头部--> <?php include __DIR__.'\inc\header.php' ?> <?php $categroy_id=(int)$_GET['categroy_id']; ?> <!--网站的主体--> <!-- 找到对应的栏目--> <?php foreach ($categroy as $cate){ if ($categroy_id==$cate['categroy_id']){ echo "<h2>{$cate['alias']}</h2>"; // 遍历对应的栏目信息 echo '<ol>'; foreach ($kecheng as $kc) if ($kc['categroy_id']==$cate['categroy_id']){ // a链接指向对应的详情页模板 echo "<ol><a href='detail.php?kecheng_id={$kc['kecheng_id']}'>{$kc['name']}</a></ol>"; } echo '</ol>'; } } ?> <?php include __DIR__.'\inc\footer.php' ?>
点击 "运行实例" 按钮查看在线实例
detail.php
<!--引入公共头部--> <?php include __DIR__.'\inc\header.php' ; $kecheng_ids=(int)$_GET['kecheng_id']; // 遍历对应的详情页信息 echo '<ol>'; foreach ($kecheng as $kc){ if ($kc['kecheng_id']==$kecheng_ids){ echo "<h3>{$kc['name']}</h3>"; echo "<p><img src='{$kc['image']}' alt=''></p>"; echo "<p>{$kc['detail']}</p>"; } echo '</ol>'; } include __DIR__.'\inc\footer.php';
点击 "运行实例" 按钮查看在线实例
详情页里面有个坑,先看下面的代码
echo '<ol>'; foreach ($kecheng as $kc){ if ($kc['kecheng_id']==$kecheng_ids){ echo "<h3>{$kc['name']}</h3>"; echo "<p><img src='{$kc['image']}' alt=''></p>"; echo "<p>{$kc['detail']}</p>"; } echo '</ol>'; }
点击 "运行实例" 按钮查看在线实例
$kc['kecheng_id'] 在7月23日的时候它得到的是个整型,今天得到的是个字符串。
整个在线演示http://www.shenxianbuzai***/7.25/