网站的数据来自数据库,查找数据库中的数据。
1 连接数据库
2. 获取影视信息
3. 获取栏目信息
4. 获取网站的配置信息
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()); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?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()); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
database.php
实例
<?php return [ 'type' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'php', 'username' => 'root', 'password' => 'root' ]; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
header.php
实例
<?php require __DIR__ . `/connect.php`; $sql = 'SELECT `mov_id`,`name`,`image`,`detail`,`cate_id` FROM `movies`'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $movies=$stmt->fetchAll(PDO::FETCH_ASSOC); $sql = 'SELECT `cate_id`,`name`,`alias` FROM `category`'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $cates=$stmt->fetchAll(PDO::FETCH_ASSOC); $cate_count = count($cates); $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $system =$stmt->fetch(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="../static/css/css1.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=<?=$cate['cate_id']?>"><?php echo $cate['alias']; ?></a></li> <?php endforeach; ?> </ul> </div>
运行实例 »
点击 "运行实例" 按钮查看在线实例
footer.php
实例
<div class="footer"> <p class="copyright"><?php echo $system['copy']?> © 版权所有</p> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
index.php
实例
<?php include __DIR__.'/inc1/header.php'; foreach ($cates as $cate){ echo "<h2> {$cate['alias']}</h2>"; echo '<ol>'; 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__ .'/inc1/footer.php';
运行实例 »
点击 "运行实例" 按钮查看在线实例
detail.php
实例
<?php include __DIR__.'/inc1/header.php'; $mov_id = intval($_GET['mov_id']); foreach ($movies as $movie){ if ($movie['mov_id'] === $mov_id){ echo "<h3>{$movie['name']}</h3>"; echo '<img src="../static/images/'.$movie['image'].'" alt="" width="300">'; echo "<p style='text-indent: 2em'>{$movie['detail']}</p>"; } } include __DIR__ .'/inc1/footer.php';
运行实例 »
点击 "运行实例" 按钮查看在线实例
list.php
实例
<?php include __DIR__.'/inc1/header.php'; $cate_id =intval($_GET['$cate_id']); foreach ($cates as $cate) { if ($cate['cate_id'] === $cate_id) { echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; 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>'; break; }; } include __DIR__ .'/inc1/footer.php';
运行实例 »
点击 "运行实例" 按钮查看在线实例