0110作业:静态网站模拟动态化
动态网站和静态分析
- 在我们常见的网站中,大部分都是动态化数据填充;
- 动态网站能最简单的保证网站最快的更新;
- 动态网站是一次投入,在以后的运行中,不必要去改动网页代码;
- 动态网站把所有数据保存到数据库,利于数据的安全;
网站示例
网站首页
<?php include __DIR__ . '/inc/public_header.php' ?>
<?php
//调用栏目名称
function catListName($cid, $catLists)
{
foreach ($catLists as $catList) {
if ($catList['cid'] === $cid) {
//当参数过来的cid和栏目的cid相等输出栏目名字
$catListName = $catList['name'];
}
}
return $catListName;
}
//调用随机小说函数
function randomBooks($bookLists, $num)
{
$bookListIds = array_rand($bookLists, $num);
foreach ($bookListIds as $bookListId) {
$bookRandLists[] = $bookLists[$bookListId];
}
return $bookRandLists;
}
$bookRandLists = randomBooks($bookLists, 6);
?>
<!--小说内容区域-->
<main>
<!--随机小说-->
<div class="book-list">
<div class="book-title"><span>随机推荐</span></div>
<div class="b-lists">
<?php foreach ($bookRandLists as $randList): ?>
<div class="b-list">
<a href="<?= $randList['url'] ?>"><img src="<?= PIC_PATH . $randList['pic'] ?>" alt=""></a>
<div class="b-list-wenzi">
<a href="<?= $randList['url'] ?>"><?= $randList['name'] ?></a>
<span><?= $randList['summary'] ?></span>
<div>
<span><i class="iconfont icon-zuozhe"></i><?= $randList['author'] ?></span>
<span><?= catListName($randList['cid'], $catLists); ?></span>
</div>
</div>
</div>
<?php endforeach ?>
</div>
</div>
<!--小说循环-->
<?php foreach ($catLists as $catList): ?>
<div class="book-list">
<div class="book-title"><span><?= $catList['name'] ?></span></div>
<div class="b-lists">
<?php foreach ($bookLists as $bookList): ?>
<!--输出当前栏目ID的小说-->
<?php if ($catList['cid'] === $bookList['cid']): ?>
<div class="b-list">
<a href="<?= $bookList['url'] ?>"><img src="<?= PIC_PATH . $bookList['pic'] ?>" alt=""></a>
<div class="b-list-wenzi">
<a href="<?= $bookList['url'] ?>"><?= $bookList['name'] ?></a>
<span><?= $bookList['summary'] ?></span>
<div>
<span><i class="iconfont icon-zuozhe"></i><?= $bookList['author'] ?></span>
<span><?= catListName($bookList['cid'], $catLists); ?></span>
</div>
</div>
</div>
<?php endif; ?>
<?php endforeach ?>
</div>
</div>
<?php endforeach ?>
</main>
<?php include __DIR__ . '/inc/public_footer.php' ?>
网站小说列表页
<!--引入头部文件-->
<?php include __DIR__.'/inc/public_header.php'?>
<?php
$cid = $_GET['cid'];
//调用栏目名称
function catListName ($cid,$catLists){
foreach ($catLists as $catList){
if($catList['cid'] === intval($cid)){
//当参数过来的cid和栏目的cid相等输出栏目名字
$catListName = $catList['name'];
}
}
return $catListName;
}
$catListName = catListName($cid,$catLists);
//调用小说列表
function bookList ($bookLists,$cid){
foreach ($bookLists as $bookList){
if($bookList['cid'] === intval($cid)) {
$currentLists[] = $bookList;
}
}
return $currentLists;
}
$currentLists = bookList($bookLists,$cid);
//print_r($currentLists);
?>
<!--小说内容区域-->
<main>
<!--小说列表-->
<div class="book-list">
<div class="book-title"><span><?=$catListName?></span></div>
<div class="b-lists">
<?php foreach ($currentLists as $currentList):?>
<div class="b-list">
<a href="<?=$currentList['url'] ?>"><img src="<?=PIC_PATH.$currentList['pic'] ?>" alt=""></a>
<div class="b-list-wenzi">
<a href="<?=$currentList['url'] ?>"><?=$currentList['name'] ?></a>
<span><?=$currentList['summary'] ?></span>
<div>
<span><i class="iconfont icon-zuozhe"></i><?=$currentList['author'] ?></span>
<span><?=$catListName?></span>
</div>
</div>
</div>
<?php endforeach ?>
</div>
</div>
</main>
<!--引入页脚文件-->
<?php include __DIR__.'/inc/public_footer.php'?>
网站专题页
<!--引入头部文件-->
<?php include __DIR__ . '/inc/public_header.php' ?>
<?php
//调用栏目名称
function catListName($cid, $catLists)
{
foreach ($catLists as $catList) {
if ($catList['cid'] === $cid) {
//当参数过来的cid和栏目的cid相等输出栏目名字
$catListName = $catList['name'];
}
}
return $catListName;
}
//调用随机小说函数
function randomBooks($bookLists, $num)
{
$bookListIds = array_rand($bookLists, $num);
foreach ($bookListIds as $bookListId) {
$bookRandLists[] = $bookLists[$bookListId];
}
return $bookRandLists;
}
$bookRandLists = randomBooks($bookLists, 6);
?>
<main>
<!--随机小说-->
<div class="book-list">
<div class="book-title"><span>随机推荐</span></div>
<div class="b-lists">
<?php foreach ($bookRandLists as $randList): ?>
<div class="b-list">
<a href="<?= $randList['url'] ?>"><img src="<?= PIC_PATH . $randList['pic'] ?>" alt=""></a>
<div class="b-list-wenzi">
<a href="<?= $randList['url'] ?>"><?= $randList['name'] ?></a>
<span><?= $randList['summary'] ?></span>
<div>
<span><i class="iconfont icon-zuozhe"></i><?= $randList['author'] ?></span>
<span><?= catListName($randList['cid'], $catLists); ?></span>
</div>
</div>
</div>
<?php endforeach ?>
</div>
</div>
</main>
<!--引入页脚文件-->
<?php include __DIR__ . '/inc/public_footer.php' ?>
网站内容详情页
<?php include __DIR__ . '/inc/public_header.php' ?>
<?php
//接收商品ID
$id = $_GET['id'];
//调用小说信息
function getBook($id, $bookLists)
{
foreach ($bookLists as $bookList) {
if ($bookList['id'] === intval($id)) {
$getBook = $bookList;
}
}
return $getBook;
}
$getBook = getBook($id, $bookLists);
//获取当前父栏目名字
$cid = $getBook['cid'];
function catListName($cid, $catLists)
{
foreach ($catLists as $catList) {
if ($catList['cid'] === intval($cid)) {
//当参数过来的cid和栏目的cid相等输出栏目名字
$catListName = $catList['name'];
}
}
return $catListName;
}
$catListName = catListName($cid, $catLists);
print_r($catListName);
?>
<div class="info">
<div class="info-top">
<a href=""><img src="<?= PIC_PATH . $getBook['pic'] ?>" alt=""></a>
<div>
<div><span><?= $getBook['name'] ?></span><span><?= $getBook['author'] ?></span></div>
<div>
<span><?= $getBook['status'] ?></span><span><?= $getBook['sign'] ?></span><span><?= $getBook['price'] ?></span><span><?= $catListName ?></span>
</div>
<div>
<span><i><?= $getBook['word'] ?></i>万字</span><span><i><?= $getBook['overall'] ?></i>总推荐</span><span><i><?= $getBook['Weekly'] ?></i>周推荐</span>
</div>
<div>
<a href="">免费试读</a>
<a href="">加入书架</a>
<a href="">投票互动</a>
</div>
</div>
</div>
<div class="info-cont">
<span>作品信息</span>
<span><?= $getBook['intro'] ?></span>
</div>
</div>
<?php include __DIR__ . '/inc/public_footer.php' ?>
网站公共部分代码
<!--公共头部-->
<?php include './config/config.php' ?>
<!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">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/font/iconfont.css">
<title>小说阅读网</title>
</head>
<body>
<header>
<a href="index.php">网站首页</a>
<a href="random.php">随机推荐</a>
<?php foreach ($catLists as $catList): ?>
<a href="<?= $catList['url'] ?>"><?= $catList['name'] ?></a>
<?php endforeach; ?>
</header>
<!--公共底部-->
<footer>
<?php foreach ($tabBars as $tabBar):?>
<a href="<?=$tabBar['url']?>"><i class="iconfont <?=$tabBar['icon']?>"></i><?=$tabBar['name']?></a>
<?php endforeach;?>
</footer>
</body>
</html>
公共css
代码
/*页面元素初始化*/
* {
margin: 0;
padding: 0;
}
body {
font-size: 13px;
color: #888888;
background-color: #EDEFF0;
display: flex;
flex-direction: column;
min-width: 360px;
max-width: 768px;
margin: 0 auto;
overflow-x: hidden;
-webkit-column-rule-color: transparent;
}
a {
text-decoration: none;
color: #404040;
font-size: 13px;
}
li {
list-style: none;
}
/*header 部分*/
header {
height: 40px;
background: #333333;
line-height: 40px;
display: flex;
justify-content: space-around;
position: fixed;
width: 100%;
min-width: 360px;
max-width: 768px;
top: 0;
}
header > a {
color: white;
}
header > a:hover {
color: #999999;
}
/*小说列表*/
main {
display: flex;
flex-direction: column;
margin: 50px 0;
flex: 1;
}
main > .book-list {
display: flex;
flex-direction: column;
background-color: white;
padding: 10px 0;
margin-top: 10px;
}
main > .book-list > .book-title {
height: 30px;
font-size: 20px;
padding: 10px 5px 5px 5px;
border-bottom: 1px solid #999999;
}
main > .book-list > .b-lists {
display: flex;
flex-wrap: wrap;
margin-top: 10px;
}
main > .book-list > .b-lists > .b-list {
width: 33.33333%;
display: flex;
padding: 5px;
box-sizing: border-box;
}
main > .book-list > .b-lists > .b-list > a > img {
width: 72px;
height: 96px;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi {
padding-left: 10px;
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: space-between;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi > a {
font-size: 16px;
color: #333333;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi > span {
font-size: 12px;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi > div {
display: flex;
justify-content: space-between;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi > div > span:last-of-type {
padding: 0 3px;
border: 1px solid #999999;
font-size: 12px;
}
main > .book-list > .b-lists > .b-list > .b-list-wenzi > div > span:first-of-type > i {
font-size: 14px;
margin-right: 5px;
}
/*footer区*/
footer {
background-color: #333333;
display: flex;
justify-content: space-around;
padding: 5px 0;
box-sizing: border-box;
position: fixed;
width: 100%;
min-width: 360px;
max-width: 768px;
bottom: 0;
}
footer > a {
display: flex;
flex-direction: column;
text-align: center;
color: white;
font-size: 12px;
}
footer > a:hover {
color: #999999;
}
footer > a > i {
font-size: 16px;
padding-bottom: 3px;
}
/*内容页*/
.info {
margin-top: 50px;
background-color: white;
padding: 5px;
display: flex;
flex-direction: column;
}
.info > .info-top {
display: flex;
}
.info > .info-top > a {
width: 144px;
height: 192px;
padding: 10px;
}
.info > .info-top > a > img {
width: 144px;
height: 192px;
box-shadow: 0 0 3px #888888;
}
.info > .info-top > div {
padding: 10px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.info > .info-top > div > div:first-of-type > {
display: flex;
}
.info > .info-top > div > div:first-of-type > span:first-of-type {
font-size: 28px;
font-weight: bold;
color: #333333;
padding-right: 20px;
box-sizing: border-box;
}
.info > .info-top > div > div:nth-child(2) {
display: flex;
padding: 15px 0;
box-sizing: border-box;
}
.info > .info-top > div > div:nth-child(2) > span {
padding: 2px 10px;
border-radius: 10px;
border: 1px solid #888888;
margin: 0 5px;
}
.info > .info-top > div > div:nth-child(2) > span:last-of-type {
color: red;
border: 1px solid red;
}
.info > .info-top > div > div:nth-child(3) {
display: flex;
padding: 10px 0;
}
.info > .info-top > div > div:nth-child(3) > span {
font-size: 12px;
margin: 0 5px;
}
.info > .info-top > div > div:nth-child(3) > span > i {
font-size: 20px;
font-weight: bold;
color: #2d353c;
font-style: normal;
padding: 0 2px;
}
.info > .info-top > div > div:last-of-type {
display: flex;
}
.info > .info-top > div > div:last-of-type > a {
border: 1px solid #3F5A93;
padding: 5px 15px;
font-size: 16px;
margin-right: 20px;
color: #3F5A93;
}
.info > .info-top > div > div:last-of-type > a:first-of-type {
background-color: red;
color: white;
border: 1px solid red;
}
.info > .info-top > div > div:last-of-type > a:first-of-type:hover {
background-color: #f64c59;
color: #eeeeee;
}
/*作品详情页信息*/
.info > .info-cont {
display: flex;
flex-direction: column;
}
.info > .info-cont > span:first-of-type {
margin-top: 20px;
border-bottom: 2px solid red;
font-size: 18px;
color: red;
width: 100px;
text-align: center;
padding: 10px 0;
}
.info > .info-cont > span:last-of-type {
border-top: 1px solid #888888;
margin-top: -1px;
padding: 20px;
}
模拟数据config.php
代码
<?php
//网站名字
$webName = '小说阅读手机版';
//图片路径
const PIC_PATH = 'static/';
//栏目导航
$catLists = [
['cid' => 1, 'name' => '都市小说', 'url' => 'list.php?cid=1'],
['cid' => 2, 'name' => '仙侠小说', 'url' => 'list.php?cid=2'],
];
//底部导航
$tabBars = [
['fid' => 1, 'name' => '书架', 'url' => 'tab_bar.php?fid=1', 'icon' => 'icon-shujia'],
['fid' => 2, 'name' => '精选', 'url' => 'tab_bar.php?fid=2', 'icon' => 'icon-jingxuan'],
['fid' => 3, 'name' => '发现', 'url' => 'tab_bar.php?fid=3', 'icon' => 'icon-faxian'],
['fid' => 4, 'name' => '我的', 'url' => 'tab_bar.php?fid=4', 'icon' => 'icon-wode'],
];
//小说列表
$bookLists = [
[
'id' => 1,
'cid' => 1,
'name' => '巨星从影视学院开始',
'pic' => 'images/juxing.jpg',
'url' => 'info.php?id=1',
'author' => '进击的咸鸭蛋',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '16.17',
'overall' => '8734',
'Weekly' => '2037',
'intro' => '简介:韩飞毫无预兆的穿越了,还没等他彻底消化原主的记忆,却要面对一场事关终生的考试——艺考!于是,他一把抢过旁边女同学的化妆镜,嗯,不错,很帅,那么艺考还是问题吗?',
],
[
'id' => 2,
'cid' => 1,
'name' => '演员请就位',
'pic' => 'images/yanyuan.jpg',
'url' => 'info.php?id=2',
'author' => '楼下赫本',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '6.69',
'overall' => '5870',
'Weekly' => '1105',
'intro' => '影帝许先莫名其妙来到了平行世界,成为了流量小生预备役节目《初代》中的一员。上辈子因为颜值问题挨够了社会的毒打,这辈子有了一副好皮囊,许先琢磨着,或许也可以尝试一下躺着赚钱…直到有人质疑他的演技。',
],
[
'id' => 3,
'cid' => 1,
'name' => '体验派影帝',
'pic' => 'images/tiyan.jpg',
'url' => 'info.php?id=3',
'author' => '黑色的单车',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '13.17',
'overall' => '19734',
'Weekly' => '5368',
'intro' => '当个演员是种什么体验?向阳,衡店大神兼职亚洲普通青年,重生了。这次他想好好体验体验。从《亮剑》开始,每一个角色,无论复杂还是简单,都是一段人生,都有苦辣酸甜。',
],
[
'id' => 4,
'cid' => 1,
'name' => '我有一座深山老林',
'pic' => 'images/woyou.jpg',
'url' => 'info.php?id=4',
'author' => '湖蛟',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '12.17',
'overall' => '7734',
'Weekly' => '2537',
'intro' => '韩冬自烹饪学院毕业后,拒绝了城里名企的工作机会。毅然回到老家,在照顾老人的同时,开了一家只在晚上营业的大排档。凭借精湛厨艺和独特美食,吸引无数人慕名前来。而他所有的食材,全都来自一片神秘山林……',
],
[
'id' => 5,
'cid' => 1,
'name' => '我是小先生',
'pic' => 'images/woshi.jpg',
'url' => 'info.php?id=5',
'author' => '九幽河上',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '19.17',
'overall' => '18734',
'Weekly' => '12037',
'intro' => '张奕做了一个梦,梦见自己在一家名叫德芸社的地方学了三年相声,又拜了个小黑胖子为师,在小剧场摸爬滚打了好几年终于要开个人专场了,梦醒了……',
],
[
'id' => 6,
'cid' => 1,
'name' => '海洋被我承包了',
'pic' => 'images/haiyang.jpg',
'url' => 'info.php?id=6',
'author' => '锦瑟华年',
'summary' => '重生过去、畅想未来、梦幻现实,再塑传奇人生!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '10.17',
'overall' => '0734',
'Weekly' => '545',
'intro' => '渔家孩子夏宇开启大禹传承后,开始踏上征服大海的旅途。美丽富饶的海洋是众多海洋动物的天堂,珊瑚播撒生命的种子,座头鲸为求偶翩翩起舞,大海深处回荡着爱情的呼唤,珊瑚礁里演绎着生命的传奇,斑驳的沉船变成鱼儿的家园,神秘的海底,蕴藏着鲜活的珍宝。夏宇遨游其中,品各色海鲜、打捞沉船、追寻海盗宝藏,享受精彩纷呈的海洋生活。',
],
[
'id' => 7,
'cid' => 2,
'name' => '聊斋世界修神通',
'pic' => 'images/liaozhai.jpg',
'url' => 'info.php?id=7',
'author' => '静坐讲黄庭',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '18.17',
'overall' => '11734',
'Weekly' => '545',
'intro' => '天罡三十六法,地煞七十二术,误入聊斋世界,仗剑神通无敌 。',
],
[
'id' => 8,
'cid' => 2,
'name' => '万界降临',
'pic' => 'images/wanjie.jpg',
'url' => 'info.php?id=8',
'author' => '紫青都帅',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '14.17',
'overall' => '6734',
'Weekly' => '1545',
'intro' => '当我们遭遇不顺的时候,盼望拥有法术让我们心想事成;当我们被人欺负时,梦想拥有超能力让敌人满地找牙;当我们遇到危险时候,期望拥有神奇力量化险为夷;当我们生病受伤时候,企盼能有灵丹妙药让人获得健康……当有一天,人类创作出来的小说、电影、游戏各个世界全都变现成真,我们获得了我们希求的力量和仙药法宝,解决了眼前的危机,但是里面的各路神仙妖魔也都纷纷降临到地球上……本书所有功法人物事件都是本人编的,全是假的!千万不要照着练,如有雷同,纯属巧合。',
],
[
'id' => 9,
'cid' => 2,
'name' => '封神萧升传',
'pic' => 'images/fengshen.jpg',
'url' => 'info.php?id=9',
'author' => '玄宗小道',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '17.17',
'overall' => '10734',
'Weekly' => '2545',
'intro' => '大道如川,百舸争流。不想做炮灰,那该怎么办?萧升说,既然选择不了出生,那就试图改变命运,毕竟,封神还很久……',
],
[
'id' => 10,
'cid' => 2,
'name' => '九叔世界当警察',
'pic' => 'images/jiushu.jpg',
'url' => 'info.php?id=10',
'author' => '九叔粉丝小白',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '19.17',
'overall' => '2734',
'Weekly' => '565',
'intro' => '方正东穿越了,成为一名民国警察。刚上任第一天,就听到了一阵熟悉的叫喊声。“快开门啊!我是你们队长阿威啊!”',
],
[
'id' => 11,
'cid' => 2,
'name' => '我想长生不死',
'pic' => 'images/woxiang.jpg',
'url' => 'info.php?id=11',
'author' => '周流星位',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '13.17',
'overall' => '2734',
'Weekly' => '618',
'intro' => '远古洪荒,天东有若木,钟山有赤龙衔烛,三皇与诸神称霸天地。三清尚无人听说,西方二人组也没什么消息,女娲、伏羲、太一、帝俊、十二祖巫等耳熟能详的大人物也都无人听闻。这是一个无尽古老的时代,距离道门炼气术开启仙路,才不过九万年。鸿钧还只是道尊,罗睺还只是魔皇。钟恒带着十二本“讲道笔记”来到了这样一个时代,理想不大,只想长生不死。长生易得,不死难成。如何不死?超越一切,方能不死!时代太早,劫数太多,低调修炼,苟字为先……',
],
[
'id' => 12,
'cid' => 2,
'name' => '西游之绝代凶蟾',
'pic' => 'images/xiyou.jpg',
'url' => 'info.php?id=12',
'author' => '贪玩的提莫',
'summary' => '修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!',
'status' => '连载',
'sign' => '签约',
'price' => '免费',
'word' => '28.17',
'overall' => '5734',
'Weekly' => '645',
'intro' => '重生在西游世界,老子竟然成了一只蛤蟆?灵感大王是我的小跟班,七大圣是我的结拜兄弟,',
]
];
总结
- 在静态转变动态过程中,把重复的代码交给数据循环来完成;
- 函数实现了代码的复用,大量相同的操作可以用调用一个函数来完成;
- 数据在网页之间跳转的参数传递,是字符串完成,在对比的时候要转换类型;
- 在调用函数的时候,传参的数据顺序要和定义函数的时候顺序一致;