一、页面布局思路:把头部和尾部或其他相同的模块抽象为公共模板
header.php
头部代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网后台管理系统</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="static/css/style.css?ddd" media="all">
<link rel="stylesheet" href="static/css/home.css?ddd" media="all">
<script src="static/js/jquery3.4.1.js"></script>
</head>
<body>
<!--网站头部开始-->
<div class="home-top phpcn-clear">
<ul class='phpcn-col-md10'>
<li >
<a href="index.php">网站首页</a>
</li>
<li>
<a href="articles.php">新闻资讯</a>
</li>
<li>
<a href="pictures.php">图片专区</a>
</li>
<li>
<a href="shop.php">商品专区</a>
</li>
</ul>
<dl class='phpcn-col-md2'>
<dd>
<a href="">免费注册</a>
</dd>
<dd>
<a href=""><i class="phpcn-icon phpcn-icon-huiyuan2"></i>登陆</a>
</dd>
</dl>
</div>
<!--网站头部结束-->
footer.php
尾部代码:
<!--网站底部开始-->
<div class="phpcn-clear phpcn-mt-30 footer">
<div class="phpcn-main">
<div class="phpcn-col-md8">
<div class="link phpcn-mb-30">
<a href="" target="_blank">简介</a>
<a href="" target="_blank">联系我们</a>
<a href="" target="_blank">友情链接</a>
<a href="" target="_blank">招聘信息</a>
<a href="" target="_blank">用户服务协议</a>
<a href="" target="_blank">隐私权声明</a>
<a href="" target="_blank">法律投诉声明</a>
</div>
<div class="phpcn-col-md2 f-logo">
<img src="static/images/logo2.png" style="width: 120px;height: auto;margin-left: 0px">
</div>
<div class="phpcn-col-md10">
<P>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</P>
<P><span>皖ICP证150110号 京ICP备14323013号-2</span> <span>皖公网安备110108024357788号</span></P>
<P><span>违法和不良信息举报电话: 0551-1234567</span> <span>举报邮箱: admin@baidu.com</span></P>
</div>
</div>
<div class="phpcn-col-md4">
<h4>关注公众号</h4>
<img src="static/images/erwei-code.png">
</div>
</div>
</div>
<!--网站底部结束-->
<!-- js文件开始 -->
<script src="static/js/pin.js"></script>
<script...> 其他各种js文件
<!-- js文件结束 -->
</body>
</html>
二、页面布局格式:引入公共模板
<!-- 头部 -->
<?php include 'common/header.php' ?>
<!-- 主体内容 -->
<main>
内容...
</main>
<!-- 尾部 -->
<?php include 'common/header.php' ?>
三、获取数据
例:Article.php
代码 与 Shop.php、Picture.php 方法相同
class Article
{
private $pdo; // pdo实例
/**
* Picture constructor.
* @param $pdo 对象
*/
public function __construct($pdo)
{
$this->pdo = $pdo;
}
// 获取单条数据
public function view($id)
{
$sql = 'select * from `articles` where id=:id';
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
$stmt->setFetchMode(PDO::FETCH_CLASS, Article::class);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return $data;
}
// 获取多条数据
public function getLimit($count)
{
$sql = 'select * from `articles` limit :offset';
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam('offset', $count, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $data;
}
// 获取所有数据
public function getData()
{
$sql = 'select * from `articles`';
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $data;
}
}
数据库 articles
表:
四、页面数据导入并渲染
例:articles.php
新闻页面,shop.php
pictures.php
方式相同,代码略…
<?php
require 'db/db.php';
require 'model/Article.php';
// 获取数据
$article = new Article($pdo);
$list = $article->getData();
$limit6 = $article->getLimit(6);
?>
<!--网站头部-->
<?php include 'common/header.php'; ?>
<!--网站主体-->
<div class="ar-head phpcn-main">
<div class='phpcn-col-md10 path'>
<a href=""><img src="static/images/ar-logo.png"></a>
<a href="">财经</a>
<span>></span>
<span>列表</span>
</div>
<div class='phpcn-col-md2'>
<input type="" name=""> <i class="phpcn-icon phpcn-icon-sousuo2" placeholder='关键字搜索'></i>
</div>
<div class="phpcn-clear">
<div class="phpcn-col-md9">
<div class="article-content">
<!--列表开始-->
<div class="alist">
<div class="aritcle-list">
<?php
foreach ($list as $item) {
$id = $item['id'];
$article = '';
$article .= '<div class="phpcn-clear phpcn-mt-30">';
$article .= '<div class="phpcn-col-md4"><img src="' . $item['img_url'] . '"></div>';
$article .= '<div class="phpcn-col-md8">';
$article .= '<h2><a href="article-info.php?id='.$id.'">' . $item['title'] . '</a></h2>';
$article .= '<div class="info phpcn-mt-10">' . $item['title'] . '</div>';
$article .= '<div>';
$article .= '<a href="">环球时事: ' . $item['create_dt'] . '</a>';
$article .= '<span><i class="phpcn-icon phpcn-icon-icon_yulan phpcn-r phpcn-mr-20">' . $item['view'] . '</i></span>';
$article .= '</div>';
$article .= '</div>';
$article .= '</div>';
echo $article;
}
?>
</div>
</div>
<!--列表开始结束-->
<!--推荐阅读 --->
<div class="about-read phpcn-mt-30 phpcn-clear">
<div class="title"><span>推荐阅读</span></div>
<?php include 'common/recommond.php'; ?>
</div>
<!--推荐阅读 结束--->
</div>
</div>
<div class="phpcn-col-md3">
<div class="hot-article">
<div class="title"><span>环球时事</span></div>
<ul>
<?php
foreach ($limit6 as $item) {
$id = $item['id'];
$card = '';
$card .= '<li style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden">';
if ($id <= 3) {
$card .= '<span class="hot">' . $id . '</span>';
} else {
$card .= '<span>' . $id . '</span>';
}
$card .= '<a href="article-info.php?id=' . $id . '">' . $item['title'] . '</a>';
$card .= '</li>';
echo $card;
}
?>
</ul>
</div>
<div class="hot-article phpcn-mt-30">
<div class="title"><span>环球业界</span></div>
<ul>
<?php
foreach ($limit6 as $item) {
$id = $item['id'];
$card = '';
$card .= '<li style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden">';
if ($id <= 3) {
$card .= '<span class="hot">' . $id . '</span>';
} else {
$card .= '<span>' . $id . '</span>';
}
$card .= '<a href="article-info.php?id=' . $id . '">' . $item['title'] . '</a>';
$card .= '</li>';
echo $card;
}
?>
</ul>
</div>
</div>
</div>
</div>
<!--网站底部-->
<?php include 'common/footer.php'; ?>
代码效果:
article-info.php
文章详情页面:
<?php
require 'db/db.php';
require 'model/Article.php';
//接收新闻页面传过来的 id
$id = intval($_GET['id'] ?? 1);
$article = new Article($pdo);
$info = $article->view($id);
$list = $article->getData();
$limit6 = $article->getLimit(6);
?>
<!-- 头部 -->
<?php include 'common/header.php'; ?>
<!--新闻内容头部分类-->
<div class="ar-head phpcn-main">
<div class='phpcn-col-md10 path'>
<a href=""><img src="static/images/ar-logo.png"></a>
<a href="">财经</a>
<span>></span>
<span>正文</span>
</div>
<div class='phpcn-col-md2'>
<input type="" name=""> <i class="phpcn-icon phpcn-icon-sousuo2" placeholder='关键字搜索'></i>
</div>
<div class="phpcn-clear">
<div class="phpcn-col-md9">
<div class="article-content">
<h1><?php echo $info['title']; ?></h1>
<div class="attribute">
<span>发布时间:<?php echo $info['create_dt']; ?></span>
<span>来源:<?php echo $info['author']; ?></span>
<span>阅读量:<?php echo $info['view']; ?></span>
<span>评论数:<?php echo $info['comment']; ?></span>
</div>
<article>
<?php echo $info['content']; ?>
</article>
<div class="suggest">
<button class="phpcn-button phpcn-bg-red phpcn-button-hover">赞</button>
<button class="phpcn-button phpcn-color-grayphpcn-button-hover">踩</button>
</div>
<!--评论--->
<div class="comment phpcn-mt-30">
<div class="title phpcn-mb-30"><span>网页评论</span></div>
<div class="phpcn-clear">
<div class="phpcn-col-md1"><img class="user" src="static/images/user.png"></div>
<div class="phpcn-col-md11">
<textarea>
我来评论两句
</textarea>
<button class="phpcn-button phpcn-bg-red phpcn-button-hover phpcn-mt-10 phpcn-mb-10 phpcn-r">
发表评论
</button>
</div>
</div>
</div>
<!--评论结束--->
<!--推荐阅读 --->
<div class="about-read phpcn-mt-30">
<div class="title"><span>推荐阅读</span></div>
<?php include 'common/recommond.php'; ?>
</div>
<!--推荐阅读 结束--->
</div>
</div>
<div class="phpcn-col-md3">
<div class="hot-article">
<div class="title"><span>环球时事</span></div>
<ul>
<?php
foreach ($limit6 as $item) {
$id = $item['id'];
$card = '';
$card .= '<li style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden">';
if ($id <= 3) {
$card .= '<span class="hot">' . $id . '</span>';
} else {
$card .= '<span>' . $id . '</span>';
}
$card .= '<a href="article-info.php?id=' . $id . '">' . $item['title'] . '</a>';
$card .= '</li>';
echo $card;
}
?>
</ul>
</div>
<div class="hot-article phpcn-mt-30">
<div class="title"><span>环球业界</span></div>
<ul>
<?php
foreach ($limit6 as $item) {
$id = $item['id'];
$card = '';
$card .= '<li style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden">';
if ($id <= 3) {
$card .= '<span class="hot">' . $id . '</span>';
} else {
$card .= '<span>' . $id . '</span>';
}
$card .= '<a href="article-info.php?id=' . $id . '">' . $item['title'] . '</a>';
$card .= '</li>';
echo $card;
}
?>
</ul>
</div>
</div>
</div>
</div>
<!--新闻内容头部分类结束-->
<!--网站底部-->
<?php include 'common/footer.php'; ?>
新闻详情:http://demo.com/fniao/article-info.php?id=1
新闻详情:http://demo.com/fniao/article-info.php?id=2
其他页面代码略,案例代码目录:
首页:
图片页:
商品页:
课堂总结:
1、界面传值:<a href='target.php?id=10'>点击传值</a>
,接收值:$id = intval($_GET['id'] ?? 1);
,intval()
: 将值转化为int
类型;
2、开发中布局相同的页面,即可抽象出来,在需要的页面直接引入,降低耦合,提高代码复用。
THE END !