Correction status:qualified
Teacher's comments:加了点简单样式, 看上去好一些
学习了 PDO, 尝试将之前的影视小网站数据从数据库中获取显示。
1. 首先需要进行数据库的连接操作,将数据库连接文件放置到单独文件中
连接数据库文件 connect.php
先简单认识一下 pdo 数据库连接格式:
不过 为了 数据的可配置与便于管理,一般将这些信息都拆开放置到数组中。
<?php /** * PDO 连接数据库文件 */ $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'film', 'username' => 'root', 'password' => 'root' ]; //配置数据源DSN信息 $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try{ $pdo = new PDO($dsn, $db['username'], $db['password']); }catch(PDOException $e){ die('连接失败: '.$e->getMessage()); }
2. 页面结构
以下只列出几个数据较多的页面。
3. 在 头部文件中将数据都获取到
header.php
<?php // 连接数据库:$pdo require __DIR__.'/connect.php'; // 获取栏目信息 $sql = 'SELECT * FROM `category`'; $stmt = $pdo->prepare($sql); $stmt->execute(); if($stmt->execute()){ $cates = $stmt->fetchAll(PDO::FETCH_ASSOC); }else { $cates = []; }; $cate_count = count($cates); // 栏目数量 // 获取影视信息 $sql = 'SELECT * FROM `movies`'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ $movies = $stmt->fetchAll(PDO::FETCH_ASSOC); }else { $movies = []; }; // 获取网站配置信息(SEO) $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ $system = $stmt->fetch(PDO::FETCH_ASSOC); }else { $system = []; }; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="static/css/style.css"> <meta name="description" content="<?php echo $system['desc'] ?>"> <meta name="keywords" content="<?php echo $system['key'] ?>"> <title><?php echo $system['title'] ?></title> </head> <body> <!--头部导航--> <div class="header"> <ul class="nav"> <li><a href="index.php">首页</a></li> <?php foreach ($cates as $cate) : ?> <li><a href="list.php?cate_id=<?php echo $cate['cate_id']; ?>"><?php echo $cate['alias'] ?></a></li> <?php endforeach; ?> </ul> </div>
4. 首页 将 影视数据 渲染出来
index.php
<?php include __DIR__ . "/header.php"; foreach ($cates as $cate) { echo "<h2 class='type-name'>{$cate['alias']}</h2>"; echo '<ol class="movie-list">'; foreach ($movies as $movie) { if ($cate['cate_id'] === $movie['cate_id']) { echo "<li><a href='detail.php?mov_id=" . $movie['mov_id'] . "'>" . $movie['name'] . "</a></li>"; } } echo '</ol>'; } include __DIR__ . '/footer.php'; ?>
5. 影视详情页,将 影视和对应的 评论一并渲染出来。
detail.php
<?php include __DIR__ . '/header.php'; $mov_id = intval($_GET['mov_id']); // 获取对应评论信息 $sql = 'SELECT * FROM `comment` WHERE `mov_id` = :mov_id'; $stmt = $pdo->prepare($sql); $stmt->bindParam('mov_id',$mov_id,PDO::PARAM_INT); if($stmt->execute()){ $comments = $stmt->fetchAll(PDO::FETCH_ASSOC); }else { $comments = []; } foreach ($movies as $movie) { if ($movie['mov_id'] == $mov_id) { echo "<h3 class='movie-name'>{$movie['name']}</h3>"; echo '<img class="pic" src="static/images/' . $movie['image'] . '" alt="" width="300">'; echo "<p style='text-indent: 2em' class='detail'>{$movie['detail']}</p>"; } }; echo "<h3>最新评论</h3>"; echo "<ul class='list'>"; foreach ($comments as $item) { if ($item['mov_id'] == $mov_id) { echo '<li class="comment-item"><span class="user">' .$item['user'].'</span>'. $item['detail'] . '</li>'; } } echo "</ul>"; include __DIR__ . '/footer.php';
至此一个简易版的影视网站初级样子了。