Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:不论如何, 实现想要的效果是第一位的, 代码的优化不着急, 有了经验之后, 不给你说, 你也知道要从哪里入手来优化或简化了
namespace Model;
class Db
{
private $dbConfig = [
'type' => 'mysql',
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'root',
'dbname' => 'a_io',
];
public static $pdo =null;
//私有化构造方法,禁止外部调用
private function __construct()
{
$this->connect();
}
private function connect(){
try {
$dsn = "{$this->dbConfig['type']}:host={$this->dbConfig['host']};dbname={$this->dbConfig['dbname']}";
self::$pdo = new \PDO($dsn,$this->dbConfig['username'],$this->dbConfig['password']);
}catch (\PDOException $e){
die('数据库连接失败:' . $e->getMessage());
}
}
//单例模式
public static function connection()
{
if (is_null(self::$pdo)){
new self();
}
return self::$pdo;
}
//私有化克隆方法
private function __clone()
{
}
}
namespace Model;
require 'Db.php';
class Model
{
public $pdo = null;
//构造方法连接数据库
public function __construct()
{
$this->pdo = Db::connection();
}
public function getLimit($table,$limit = 1,$where = '')
{
if (!empty($where)){
$where =' WHERE ' . $where ;
}
$sql = "SELECT * FROM {$table} {$where} LIMIT {$limit}";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
// $stmt->debugDumpParams();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}
namespace View;
class View
{
public function getLimitData($data)
{
return $data;
}
}
namespace Controller;
require dirname(__DIR__) .'/common/autoload.php';
use Model\Model;
use View\View;
class Container
{
public $instance = [];
//存进去,绑定类实例方法
public function bind($alias,\Closure $process)
{
$this->instance[$alias] = $process;
}
//取出来,执行实例化过程
public function make($alias,$params = [])
{
return call_user_func_array($this->instance[$alias],$params);
}
}
$container = new Container();
$container->bind('model',function (){return new Model();});
$container->bind('view',function (){return new View();});
class Facade
{
//创建容器数据,接收实例化后的类
protected static $container = null;
//存储数据
protected static $data = [];
public static function initialize($container)
{
static::$container = $container;
}
//静态化模型方法
public static function getLimit($table,$limit = 1,$where = '')
{
static::$data = static::$container->make('model')->getLimit($table,$limit,$where);
}
//静态化视图方法
public static function getLimitData()
{
return static::$container->make('view')->getLimitData(static::$data);
}
}
class Controller
{
//初始化门面
public function __construct(Container $container)
{
Facade::initialize($container);
}
public function indexLimit($table,$limit = 1 ,$where = '')
{
//获取数据
Facade::getLimit($table,$limit,$where);
//渲染模板
return Facade::getLimitData();
}
}
//自动加载
spl_autoload_register(function ($className){
$path = str_replace('\\','/',$className);
require dirname(__DIR__) . '/' . $path . '.php';
});
<!--公共顶部导航区-->
<header>
<a href="">网站首页</a>
<a href="">专题</a>
<a href="">网站导航</a>
<a href="">二手商品</a>
<a href="">讨论区</a>
<span><a href=""><i class="iconfont icon-huiyuan2"></i>登陆</a><a href="">免费注册</a></span>
</header>
<style>
img { max-width: 100%;}
</style>
<footer>
<div>
<a href="">简介</a>
<a href="">联系我们</a>
<a href="">招聘信息</a>
<a href="">友情链接</a>
<a href="">用户服务协议</a>
<a href="">隐私权声明</a>
<a href="">法律投诉声明</a>
</div>
<div><span>LOGO</span></div>
<div>
<p>2019 fengniao.com. All rights reserved . 安徽闹着玩***(无聊网)版权所有</p>
<p>皖ICP证150110号 京ICP备14323013号-2 皖公网安备110108024357788号</p>
<p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>
</div>
<div>
<p>关注公众号</p>
<img src="static/images/erwei-code.png" alt="">
</div>
</footer>
<?php
require 'Controller\Controller.php';
//文章
$limit1 = $controller -> indexLimit('article',1);
$limit9 = $controller -> indexLimit('article','1,9');
//图片
$photo = $controller -> indexLimit('photo',2);
$photo1 = $controller -> indexLimit('photo','2,2');
$photo2 = $controller -> indexLimit('photo','4,2');
$photo3 = $controller -> indexLimit('photo','6,2');
$photo4 = $controller -> indexLimit('photo','8,2');
$photo5 = $controller -> indexLimit('photo','10,2');
//汽车
$shop = $controller -> indexLimit('shop',4);
$shop1 = $controller -> indexLimit('shop','4,4');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
<!-- <link rel="stylesheet" href="static/css/reset.css">-->
<link rel="stylesheet" href="static/font/iconfont.css">
<link rel="stylesheet" href="static/css/index.css">
</head>
<body>
<?php include 'common/head.php';?>
<!--logo+搜索框+快捷入口区-->
<div class="logo">
<img src="static/images/logo.png" alt="">
<label>
<input type="search">
<a href="" class="iconfont icon-jinduchaxun"></a>
</label>
<span>
<a href="" class="iconfont icon-huiyuan1"></a>
<a href="" class="iconfont icon-danmu"></a>
<a href="" class="iconfont icon-duoxuankuang"></a>
<a href="" class="iconfont icon-jishufuwu"></a>
<a href="" class="iconfont icon-peiwangyindao"></a>
<a href="" class="iconfont icon-wenjianjia"></a>
<a href="" class="iconfont icon-huiyuan1"></a>
</span>
</div>
<!--主导航区-->
<nav>
<div>
<span class="iconfont icon-gongdan"></span>
<span>资讯 <br> 看学</span>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
</div>
<div>
<span class="iconfont icon-renwujincheng"></span>
<span>资讯 <br> 看学</span>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
</div>
<div>
<span class="iconfont icon-gongdan"></span>
<span>资讯 <br> 看学</span>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
</div>
<div>
<span class="iconfont icon-DOC"></span>
<span>资讯 <br> 看学</span>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
<a href="">器材</a>
<a href="">大师</a>
<a href="">学院</a>
<a href="">影赛</a>
</div>
</nav>
<!--轮播图-->
<div class="slider">
<img src="static/images/1.jpg" alt="">
<img src="static/images/banner-right.jpg" alt="">
</div>
<!--新闻资讯区-->
<div class="news">
<div class="title">
<a>新闻资讯</a>
<a href="">更多</a>
</div>
<div class="content">
<div class="pic">
<a href=""><img src="static/images/news.jpg" alt="" class="first-img"></a>
<a href=""><img src="static/images/n-2.jpg" alt=""></a>
<a href=""><img src="static/images/n-3.jpg" alt=""></a>
<a href="">三星Note10/10+发布 <br> 搭载挖孔前摄</a>
<a href="">小米公布6400万 <br> 和1亿像素手机信息</a>
</div>
<div class="list">
<?php foreach($limit1 as $row) :?>
<a href="article.php?id=<?php echo $row['id'] ?>"><?php echo mb_substr($row['title'],0,15) ?></a>
<?php endforeach;?>
<ul> <?php foreach($limit9 as $row) :?>
<li><span>[新闻]</span><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="list">
<?php foreach ($limit1 as $row) : ?>
<a href="article.php?id=<?php echo $row['id']; ?>"><?php echo mb_substr($row['title'],0,15) ?></a>
<?php endforeach; ?>
<ul>
<?php foreach($limit9 as $row) :?>
<li><span>[新闻]</span><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
</div>
</div>
<!--图片专区-->
<div class="title">
<span>图片专区</span>
</div>
<div class="picture">
<div>
<div>
<a href="">自然风光</a>
<span>纵观摄影艺术</span>
</div>
<?php foreach ($photo as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
<?php foreach ($photo1 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo1 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
</div>
<div>
<div>
<a href="">自然风光</a>
<span>纵观摄影艺术</span>
</div>
<?php foreach ($photo2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
<?php foreach ($photo3 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo3 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
</div>
<div>
<div>
<a href="">自然风光</a>
<span>纵观摄影艺术</span>
</div>
<?php foreach ($photo4 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo4 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
<?php foreach ($photo5 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="162" height="122"></a>
<?php endforeach; ?>
<?php foreach ($photo5 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
</div>
</div>
<!--二手交易专区-->
<div class="title">
<span>汽车***</span>
</div>
<div class="second-hand">
<div>
<a href="">抢好货</a>
<span>0低价, 便捷,安全,快速</span>
</div>
<div>
<span>热门分类</span>
<a href="">国产新车</a>
<a href="">海外车讯</a>
<a href="">新车谍照</a>
<a href="">***新车</a>
<a href="">新车盘点</a>
</div>
<?php foreach ($shop as $item) : ?>
<a href="shop.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="176" height="120"></a>
<?php endforeach; ?>
<?php foreach ($shop as $item) : ?>
<div class="detail">
<a href="shop.php?id=<?php echo $item['id'] ?>"><?php echo $item['title'] ?></a>
<div>
<a href="shop.php?id=<?php echo $item['id'] ?>">
<span>¥ <?php echo $item['price'] ?>万</span>
<span>新车</span>
</a>
</div>
</div>
<?php endforeach; ?>
<?php foreach ($shop1 as $item) : ?>
<a href="shop.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="176" height="120"></a>
<?php endforeach; ?>
<?php foreach ($shop1 as $item) : ?>
<div class="detail">
<a href="shop.php?id=<?php echo $item['id'] ?>"><?php echo $item['title'] ?></a>
<div>
<a href="shop.php?id=<?php echo $item['id'] ?>">
<span>¥ <?php echo $item['price'] ?>万</span>
<span>新车</span>
</a>
</div>
</div>
<?php endforeach; ?>
<div>
<a href=""><img src="static/images/ad/1.png" alt="" width="180" height="112"></a>
<a href=""><img src="static/images/ad/2.png" alt="" width="180" height="112"></a>
<a href=""><img src="static/images/ad/3.png" alt="" width="180" height="112"></a>
<a href=""><img src="static/images/ad/4.png" alt="" width="180" height="112"></a>
<a href=""><img src="static/images/ad/image.png" alt="" width="393" height="56"></a>
<a href=""><img src="static/images/ad/ad2.jpg" alt="" width="393" height="56"></a>
</div>
</div>
<!--合作网站-->
<div class="title" style="background:#fff">
<span>合作网站</span>
</div>
<div class="my-links">
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.php.cn">php中文网</a>
<a href="https://www.html.cn">html中文网</a>
<a href="https://www.py.cn">python中文网</a>
<a href="https://www.py.cn">python中文网</a>
</div>
<!--页底部-->
<?php include 'common/footer.php';?>
</body>
</html>
<?php
require 'Controller\Controller.php';
$id = isset($_GET['id']) ? $_GET['id'] : 0 ;
if ($id == 0){
echo "<script>alert('ID不存在!'); location.assign('index.php');</script>";
}
$list = $controller->indexLimit('article', 10 ,"listid=$id" );
$limit6= $controller -> indexLimit('article',6);
$limit6_2 = $controller -> indexLimit('article','6,6');
$limit4= $controller -> indexLimit('article',4);
$limit4_2 = $controller -> indexLimit('photo',4);
function tranTime($time)
{
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60)
{
$str = '刚刚';
}
elseif ($time < 60 * 60)
{
$min = floor($time/60);
$str = $min.'分钟前';
}
elseif ($time < 60 * 60 * 24)
{
$h = floor($time/(60*60));
$str = $h.'小时前 ';
}
elseif ($time < 60 * 60 * 24 * 3)
{
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天 '.$rtime;
else
$str = '前天 '.$rtime;
}
else
{
$str = $rtime;
}
return $str;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章详情页面</title>
<link rel="stylesheet" href="static/font/iconfont.css">
<link rel="stylesheet" href="static/css/article-list.css">
</head>
<body>
<!--公共顶部导航区-->
<?php include 'common/head.php';?>
<div class="main">
<div class="top">
<img src="static/images/ar-logo.png" alt="">
<a href="">财经</a>><span>正文</span>
<label><input type="search"><span class="iconfont icon-sousuo2"></span></label>
</div>
<!-- 列表-->
<article>
<div>
<a href="">头条</a>
<a href="">热文</a>
<a href="">直播</a>
<a href="" id="active">新闻</a>
<a href="">政策地图</a>
<a href="">相对论</a>
<a href="">人物</a>
<a href="">行情</a>
<a href="">投研</a>
<a href="">技术</a>
<a href="">百科</a>
</div>
<?php foreach ($list as $item) : ?>
<div class="list1">
<a href="article.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="272"></a>
<div>
<a href="article.php?id=<?php echo $item['id'] ?>"><?php echo $item['title'] ?></a>
<span><?php echo $item['title'] ?></span>
</div>
<a href="article.php?id=<?php echo $item['id'] ?>">区块链头条 · <?php echo tranTime($item['pubdate']) ?></a>
<span><i class="iconfont icon-icon_yulan"></i><?php echo $item['read'] ?></span>
</div>
<?php endforeach; ?>
</article>
<!-- 右侧列表-->
<div class="list1">
<h3>网页评论</h3>
<ul>
<?php foreach($limit6 as $row) :?>
<li><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="list2">
<h3>网页评论</h3>
<ul>
<?php foreach($limit6_2 as $row) :?>
<li><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="recommend">
<h3>推荐阅读</h3>
<?php foreach ($limit4 as $item) : ?>
<a href="article.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="195" height="130"></a>
<?php endforeach; ?>
<?php foreach ($limit4 as $item) : ?>
<a href="article.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
<?php foreach ($limit4_2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="195" height="130"></a>
<?php endforeach; ?>
<?php foreach ($limit4_2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
</div>
</div>
<!--页底部-->
<?php include 'common/footer.php';?>
</body>
</html>
<?php
require 'Controller\Controller.php';
$id = isset($_GET['id']) ? $_GET['id'] : 0 ;
if ($id == 0){
echo "<script>alert('ID不存在!'); location.assign('index.php');</script>";
}
$article = $controller->indexLimit('article', 1 ,"id=$id" );
$article = $article[0];
$limit6= $controller -> indexLimit('article',6);
$limit6_2 = $controller -> indexLimit('article','6,6');
$limit4= $controller -> indexLimit('article',4);
$limit4_2 = $controller -> indexLimit('photo',4);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $article['title'] ?></title>
<link rel="stylesheet" href="static/font/iconfont.css">
<link rel="stylesheet" href="static/css/article.css">
</head>
<body>
<!--公共顶部导航区-->
<?php include 'common/head.php'; ?>
<div class="main">
<div class="top">
<img src="static/images/ar-logo.png" alt="">
<a href="">财经</a>><span>正文</span>
<label><input type="search"><span class="iconfont icon-sousuo2"></span></label>
</div>
<!-- 正文-->
<article>
<h1><?php echo $article['title'] ?></h1>
<div>
<span>发布时间:<?php echo date('Y-m-d',$article['pubdate']) ?></span>
<span>来源:<?php echo $article['source'] ?></span>
<span>阅读量:<?php echo $article['read'] ?></span>
<span>评论数:1545</span>
</div>
<div>
<?php echo $article['body'] ?>
</div>
</article>
<!-- 右侧列表-->
<div class="list1">
<h3>网页评论</h3>
<ul>
<?php foreach($limit6 as $row) :?>
<li><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="list2">
<h3>网页评论</h3>
<ul>
<?php foreach($limit6_2 as $row) :?>
<li><a href="article.php?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="ding">
<span>赞</span>
<span>踩</span>
</div>
<div class="comment">
<h3>网页评论</h3>
<img src="static/images/user.png" alt="" width="60">
<textarea name="" id="" cols="30" rows="10"></textarea>
<button>发表评论</button>
</div>
<div class="recommend">
<h3>推荐阅读</h3>
<?php foreach ($limit4 as $item) : ?>
<a href="article.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="195" height="130"></a>
<?php endforeach; ?>
<?php foreach ($limit4 as $item) : ?>
<a href="article.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
<?php foreach ($limit4_2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><img src="<?php echo $item['litpic'] ?>" alt="" width="195" height="130"></a>
<?php endforeach; ?>
<?php foreach ($limit4_2 as $item) : ?>
<a href="photo.php?id=<?php echo $item['id'] ?>"><span><?php echo $item['title'] ?></span></a>
<?php endforeach; ?>
</div>
</div>
<!--页底部-->
<?php include 'common/footer.php';?>
</body>
</html>
<a href='article.php?id=1'>点击传值</a>
传值,$_GET['id']
接收。MVC
流程,虽然写的时候还是不断的出现各种错误,现在可以找到问题并解决了。