数据库
1、system表:网站的配置信息
2、category表:栏目表
3、detailsList表:内容详情表
一、公共部分
database.php
连接数据库参数设置:
$dsn = '数据库的类型:host=数据库主机名; dbname=数据库名称';
<?php return[ 'type' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'php', 'username' => 'root', 'password' => '' ];
connect.php
连接数据库
<?php // 连接数据库:PDO $db = require 'database.php'; // $dsn = '数据库的类型:host=数据库主机名; dbname=数据库名称'; $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 // 1、连接数据库 require __DIR__ . '/connect.php'; // 2、获取网站的配置信息 $sql = 'SELECT * FROM `system` limit 1'; $stmt = $pdo->prepare($sql); $stmt->execute(); $system = $stmt->fetch(PDO::FETCH_ASSOC); // 3、获取栏目信息 $sql = 'SELECT `cate_id`, `name`, `alias` FROM `category`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $cates = $stmt->fetchAll(PDO::FETCH_ASSOC); $cate_count = count($cates); // 4、获取详细信息 $sql = 'SELECT * FROM detailsList'; $stmt = $pdo->prepare($sql); $stmt -> execute(); $detailsList = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?php echo $system['title'] ?></title> <meta name="keywords" content="<?php echo $system['key'] ?>"> <meta name="description" content="<?php echo $system['desc'] ?>"> <link rel="stylesheet" href="./static/css/style.css"> </head> <body> <div class="header"> <ul class="nav"> <li><a href="index.php">首页</a></li> <li><a href="">***介绍</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>
底部 footer.php
<p class="footer"><?php echo $system['copy'] ?> @ 版权所有</p> </body> </html>
二、首页、列表页、详情页
1、首页 index.php
<?php header("content-type:text/html;charset=utf-8"); // 加载公共头部 include __DIR__. "/inc/header.php"; ?> <h2>***介绍</h2> <p>本院坚持“专家技术型”发展战略,组建了一支由医学博士、硕士等精英组成的,具有专业水准的医疗技术团队,其中医务人员 一百余名,高级职称三十余名,中级职称五十余名。全院各科室学科带头人由临床经验丰富的权威医学专家担任, 他们来自于同济、协和、省人民、省妇幼、中南、陆总、武警等***。</p> <?php // 使用双重循环来遍历两个数组 // 外层遍历栏目数组$cates,内层根据栏目id来查询对应的栏目详情 $detailsList 信息 foreach ($cates as $cate){ echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; // 遍历栏目详情数组 foreach ($detailsList as $list){ // 判断当前栏目详情所属栏目是否与当前栏目id相同 if($list['cate_id'] === $cate['cate_id']){ echo "<li><a href='detail.php?deta_id=". $list['deta_id'] ."'>{$list['title']}</a></li>"; } } echo '</ol>'; } ?> <?php // 加载公共底部 include __DIR__ . '/inc/footer.php'; ?>
2、列表页
<?php header("content-type:text/html;charset=utf-8"); require __DIR__ . '/inc/header.php'; $cate_id = $_GET['cate_id']; //var_dump($cate_id);die; string类型 foreach ($cates as $cate){ if($cate['cate_id'] === $cate_id){ echo "<h2>{$cate['alias']}</h2>"; echo '<ul>'; // 遍历 detailsList 详情表 foreach ($detailsList as $list){ if($cate['cate_id'] === $list['cate_id']){ echo "<li><a href='detail.php?deta_id=".$list['cate_id']."'>{$list['title']}</a></li>"; } } echo '</ul>'; } } include __DIR__ . '/inc/footer.php';
3、详情页
<?php header("content-type:text/html;charset=utf-8"); include __DIR__ . '/inc/header.php'; $deta_id = $_GET['deta_id']; foreach ($detailsList as $detail){ if($detail['deta_id'] === $deta_id ){ echo "<h3>{$detail['title']}</h3>"; echo '<img src="./images/'. $detail['img'] .'" alt="" width="350px">'; echo "<p>{$detail['detail']}</p>"; } } include __DIR__ . '/inc/footer.php';