<?php include __DIR__."/pdo.php"; //导航 $sql = "SELECT `cate_id`,`name`,`alias` FROM `category`"; $stmt = $dbh-> prepare($sql); //预处理得到stement类 $stmt->execute(); //执行 $cates = $stmt->fetchAll(PDO::FETCH_ASSOC);//返回结果集,已关联数组方式 // print_r ($cates); //movies $sql = "SELECT `mov_id`,`name`,`image`,`detail`,`cate_id` FROM `movies`"; $stmt = $dbh->prepare($sql); $stmt ->execute(); $movies = $stmt->fetchAll(PDO::FETCH_ASSOC); // print_r ($movies); //selfinfo $sql = "SELECT * FROM `system` LIMIT 1"; $stmt= $dbh->prepare($sql); $stmt ->execute(); $systems = $stmt -> fetch(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html> <head> <title><?php echo $systems['title'];?></title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <mata name="description" content="<?php echo $systems['desc'];?>"> <meta name="keywords" content="<?php echo $systems['key'];?>"> </head> <body> <div class="menu"> <a href="index.php">首页</a> <?php foreach ($cates as $cate):?> <a href="list.php?cate_id=<?php echo $cate['cate_id']; ?>"><?php echo $cate['alias']; ?></a> <?php endforeach; ?> </div>
<?php $dbh = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); ?>
上面是header.php和pdo.php的页面代码,下面是index.php页面代码。
<?php include __DIR__."/include/header.php"; //引入头部文件,有错误继续执行 require 遇到错误停止执行。 ?> <div class="contents"> <?php foreach ($cates as $cate):?> <!-- 循环分类变量 --> <h2><?php echo $cate['alias']; ?></h2> <ol> <?php foreach($movies as $movie){ //将导航主键ID与电影外键ID比较,相等即表示是同一类型电影,并输入,不相等,不做任何输出操作。 if ($cate['cate_id']===$movie['cate_id']){ echo "<li><a href='detail.php?mov_id=".$movie['mov_id']."'> ".$movie['name']."</a></li>"; } } ?> </ol> <?php endforeach;?> <!-- 结算循环 --> <?php include __DIR__."/include/footer.php"; ?>
下面是footer.php页面。
</div> <div class="footer"> <p><?php echo $systems['copy'];?> ©版权所有</p> </div> </body> </html>
下面是CSS样式
.menu { width: 500px; margin:0px auto; background-color: #666; text-align: center; font-size: 20px; margin-bottom:5px; } a{text-decoration: none;color: #000;} .menu a {margin-left:40px;color: #fff;} .contents {width: 460px; margin:0px auto; border: 1px solid #444; padding: 20px; font-size: 20px;} .contents h2 { border-bottom: 1px solid #eee; } div.footer { width: 500px; margin:0px auto; text-align: center; border: 1px solid #444; margin-top: 5px; /*background-color: ;*/ }
下面是detail.php和list.php页面
<?php include __DIR__."/include/header.php"; $mov_id = intval($_GET['mov_id']); ?> <div class="contents"> <?php foreach($movies as $movie){ if ($movie['mov_id']==$mov_id){ //遍历比较传值过来的mov_id与查询出来的数组集合的每一项ID对比,对比上了就显示那条数据。否则不做任何显示。 echo "<img src='images/".$movie['image']."' width='400px;'><br/>"; echo "<h2>".$movie['name']."</h2>"; echo "<p>".$movie['detail']."</p>"; } } ?> <?php include __DIR__."/include/footer.php"; ?>
<?php include __DIR__."/include/header.php"; $cate_id=intval($_GET['cate_id']); echo "<div class='contents'><ol>"; foreach ($movies as $movie) { if ($movie['cate_id']==$cate_id) { echo "<li><a href='detail.php?mov_id=".$movie['mov_id']."'> ".$movie['name']."</a></li>"; } } echo "</ol>"; include __DIR__."/include/footer.php"; ?>