Correction status:qualified
Teacher's comments:模板写是不错, html与php能完整的分离开
<!--数据库信息:database.php--> <?php return [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'php', 'username' => 'root', 'password' => 'root' ];
点击 "运行实例" 按钮查看在线实例
<!--connect.php--> <?php // 连接数据库: PDO //$dsn = '数据库的类型:host=数据库主机名;dbname=默认数据库名称'; $db = require 'database.php'; // $dsn $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; $username = $db['username']; $password = $db['password']; try { $pdo = new PDO($dsn, $username, $password); // var_dump($pdo); } catch (PDOException $e) { die('连接失败' . $e->getMessage()); }
点击 "运行实例" 按钮查看在线实例
<!--公共头部header.php--> <?php //引入数据库 include __DIR__ . '/inc/connect.php'; // 获取栏目表 $sql = 'SELECT `cate_id`,`name`,`alias` FROM `cates`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $cates = $stmt->fetchAll(PDO::FETCH_ASSOC); // 获取系统数据 $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt->execute(); $system = $stmt->fetch(PDO::FETCH_ASSOC); // print_r($system);die(); // 获取文章数据 $sql = 'SELECT * FROM `movies`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $movies = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="keywords" content="<?php echo $system['key'] ?>" /> <meta name="description" content="<?php echo $system['desc'] ?> " /> <title><?php echo $system['title'] ?></title> <style> *{ margin: 0; padding: 0; } #container{ width: 1000px; margin: 0 auto; } #container #header{ width: 100%; height: 50px; background: #000; margin: 0 auto; } #container #header .nav{ width: 600px; margin: 0 auto; overflow: hidden; } #container #header .nav li{ width: 120px; height: 50px; float: left; line-height: 50px; list-style: none; text-align: center; } #container #header .nav li:hover{ background: #444444; } #container #header .nav li a{ color: white; text-decoration: none; } #container #box{ width: 100%; height: 600px; border: 1px solid #cccccc; margin: 10px auto; } #container #box ul{ margin-left: 20px; } #container #box ul li{ list-style: none; height: 30px; margin-bottom: 5px; } #container #box ul li a{ color: black; margin-left: 20px; } #container #footer{ width: 100%; height: 40px; background: #cccccc; text-align: center; line-height: 40px; } </style> </head> <body> <div id="container"> <!-- 头部--> <div id="header"> <ul class="nav"> <li style="background: red"><a href="index.php">首页</a></li> <?php foreach ($cates as $value) : ?> <li><a href="list.php?cate_id=<?php echo $value['cate_id'] ?>"><?php echo $value['alias'] ?></a></li> <?php endforeach; ?> </ul> </div> <!--end头部-->
点击 "运行实例" 按钮查看在线实例
<!--首页index.php--> <?php require __DIR__ .'/header.php'?> <!--主体--> <div id="box"> <?php foreach ($cates as $cate){ echo "<h2>{$cate['alias']}</h2>"; foreach ($movies as $movie){ if ($cate['cate_id'] === $movie['cate_id']){ echo '<ul>'; echo "<li><a href='article.php?mov_id={$movie['mov_id']}' >{$movie['name']}</a></li>"; echo '</ul>'; } }; echo '<hr>'; } ?> </div> <!--end主体--> <!--footer--> <?php require __DIR__ . '/footer.php'?>
点击 "运行实例" 按钮查看在线实例
<!--列表页面list.php--> <?php require __DIR__ .'/header.php'?> <?php //print_r($movies);die; ?> <!--主体--> <div id="box"> <?php foreach ($cates as $cate){ if (intval($_GET['cate_id']) === intval($cate['cate_id'])){ echo "<h2>{$cate['alias']}</h2>"; foreach ($movies as $movie){ if ($cate['cate_id'] === $movie['cate_id']){ echo '<ul>'; echo "<li><a href='article.php?mov_id={$movie['mov_id']}' >{$movie['name']}</a></li>"; echo '</ul>'; } }; echo '<hr>'; } } ?> </div> <!--end主体--> <!--footer--> <?php require __DIR__ . '/footer.php'?>
点击 "运行实例" 按钮查看在线实例
<!--内容页article.php--> <?php require __DIR__ .'/header.php'?> <!--主体--> <div id="box"> <?php foreach ($movies as $movie){ if (intval($movie['mov_id']) ===intval($_GET['mov_id'])){ echo "<h2>{$movie['name']}</h2>"; echo $movie['detail']; } } ?> </div> <!--end主体--> <!--footer--> <?php require __DIR__ . '/footer.php'?>
点击 "运行实例" 按钮查看在线实例
<!--公共底部footer.php--> <div id="footer"> <?php echo $system['copy'] . '© 版权所有' ?> </div> <!--end footer--> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例
数据库截图: