Blogger Information
Blog 22
fans 1
comment 0
visits 20154
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
寒假作业1-将首页按照mvc的模式动态化-php培训十期线上班
Dseven
Original
911 people have browsed it

1.作业要求

将老师提供的静态首页页面,用MVC的模式进行动态改写。

2.完成步骤

2.1分析页面建立数据库

通过页面分析,将整个页面的数据动态表现出来,需要建立10张数据表

商品信息表

主导航分类表

主导航详表

新闻详表

新闻分类表

页头、页脚导航表

合作导航表

图片站详情表

图片站分类表

二手交易分类表

2.1链接数据库

2.1.1在model脚本中建立数据库操作类

  1. class Data
  2. {
  3. private $dsn = 'mysql:host=localhost;dbname=test;';
  4. private $username = 'root1';
  5. private $password = 'root123';
  6. private $tablenames;
  7. public function __construct($tablenames)
  8. {
  9. $this->tablenames = $tablenames;
  10. }
  11. public function getdata()
  12. {
  13. $pdo = new PDO($this->dsn, $this->username, $this->password);
  14. foreach ($this->tablenames as $key => $value) {
  15. $data = $pdo->prepare('SELECT * FROM ' . $value . ';');
  16. $data->execute();
  17. // $data = $pdo->query('SELECT * FROM '.$value.';');
  18. $data->setFetchMode(PDO::FETCH_ASSOC);
  19. $datas[$value] = $data->fetchAll();
  20. }
  21. return $datas;
  22. }
  23. }

2.2改写页面

改写的总体思路是,将每一个板块看成一个类,定义一个渲染函数,然后用拼接的语法进行输出
公共页头类

  1. class Pheader
  2. {
  3. public function fetch($links){
  4. $pheader = '<nav class="public-header">';
  5. $pheader .='<a href="">网站首页</a>';
  6. foreach ($links as $link){
  7. if($link['position']==='header')
  8. $pheader .= '<a href="">'.$link['name'].'</a>';
  9. }
  10. $pheader .= '<span>';
  11. $pheader .= '<a href=""><i class="iconfont icon-huiyuan2"></i>登陆</a>';
  12. $pheader .= '<a href="">免费注册</a>';
  13. $pheader .= '</span>';
  14. $pheader .= '</nav>';
  15. return $pheader;
  16. }
  17. }

主导航类

  1. class Mainnav
  2. {
  3. public function fetch($mainnav,$navlist){
  4. $mn = '<div class="index-header">';
  5. $mn .= '<div class="content">';
  6. $mn .= '<div class="log-search">';
  7. $mn .= '<a href="" class="logo"><img src="../static/images/logo.png" alt=""></a>';
  8. $mn .= '<div class="search">';
  9. $mn .= '<input type="search" name="search" id="search">';
  10. $mn .= '<label for="search" class="iconfont icon-jinduchaxun"></label>';
  11. $mn .= '</div>';
  12. $mn .= '<div class="quick-entry">';
  13. $mn .= '<a href="" class="iconfont icon-huiyuan1"></a>';
  14. $mn .= '<a href="" class="iconfont icon-danmu1"></a>';
  15. $mn .= '<a href="" class="iconfont icon-fabu"></a>';
  16. $mn .= '<a href="" class="iconfont icon-fangda"></a>';
  17. $mn .= '<a href="" class="iconfont icon-huiyuan2"></a>';
  18. $mn .= '<a href="" class="iconfont icon-dianzan"></a>';
  19. $mn .= '</div>';
  20. $mn .= '</div>';
  21. $mn .= '<div class="main-nav">';
  22. foreach($mainnav as $m){
  23. $mn .= '<div class="nav-detail">';
  24. $mn .= '<div class="pic">';
  25. $mn .= '<span class="'.$m['pic'].'"></span>';
  26. $mn .= '<div>';
  27. $mn .= '<span>'.mb_substr($m['name'],0,2).'</span>';
  28. $mn .= '<span>'.mb_substr($m['name'],2,2).'</span>';
  29. $mn .= '</div>';
  30. $mn .= '</div>';
  31. $mn .= '<div class="links">';
  32. foreach($navlist as $l){
  33. if($l['nid']===$m['nid'])
  34. $mn .='<a href=" ">'.$l['name'].'</a>';
  35. }
  36. $mn .= '</div>';
  37. $mn .= '</div>';
  38. }
  39. $mn .= '</div>';
  40. $mn .= '<div class="slider">';
  41. $mn .= '<a href=""><img src="../static/images/2.jpg" alt=""></a>';
  42. $mn .= '<a href=""><img src="../static/images/banner-right.jpg" alt=""></a>';
  43. $mn .= '</div>';
  44. $mn .= '</div>';
  45. $mn .= '</div>';
  46. return $mn;
  47. }
  48. }

新闻类

  1. class News{
  2. public function fetch($newstypes,$newsinfos){
  3. $news = '<div class="public-headline">';
  4. $news .= '<span>新闻资讯</span>';
  5. $news .= '</div>';
  6. $news .= '<div class="index-news">';
  7. $news .= '<div class="imgs">';
  8. $news .= '<a href=""><img src="../static/images/news.jpg" alt="" class="first-img"></a>';
  9. $news .= '<div>';
  10. $news .= '<a href=""><img src="../static/images/n-2.jpg" alt=""></a>';
  11. $news .= '<a href="">三星Note10发布搭载挖孔前摄</a>';
  12. $news .= '</div>';
  13. $news .= '<div>';
  14. $news .= '<a href=""><img src="../static/images/n-3.jpg" alt=""></a>';
  15. $news .= '<a href="">小米公布1亿像素手机信息</a>';
  16. $news .= '</div>';
  17. $news .= '</div>';
  18. foreach ($newstypes as $newstype){
  19. $news .= '<div class="list">';
  20. $news .= '<a href="">'.$newstype['title'].'</a>';
  21. if($newstype['nid']==1){
  22. $news .= '<ul>';
  23. foreach($newsinfos as $newsinfo){
  24. if(intval($newsinfo['type'])===1){
  25. $news .= '<li><span>';
  26. $news .='[新闻]';
  27. $news .= '</span>';
  28. $news .= '<a href="">';
  29. $news .= $newsinfo['title'];
  30. $news .= '</a></li>';
  31. }
  32. }
  33. }else{
  34. foreach($newsinfos as $newsinfo){
  35. if(intval($newsinfo['type'])===2){
  36. $news .= '<li><span>';
  37. $news .='[促销]';
  38. $news .= '</span>';
  39. $news .= '<a href="">';
  40. $news .= $newsinfo['title'];
  41. $news .= '</a></li>';
  42. }
  43. }
  44. }
  45. $news .= '</ul>';
  46. $news .= '</div>';
  47. }
  48. $news .= '</div>';
  49. return $news;
  50. }
  51. }

图片类

  1. class Pic{
  2. public function fetch($picnavs,$picinfos){
  3. $pic ='<div class="public-headline">';
  4. $pic .= '<span>图片专区</span>';
  5. $pic .= '</div>';
  6. $pic .= '<div class="public-image-list">';
  7. foreach ($picnavs as $picnav){
  8. $pic .= '<div class="list">';
  9. $pic .= '<div class="title">';
  10. $pic .= '<a href="">';
  11. $pic .= $picnav['name'];
  12. $pic .= '</a>';
  13. $pic .= '<span>纵观摄影艺术</span>';
  14. $pic .= '</div>';
  15. $pic .= '<div class="img-list">';
  16. foreach($picinfos as $picinfo){
  17. if ($picinfo['type'] === $picnav['pid']){
  18. $pic .= '<div>';
  19. $pic .= '<a href=""><img src="../static/images/'.$picinfo['link'].'" alt=""></a>';
  20. $pic .= '<a href="">'.$picinfo['info'].'</a>';
  21. $pic .= '</div>';
  22. }
  23. }
  24. $pic .= '</div>';
  25. $pic .= '</div>';
  26. }
  27. $pic .= '</div>';
  28. return $pic;
  29. }
  30. }

二手交易类

  1. class Shop{
  2. public function fetch($shopnavs,$goodsinfos){
  3. $shop = '<div class="public-headline">';
  4. $shop .= '<span>二手交易</span>';
  5. $shop .= '</div>';
  6. $shop .= '<div class="public-second-hand">';
  7. $shop .= '<div class="title1">';
  8. $shop .= '<a href="">抢好货</a>';
  9. $shop .= '<span>0低价, 便捷,安全,快速</span>';
  10. $shop .= '</div>';
  11. $shop .= '<div class="title2">';
  12. $shop .= '<span>热门分类</span>';
  13. foreach ($shopnavs as $shopnav){
  14. $shop .= '<a href="">'.$shopnav['name'].'</a>';
  15. }
  16. $shop .= '</div>';
  17. $shop .= '<div class="goods">';
  18. $shop .= '<div class="goods-list">';
  19. foreach ($goodsinfos as $goodsinfo){
  20. $shop .= '<div class="intro">';
  21. $shop .= '<a href="'.$goodsinfo['link'].'">';
  22. $shop .= '<img src="../static/images/shop/'.$goodsinfo['imglink'].'" alt="" width="176" height="120">';
  23. $shop .= '</a>';
  24. $shop .= '<a href="">'.$goodsinfo['info'].'</a>';
  25. $shop .= '<div>';
  26. $shop .= '<span>¥ '.$goodsinfo['price'].'</span>';
  27. $shop .= '<span>'.$goodsinfo['flag'].'</span>';
  28. $shop .= '</div>';
  29. $shop .= '</div>';
  30. }
  31. $shop .= '</div>';
  32. $shop .= '<div class="quick-entry">';
  33. $shop .= '<a href=""><img src="../static/images/ad/1.png" alt="" ></a>';
  34. $shop .= '<a href=""><img src="../static/images/ad/2.png" alt=""></a>';
  35. $shop .= '<a href=""><img src="../static/images/ad/3.png" alt=""></a>';
  36. $shop .= '<a href=""><img src="../static/images/ad/4.png" alt=""></a>';
  37. $shop .= '<div>';
  38. $shop .= '<a href=""><img src="../static/images/ad/image.png" alt=""></a>';
  39. $shop .= '<a href=""><img src="../static/images/ad/ad2.jpg" alt=""></a>';
  40. $shop .= '</div>';
  41. $shop .= '</div>';
  42. $shop .= '</div>';
  43. $shop .= '</div>';
  44. return $shop;
  45. }
  46. }

合作单位类

  1. class Partner{
  2. public function fetch($partners){
  3. $par = '<div class="public-headline">';
  4. $par .= '<span>合作单位</span>';
  5. $par .= '</div>';
  6. $par .= '<div class="index-frend-links">';
  7. for($i = 0;$i<26;$i++){
  8. $n = array_rand($partners);
  9. $par .= '<a href="'.$partners[$n]['link'].'">'.$partners[$n]['name'].'</a>';
  10. }
  11. $par .= '</div>';
  12. return $par;
  13. }
  14. }

公共页脚类

  1. class Pfooter{
  2. public function fetch($pagenavs){
  3. $pfooter = '<footer class="public-footer">';
  4. $pfooter .= '<div>';
  5. foreach($pagenavs as $pagenav){
  6. if($pagenav['position']==='footer')
  7. $pfooter .= '<a href="'.$pagenav['link'].'">'.$pagenav['name'].'</a>';
  8. }
  9. $pfooter .= '</div>';
  10. $pfooter .= '<div><span>LOGO</span></div>';
  11. $pfooter .= '<div>';
  12. $pfooter .= '<p>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</p>';
  13. $pfooter .= '<p>皖ICP证150110号 京ICP备14323013号-2 皖公网安备110108024357788号</p>';
  14. $pfooter .= '<p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>';
  15. $pfooter .= '</div>';
  16. $pfooter .= '<div>';
  17. $pfooter .= '<p>关注公众号</p>';
  18. $pfooter .= '<img src="../static/images/erwei-code.png" alt="">';
  19. $pfooter .= '</div>';
  20. $pfooter .= '</footer>';
  21. return $pfooter;
  22. }
  23. }

整页显示类

  1. class View
  2. {
  3. private $pheader;
  4. private $mainnav;
  5. private $news;
  6. private $pic;
  7. private $shop;
  8. private $par;
  9. private $pfooter;
  10. public function fetch($datas)
  11. {
  12. $this->pheader = new Pheader;
  13. $this->mainnav = new Mainnav;
  14. $this->news = new News;
  15. $this->pic = new Pic;
  16. $this->shop = new Shop;
  17. $this->par = new Partner;
  18. $this->pfooter = new Pfooter;
  19. $header = '<!DOCTYPE html>
  20. <html lang="en">
  21. <head>
  22. <meta charset="UTF-8">
  23. <!-- 当前文档要用到阿里字体图标-->
  24. <link rel="stylesheet" href="../static/font/iconfont.css">
  25. <link rel="stylesheet" href="index.css">
  26. <title>全站首页</title>
  27. </head>
  28. <body>
  29. <main>';
  30. $footer = ' </main>
  31. </body>
  32. </html>';
  33. $index = $header;
  34. $index .= $this->pheader->fetch($datas['pagenav']);
  35. $index .= $this->mainnav->fetch($datas['mainnav'], $datas['navlist']);
  36. $index .= $this->news->fetch($datas['newstype'], $datas['newsinfo']);
  37. $index .= $this->pic->fetch($datas['picnav'], $datas['picinfo']);
  38. $index .= $this->shop->fetch($datas['shopnav'], $datas['goodsinfo']);
  39. $index .= $this->par->fetch($datas['partner']);
  40. $index .= $this->pfooter->fetch($datas['pagenav']);
  41. $index .= $footer;
  42. return $index;
  43. }
  44. }

2.3完成控制脚本

  1. namespace test;
  2. include 'model.php';
  3. include 'view.php';
  4. class Index{
  5. private $model;
  6. private $view;
  7. public function __construct($model,$view)
  8. {
  9. $this->model = $model;
  10. $this->view = $view;
  11. }
  12. public function fetch(){
  13. $datas = $this->model->getdata();
  14. return $this->view->fetch($datas);
  15. }
  16. }
  17. $arr = [
  18. 'pagenav',
  19. 'mainnav',
  20. 'navlist',
  21. 'newstype',
  22. 'newsinfo',
  23. 'picnav',
  24. 'picinfo',
  25. 'shopnav',
  26. 'goodsinfo',
  27. 'partner'
  28. ];
  29. $model = new Data($arr);
  30. $view = new View;
  31. $index = new Index($model,$view);
  32. echo $index->fetch();

3.作业总结

3.1这次作业是在懵懵懂懂中完成的,开始作业前一头雾水,不知道从哪里开始,但是在改写完一两个view页面的类后,基本找到了方法,也初步体会到老师html代码的结构为什么那么写。

3.2操作数据库取出数据遇到了一些小问题,一开始用query的方法取出的是一个对象,需要再转换成数组,后来通过查手册,找到了一次性取出数据集的fetchAll方法,该方法返回的是一个数组。

3.3在改写页面时从何处介入foreach循环很重要,头脑不清醒很容易出错。

3.4变量、函数、类的命名还没有养成好的习惯。

3.5单页面的控制器比较简单,整站的应该会复杂很多,还需要慢慢摸索。

3.6存在的一个疑问,就是将静态网站动态化主要是要将哪些内容进行动态显示???

4.最后放一张效果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:其实大量的html代码,可以使用模板标签来做
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post