博客列表 >大周作业:mvc做网站,会员登录setcookie

大周作业:mvc做网站,会员登录setcookie

张浩刚
张浩刚原创
2019年12月13日 22:28:431083浏览

上图






数据库model.php

  1. <?php
  2. namespace config;
  3. use PDO;
  4. class Model{
  5. //pdo
  6. private static $pdo;
  7. //数据表
  8. private static $tabel;
  9. public function __construct($dsn='mysql:host=localhost;dbname=film',$user='root',$password='root')
  10. {
  11. static::$pdo = new PDO($dsn,$user,$password);
  12. }
  13. //获取栏目表数据
  14. private static function list($table='listinfo')
  15. {
  16. static::$tabel = $table;
  17. $stmt = static::$pdo->prepare('SELECT * FROM ' . static::$tabel);
  18. $stmt -> execute();
  19. return $stmt -> fetchAll(PDO::FETCH_ASSOC);
  20. }
  21. //获取新闻内容页数据表数据
  22. private static function article($table='article')
  23. {
  24. static::$tabel = $table;
  25. $stmt = static::$pdo->prepare('SELECT * FROM ' . static::$tabel);
  26. $stmt -> execute();
  27. return $stmt -> fetchAll(PDO::FETCH_ASSOC);
  28. }
  29. //获取商品内容页表数据
  30. private static function shop($table='shop')
  31. {
  32. static::$tabel = $table;
  33. $stmt = static::$pdo->prepare('SELECT * FROM ' . static::$tabel);
  34. $stmt -> execute();
  35. return $stmt -> fetchAll(PDO::FETCH_ASSOC);
  36. }
  37. //所有数据整合到一个类方法中,方便mvc调用
  38. public function getDate()
  39. {
  40. return [
  41. 'list' => static::list(),
  42. 'article' => static::article(),
  43. 'shop' => static::shop()
  44. ];
  45. }
  46. }

控制器

  1. <?php
  2. use \view\index;
  3. use \config\Model;
  4. use \view\article;
  5. use \view\footer;
  6. use \view\header;
  7. use \view\list_article;
  8. use \view\list_shop;
  9. use \view\shop;
  10. require __DIR__ . '/Autoload.php';
  11. //容器类
  12. class Container
  13. {
  14. //闭包数组
  15. private static $binds = [];
  16. //属性数组
  17. private static $instance = [];
  18. //依赖类或属性绑定到容器
  19. public function bind($abstract,$concrete)
  20. {
  21. if($concrete instanceof Closure){
  22. static::$binds[$abstract] = $concrete;
  23. }else{
  24. static::$instance[$abstract] = $concrete;
  25. }
  26. }
  27. //将需要调用的依赖对象放出去
  28. public function make($abstract, $parameters = [])
  29. {
  30. if(isset(static::$instance[$abstract])){
  31. return static::$instance[$abstract];
  32. }else{
  33. return call_user_func_array(static::$binds[$abstract], $parameters);
  34. }
  35. }
  36. }
  37. //容器类实例化
  38. $container = new Container();
  39. //将数据库文件绑定到容器中
  40. $container->bind('model', function(){return new Model();});
  41. //将依赖的模板类绑定到容器中
  42. $container->bind('header', function(){return new header();});
  43. $container->bind('footer', function(){return new footer();});
  44. $container->bind('index', function(){return new index();});
  45. $container->bind('list_article', function(){return new list_article();});
  46. $container->bind('list_shop', function(){return new list_shop();});
  47. $container->bind('article', function(){return new article();});
  48. $container->bind('shop', function(){return new shop();});
  49. //Facade门面
  50. class Facade
  51. {
  52. //设置容器数组
  53. private static $container;
  54. //设置数据库数组
  55. private static $data;
  56. //导入容器类
  57. public static function insert(Container $container)
  58. {
  59. static::$container = $container;
  60. }
  61. //导入数据库
  62. public static function data()
  63. {
  64. static::$data = static::$container->make('model')->getDate();
  65. }
  66. //首页视图模板静态化
  67. public static function index()
  68. {
  69. return static::$container->make('index')->index(static::$data);
  70. }
  71. //header模板静态化
  72. public static function header()
  73. {
  74. return static::$container->make('header')->header();
  75. }
  76. //footer模板静态化
  77. public static function footer()
  78. {
  79. return static::$container->make('footer')->footer();
  80. }
  81. //list_article
  82. public static function list_article()
  83. {
  84. return static::$container->make('list_article')->list_article(static::$data);
  85. }
  86. //article
  87. public static function article()
  88. {
  89. return static::$container->make('article')->article(static::$data);
  90. }
  91. //list_shop
  92. public static function list_shop()
  93. {
  94. return static::$container->make('list_shop')->list_shop(static::$data);
  95. }
  96. //shop
  97. public static function shop()
  98. {
  99. return static::$container->make('shop')->shop(static::$data);
  100. }
  101. }
  102. //控制器
  103. class Controller
  104. {
  105. //构造方法,导入容器
  106. public function __construct(Container $container)
  107. {
  108. Facade::insert($container);
  109. }
  110. public function index()
  111. {
  112. //导入数据库
  113. Facade::data();
  114. //渲染首页模板
  115. Facade::header();
  116. Facade::index();
  117. Facade::footer();
  118. }
  119. public function list_article()
  120. {
  121. //导入数据库
  122. Facade::data();
  123. //渲染新闻列表模板
  124. Facade::header();
  125. Facade::list_article();
  126. Facade::footer();
  127. }
  128. public function list_shop()
  129. {
  130. //导入数据库
  131. Facade::data();
  132. //渲染商品列表模板
  133. Facade::header();
  134. Facade::list_shop();
  135. Facade::footer();
  136. }
  137. public function article()
  138. {
  139. //导入数据库
  140. Facade::data();
  141. //渲染新闻内容模板
  142. Facade::header();
  143. Facade::article();
  144. Facade::footer();
  145. }
  146. public function shop()
  147. {
  148. //导入数据库
  149. Facade::data();
  150. //渲染商品内容模板
  151. Facade::header();
  152. Facade::shop();
  153. Facade::footer();
  154. }
  155. }
  156. //控制器实例化
  157. $controller = new Controller($container);

自动连接

  1. <?php
  2. spl_autoload_register(function (string $class) {
  3. $file = str_replace('\\', '/', $class) . '.php';
  4. if (!is_file($file)) {
  5. throw new \Exception("file don't exists");
  6. }
  7. require $file;
  8. });

视图层

  1. //header.php
  2. <?php
  3. namespace view;
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <title>首页</title>
  10. <!-- <link rel="stylesheet" href="static/css/reset.css">-->
  11. <link rel="stylesheet" href="static/font/iconfont.css">
  12. <link rel="stylesheet" href="static/css/index.css">
  13. </head>
  14. <body>
  15. <!--公共顶部导航区-->
  16. <header>
  17. <a href="/">网站首页</a>
  18. <a href="/">专题</a>
  19. <a href="/">网站导航</a>
  20. <a href="/">二手商品</a>
  21. <a href="/">讨论区</a>
  22. <span>
  23. <?php
  24. if(isset($_COOKIE['name'])){
  25. echo "<a><i class='iconfont icon-huiyuan2'></i>{$_COOKIE['name']}</a> <a href='dispatch.php?action=logout'>退出</a>";
  26. }else{
  27. echo '<a href="dispatch.php?action=login"><i class="iconfont icon-huiyuan2"></i>登陆</a><a href="dispatch.php?action=register">免费注册</a>';
  28. }
  29. ?>
  30. </span>
  31. </header>
  32. <?php
  33. class header
  34. {
  35. public function header()
  36. {
  37. }
  38. }
  39. ?>
  40. *************************
  41. *************************
  42. //footer.php
  43. <?php
  44. namespace view;
  45. class footer
  46. {
  47. public function footer()
  48. {
  49. }
  50. }
  51. ?>
  52. <!-- 页底部-->
  53. <footer>
  54. <div>
  55. <a href="">简介</a>
  56. <a href="">联系我们</a>
  57. <a href="">招聘信息</a>
  58. <a href="">友情链接</a>
  59. <a href="">用户服务协议</a>
  60. <a href="">隐私权声明</a>
  61. <a href="">法律投诉声明</a>
  62. </div>
  63. <div><span>LOGO</span></div>
  64. <div>
  65. <p>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</p>
  66. <p>ICP150110 ICP14323013号-2 皖公网安备110108024357788号</p>
  67. <p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>
  68. </div>
  69. <div>
  70. <p>关注公众号</p>
  71. <img src="static/images/erwei-code.png" alt="">
  72. </div>
  73. </footer>
  74. </body>
  75. </html>
  76. *************************
  77. *************************
  78. //index.php
  79. <?php
  80. namespace view;
  81. ?>
  82. <!--logo+搜索框+快捷入口区-->
  83. <?php
  84. class index
  85. {
  86. public function index($data)
  87. {
  88. echo '
  89. <link rel="stylesheet" href="static/css/base.css">
  90. <main>
  91. <section class="one w1220">
  92. <a href=""><img src="img/logo.png" alt=""></a>
  93. <div class="search">
  94. <input type="text" value="">
  95. <i class="icon icon-jinduchaxun"></i>
  96. </div>
  97. <div class="one-r">
  98. <a href=""><i class="icon icon-huiyuan1"></i></a>
  99. <a href=""><i class="icon icon-danmu1"></i></a>
  100. <a href=""><i class="icon icon-duoxuankuang1"></i></a>
  101. <a href=""><i class="icon icon-jishufuwu"></i></a>
  102. <a href=""><i class="icon icon-peiwangyindao"></i></a>
  103. <a href=""><i class="icon icon-wenjianjia"></i></a>
  104. <a href=""><i class="icon icon-huiyuan1"></i></a>
  105. </div>
  106. </section>
  107. <nav class="w1220">
  108. <ul>
  109. <span>
  110. <i class="icon icon-renwujincheng"></i>
  111. <a>资讯看学</a>
  112. </span>
  113. <li>器材</li>
  114. <li>大师</li>
  115. <li>学院</li>
  116. <li>影赛</li>
  117. <li>器材</li>
  118. <li>大师</li>
  119. <li>学院</li>
  120. <li>影赛</li>
  121. </ul>
  122. <ul>
  123. <span>
  124. <i class="icon icon-renwujincheng"></i>
  125. <a>资讯看学</a>
  126. </span>
  127. <li>器材</li>
  128. <li>大师</li>
  129. <li>学院</li>
  130. <li>影赛</li>
  131. </ul>
  132. <ul>
  133. <span>
  134. <i class="icon icon-renwujincheng"></i>
  135. <a>资讯看学</a>
  136. </span>
  137. <li>器材</li>
  138. <li>大师</li>
  139. <li>学院</li>
  140. <li>影赛</li>
  141. </ul>
  142. <ul>
  143. <span>
  144. <i class="icon icon-renwujincheng"></i>
  145. <a>资讯看学</a>
  146. </span>
  147. <li>器材</li>
  148. <li>大师</li>
  149. <li>学院</li>
  150. <li>影赛</li>
  151. <li>器材</li>
  152. <li>大师</li>
  153. <li>学院</li>
  154. <li>影赛</li>
  155. <li>器材</li>
  156. <li>大师</li>
  157. </ul>
  158. </nav>
  159. <section class="banner w1220">
  160. <div><img src="/img/2.jpg" alt=""></div>
  161. <div><img src="/img/banner-right.jpg" alt=""></div>
  162. </section>
  163. <article class="news w1220">
  164. <section>
  165. <h1>新闻资讯</h1>
  166. <a href="">更多</a>
  167. </section>
  168. <article>
  169. <aside>';
  170. $route = array_slice($data['article'], 0, 1);
  171. foreach ($route as $value) {
  172. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']} width=380 height=160></a>";
  173. }
  174. $route = array_slice($data['article'], 10, 2);
  175. foreach ($route as $value) {
  176. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']} width=180 height=120><span>{$value['title']}</span></a>";
  177. }
  178. echo '
  179. </aside>
  180. <article>';
  181. $route = array_slice($data['article'], 7, 1);
  182. foreach ($route as $value) {
  183. echo "<h3><a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></h3>";
  184. }
  185. echo '
  186. <ul>';
  187. $route = array_slice($data['article'], 4, 8);
  188. foreach ($route as $value) {
  189. echo "<li><span>[新闻]</span><a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  190. }
  191. echo '
  192. </ul>
  193. </article>
  194. <article>';
  195. $route = array_slice($data['article'], 10, 1);
  196. foreach ($route as $value) {
  197. echo "<h3><a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></h3>";
  198. }
  199. echo '
  200. <ul>';
  201. $route = array_slice(array_reverse($data['article']), 0, 8);
  202. foreach ($route as $value) {
  203. echo "<li><span>[新闻]</span><a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  204. }
  205. echo '
  206. </ul>
  207. </article>
  208. </article>
  209. </article>
  210. <section class="bg">
  211. <article class="picture w1220">
  212. <h2><span>图片专区</span></h2>
  213. <dl>';
  214. $route = array_slice($data['list'], 2, 1);
  215. foreach ($route as $value) {
  216. echo "<dt>{$value['title']}<span>纵观摄影艺术</span></dt>";
  217. }
  218. $route = array_slice(array_reverse($data['article']), 0, 4);
  219. foreach ($route as $value) {
  220. echo "<dd><a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']}>{$value['title']}</a></dd>";
  221. }
  222. echo '
  223. </dl>
  224. <dl>';
  225. $route = array_slice($data['list'], 1, 1);
  226. foreach ($route as $value) {
  227. echo "<dt>{$value['title']}<span>纵观摄影艺术</span></dt>";
  228. }
  229. $route = array_slice(array_reverse($data['article']), 4, 4);
  230. foreach ($route as $value) {
  231. echo "<dd><a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']}>{$value['title']}</a></dd>";
  232. }
  233. echo '
  234. </dl>
  235. <dl>';
  236. $route = array_slice($data['list'], 0, 1);
  237. foreach ($route as $value) {
  238. echo "<dt>{$value['title']}<span>纵观摄影艺术</span></dt>";
  239. }
  240. $route = array_slice(array_reverse($data['article']), 8, 4);
  241. foreach ($route as $value) {
  242. echo "<dd><a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']}>{$value['title']}</a></dd>";
  243. }
  244. echo '
  245. </dl>
  246. </article>
  247. </section>
  248. </main>';
  249. echo '
  250. <!--二手交易专区-->
  251. <div class="title">
  252. <span>二手交易</span>
  253. </div>
  254. <div class="second-hand">
  255. <div>
  256. <a href="">抢好货</a>
  257. <span>0低价, 便捷,安全,快速</span>
  258. </div>
  259. <div>
  260. <span>热门分类</span>
  261. <a href="">美女写真</a>
  262. <a href="">日本美女</a>
  263. <a href="">美国美女</a>
  264. <a href="">国内美女</a>
  265. <a href="">AV美女</a>
  266. </div>';
  267. $route = array_slice(array_reverse($data['shop']),0, 4);
  268. foreach ($route as $value) {
  269. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']} width=176 height=120></a>";
  270. }
  271. $route = array_slice(array_reverse($data['shop']), 0, 4);
  272. foreach ($route as $value) {
  273. echo "<div class='detail'>";
  274. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  275. echo "<div>";
  276. echo " <a href=/article.php?list={$value['list']}&id={$value['id']}>";
  277. echo " <span>&yen; {$value['price']}</span>";
  278. echo " <span>美女</span>";
  279. echo " </a>
  280. </div></div>";
  281. }
  282. $route = array_slice($data['shop'],0, 4);
  283. foreach ($route as $value) {
  284. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']} width=176 height=120></a>";
  285. }
  286. $route = array_slice($data['shop'],0, 4);
  287. foreach ($route as $value) {
  288. echo "<div class='detail'>";
  289. echo "<a href=/article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  290. echo "<div>";
  291. echo " <a href=/article.php?list={$value['list']}&id={$value['id']}>";
  292. echo " <span>&yen; {$value['price']}</span>";
  293. echo " <span>美女</span>";
  294. echo " </a>
  295. </div></div>";
  296. }
  297. echo '
  298. <div>
  299. <a href=""><img src="static/images/ad/1.png" alt="" width="180" height="112"></a>
  300. <a href=""><img src="static/images/ad/2.png" alt="" width="180" height="112"></a>
  301. <a href=""><img src="static/images/ad/3.png" alt="" width="180" height="112"></a>
  302. <a href=""><img src="static/images/ad/4.png" alt="" width="180" height="112"></a>
  303. <a href=""><img src="static/images/ad/image.png" alt="" width="393" height="56"></a>
  304. <a href=""><img src="static/images/ad/ad2.jpg" alt="" width="393" height="56"></a>
  305. </div>
  306. </div>
  307. <!--合作网站-->
  308. <div class="title" style="background:#fff">
  309. <span>合作网站</span>
  310. </div>
  311. <div class="my-links">
  312. <a href="https://www.php.cn">php中文网</a>
  313. <a href="https://www.html.cn">html中文网</a>
  314. <a href="https://www.py.cn">python中文网</a>
  315. <a href="https://www.php.cn">php中文网</a>
  316. <a href="https://www.html.cn">html中文网</a>
  317. <a href="https://www.py.cn">python中文网</a>
  318. <a href="https://www.php.cn">php中文网</a>
  319. <a href="https://www.html.cn">html中文网</a>
  320. <a href="https://www.py.cn">python中文网</a>
  321. <a href="https://www.php.cn">php中文网</a>
  322. <a href="https://www.html.cn">html中文网</a>
  323. <a href="https://www.py.cn">python中文网</a>
  324. <a href="https://www.py.cn">python中文网</a>
  325. <a href="https://www.php.cn">php中文网</a>
  326. <a href="https://www.html.cn">html中文网</a>
  327. <a href="https://www.py.cn">python中文网</a>
  328. <a href="https://www.php.cn">php中文网</a>
  329. <a href="https://www.html.cn">html中文网</a>
  330. <a href="https://www.py.cn">python中文网</a>
  331. <a href="https://www.php.cn">php中文网</a>
  332. <a href="https://www.html.cn">html中文网</a>
  333. <a href="https://www.py.cn">python中文网</a>
  334. <a href="https://www.php.cn">php中文网</a>
  335. <a href="https://www.html.cn">html中文网</a>
  336. <a href="https://www.py.cn">python中文网</a>
  337. <a href="https://www.py.cn">python中文网</a>
  338. </div>
  339. ';
  340. }
  341. }
  342. ?>
  343. ######################################
  344. ######################################
  345. //list_article.php
  346. <?php
  347. namespace view;
  348. class list_article
  349. {
  350. public function list_article($data)
  351. {
  352. echo '
  353. <link rel="stylesheet" href="static/css/article-list.css">
  354. <div class="main">
  355. <div class="top">
  356. <img src="static/images/ar-logo.png" alt="">';
  357. foreach ($data['list'] as $value) {
  358. if($_GET['id'] === $value['id']){
  359. echo "<a style='font-size:24px;margin-left:10px;' href=list.php?id={$value['id']}>{$value['title']}</a>";
  360. }
  361. }
  362. echo '
  363. <label><input type="search"><span class="iconfont icon-sousuo2"></span></label>
  364. </div>
  365. <article>
  366. <div>';
  367. foreach ($data['list'] as $value) {
  368. if($_GET['id'] === $value['id']){
  369. echo "<a href=list.php?id={$value['id']} id=active>{$value['title']}</a>";
  370. }
  371. }
  372. foreach ($data['list'] as $value) {
  373. if(!($_GET['id'] === $value['id'])){
  374. echo "<a href=list.php?id={$value['id']}>{$value['title']}</a>";
  375. }
  376. }
  377. echo '
  378. </div>';
  379. foreach (array_reverse($data['article']) as $value) {
  380. if(!($_GET['id'] === $value['id'])){
  381. echo "<div class='list1'>";
  382. echo "<a href=article.php?list={$value['list']}&id={$value['id']}><img src={$value['img']} width='272' height='200'></a>";
  383. echo "<div>";
  384. echo "<a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  385. echo "<span>{$value['title']}</span>";
  386. echo "</div>";
  387. foreach($data['list'] as $v){
  388. if($v['id'] === $value['list']){
  389. echo "<a href=list.php?id={$v['id']}>{$v['title']} · 46 分钟前</a>";
  390. }
  391. }
  392. echo "<span><i class='iconfont icon-icon_yulan'></i>2233</span>";
  393. echo "</div>";
  394. }
  395. }
  396. echo '
  397. </article>
  398. <!-- 右侧列表-->
  399. <div class="list1">
  400. <h3>网页评论</h3>
  401. <ul>';
  402. foreach (array_reverse($data['article']) as $value) {
  403. echo "<li><a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  404. }
  405. echo '
  406. </ul>
  407. </div>
  408. <div class="list2">
  409. <h3>网页评论</h3>
  410. <ul>';
  411. foreach ($data['article'] as $value) {
  412. echo "<li><a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  413. }
  414. echo '
  415. </ul>
  416. </div>
  417. <div class="recommend">
  418. <h3>推荐阅读</h3>';
  419. $route = array_slice($data['article'],0,4);
  420. foreach ($route as $value) {
  421. echo "<a href=article.php?list={$value['list']}&id={$value['id']}><img src='{$value['img']}' width='195' height='130'></a>";
  422. }
  423. $route = array_slice($data['article'],0,4);
  424. foreach ($route as $value) {
  425. echo "<a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  426. }
  427. $route = array_slice($data['article'],4,4);
  428. foreach ($route as $value) {
  429. echo "<a href=article.php?list={$value['list']}&id={$value['id']}><img src='{$value['img']}' width='195' height='130'></a>";
  430. }
  431. $route = array_slice($data['article'],4,4);
  432. foreach ($route as $value) {
  433. echo "<a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  434. }
  435. echo '
  436. </div>
  437. </div>
  438. ';
  439. }
  440. }
  441. ######################################
  442. ######################################
  443. //shop.php
  444. <?php
  445. namespace view;
  446. class shop
  447. {
  448. public function shop($data)
  449. {
  450. echo '
  451. <link rel="stylesheet" href="static/css/shop-content.css">
  452. <main>
  453. <!--导航-->
  454. <nav>
  455. <h3>所有产品分类 <span class="iconfont icon-liebiao"></span></h3>
  456. <a href="\" class="active">首页</a>
  457. <a href="">3C</a>
  458. <a href="">生活用品</a>
  459. <a href="">名字名画</a>
  460. </nav>
  461. <!-- 商品展示-->
  462. <div class="goods">';
  463. foreach($data['list'] as $value){
  464. if($value['id'] === $_GET['list']){
  465. echo "<div class='top-nav'>
  466. <a href=\>首页</a>-&gt;&gt;
  467. <a href=list.php?id={$value['id']}>{$value['title']}</a>-&gt;&gt; ";
  468. foreach($data['shop'] as $vv){
  469. if($_GET['id'] == $vv['id']){
  470. echo "{$vv['title']}";
  471. }
  472. }
  473. echo "</div>";
  474. }
  475. }
  476. foreach($data['shop'] as $value){
  477. if($value['id'] === $_GET['id'] && $_GET['list'] == 2){
  478. echo "<img src={$value['img']}>
  479. <h2>{$value['title']}</h2>
  480. <p>本站特惠: <span>&yen;{$value['price']}</span></p>";
  481. }
  482. }
  483. echo '
  484. <p>
  485. 销量: <span>13</span>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
  486. 累积评价: <span>3</span>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
  487. 好评率: <span>199%</span>
  488. </p>
  489. <p>
  490. <label for="num">购买数量:</label><input type="number" id="num" value="1">
  491. </p>
  492. <div>
  493. <button>立即购买</button>
  494. <button><i class="iconfont icon-icon_tianjia"></i>加入购物车</button>
  495. </div>
  496. <div>
  497. <span><i class="iconfont icon-zhanghaoquanxianguanli"></i>本站保障</span>
  498. <span><i class="iconfont icon-icon_safety"></i>企业认证</span>
  499. <span><i class="iconfont icon-tianshenpi"></i>退款承诺</span>
  500. <span><i class="iconfont icon-kuaisubianpai"></i>免费换货</span>
  501. </div>
  502. </div>
  503. <!-- 商品详情-->
  504. <div class="detail">
  505. <aside>
  506. <div><span>商品详情</span><span>案例演示</span></div>';
  507. foreach ($data['shop'] as $value) {
  508. echo "<div><a href=article.php?list={$value['lis']}&id={$value['id']}><img src={$value['img']} width=170></a>
  509. <a href=article.php?list={$value['lis']}&id={$value['id']}>{$value['title']}</a>
  510. <div><span>热销:11</span><span>&yen;{$value['price']}</span></div></div>";
  511. }
  512. echo '
  513. </aside>
  514. <article>
  515. <div class="nav">
  516. <a href="">商品详情</a>
  517. <a href="">案例/演示</a>
  518. <a href="">常见问题</a>
  519. <a href="">累计评价</a>
  520. <a href="">产品咨询</a>
  521. </div>
  522. <div class="content">';
  523. foreach ($data['shop'] as $value) {
  524. if($_GET['id'] == $value['id']){
  525. echo $value['newstext'];
  526. }
  527. }
  528. echo '
  529. </div>
  530. <div class="comment">
  531. <h3>网页评论</h3>
  532. <img src="static/images/user.png" alt="" width="60">
  533. <textarea name="" id="" cols="30" rows="10"></textarea>
  534. <button>发表评论</button>
  535. </div>
  536. <!-- 最新评论-->
  537. <div>
  538. <h3>最新评论</h3>
  539. <div>
  540. <img src="static/images/user.png" alt="" width="60" height="60">
  541. <span>用户昵称</span>
  542. <span>留言内容</span>
  543. <div>
  544. <span>2019-12-12 15:34:23发表</span>
  545. <span><i class="iconfont icon-dianzan"></i>回复</span>
  546. </div>
  547. </div>
  548. <div>
  549. <img src="static/images/user.png" alt="" width="60" height="60">
  550. <span>用户昵称</span>
  551. <span>留言内容</span>
  552. <div>
  553. <span>2019-12-12 15:34:23发表</span>
  554. <span><i class="iconfont icon-dianzan"></i>回复</span>
  555. </div>
  556. </div>
  557. <div>
  558. <img src="static/images/user.png" alt="" width="60" height="60">
  559. <span>用户昵称</span>
  560. <span>留言内容</span>
  561. <div>
  562. <span>2019-12-12 15:34:23发表</span>
  563. <span><i class="iconfont icon-dianzan"></i>回复</span>
  564. </div>
  565. </div>
  566. <div>
  567. <img src="static/images/user.png" alt="" width="60" height="60">
  568. <span>用户昵称</span>
  569. <span>留言内容</span>
  570. <div>
  571. <span>2019-12-12 15:34:23发表</span>
  572. <span><i class="iconfont icon-dianzan"></i>回复</span>
  573. </div>
  574. </div>
  575. </div>
  576. </article>
  577. </div>
  578. </main>
  579. ';
  580. }
  581. }
  582. ######################################
  583. ######################################
  584. //article.php
  585. <?php
  586. namespace view;
  587. class article
  588. {
  589. public function article($data)
  590. {
  591. echo '
  592. <link rel="stylesheet" href="static/css/article.css">
  593. <div class="main">
  594. <div class="top">
  595. <img src="static/images/ar-logo.png" alt="">';
  596. foreach($data['list'] as $value){
  597. if($value['id'] === $_GET['list']){
  598. echo "<a style='font-size:27px;margin-left:15px;color:red' href=list.php?id={$value['id']}>{$value['title']}</a>";
  599. }
  600. }
  601. echo '
  602. <label><input type="search"><span class="iconfont icon-sousuo2"></span></label>
  603. </div>
  604. <!-- 正文-->
  605. <article>';
  606. foreach($data['article'] as $value){
  607. if($value['id'] === $_GET['id']){
  608. echo "<h1>{$value['title']}</h1>";
  609. }
  610. }
  611. echo '
  612. <div>
  613. <span>发布时间:2019.8.8</span>
  614. <span>来源:北京 青年报</span>
  615. <span>阅读量:545</span>
  616. <span>评论数:1545</span>
  617. </div>
  618. <div>';
  619. foreach($data['article'] as $value){
  620. if($value['id'] === $_GET['id']){
  621. if($_GET['list'] == 1){
  622. echo "<img src={$value['img']}><br>";
  623. echo "{$value['newstext']}";
  624. }else{
  625. echo "{$value['newstext']}";
  626. }
  627. }
  628. }
  629. echo '
  630. </div>
  631. </article>
  632. <!-- 右侧列表-->
  633. <div class="list1">
  634. <h3>网页评论</h3>
  635. <ul>';
  636. foreach (array_reverse($data['article']) as $value) {
  637. echo "<li><a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  638. }
  639. echo '
  640. </ul>
  641. </div>
  642. <div class="list2">
  643. <h3>网页评论</h3>
  644. <ul>';
  645. foreach ($data['article'] as $value) {
  646. echo "<li><a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a></li>";
  647. }
  648. echo '
  649. </ul>
  650. </div>
  651. <div class="recommend">
  652. <h3>推荐阅读</h3>';
  653. $route = array_slice($data['article'],0,4);
  654. foreach ($route as $value) {
  655. echo "<a href=article.php?list={$value['list']}&id={$value['id']}><img src='{$value['img']}' width='195' height='130'></a>";
  656. }
  657. $route = array_slice($data['article'],0,4);
  658. foreach ($route as $value) {
  659. echo "<a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  660. }
  661. $route = array_slice($data['article'],4,4);
  662. foreach ($route as $value) {
  663. echo "<a href=article.php?list={$value['list']}&id={$value['id']}><img src='{$value['img']}' width='195' height='130'></a>";
  664. }
  665. $route = array_slice($data['article'],4,4);
  666. foreach ($route as $value) {
  667. echo "<a href=article.php?list={$value['list']}&id={$value['id']}>{$value['title']}</a>";
  668. }
  669. echo '
  670. </div>
  671. </div>
  672. ';
  673. }
  674. }

客户端

这里因为知道栏目id,故用$_GET区别调用渲染模板

  1. //index.php
  2. <?php
  3. require __DIR__ . '/Controller.php';
  4. $controller->index();
  5. ######################################
  6. ######################################
  7. //article.php
  8. <?php
  9. require __DIR__ . '/Controller.php';
  10. if($_GET['list'] == 2){
  11. $controller->shop();
  12. }else{
  13. $controller->article();
  14. }
  15. ######################################
  16. ######################################
  17. //list.php
  18. <?php
  19. require __DIR__ . '/Controller.php';
  20. if($_GET['id'] == 2){
  21. $controller->list_shop();
  22. }else{
  23. $controller->list_article();
  24. }

会员登录

  1. //派发器dispatch.php
  2. <?php
  3. $pdo = new PDO('mysql:host=localhost;dbname=film', 'root', 'root');
  4. $action = isset($_GET['action']) ? $_GET['action'] : 'login';
  5. $action = htmlentities(strtolower(trim($action)));
  6. switch ($action){
  7. case 'login' : require __DIR__ . '/login/login.php';//登录
  8. break;
  9. case 'check' : require __DIR__ . '/login/check.php';//登录验证
  10. break;
  11. case 'register' : require __DIR__ . '/login/register.php'; //注册
  12. break;
  13. case 'checker' : require __DIR__ . '/login/checker.php'; //注册验证
  14. break;
  15. case 'logout' : require __DIR__ . '/login/logout.php';//退出登录
  16. break;
  17. default : die('<script>location.assign("index.php");</script>');
  18. }
  19. ######################################
  20. ######################################
  21. //登录页面 login.php
  22. <?php
  23. if(isset($_COOKIE['name'])){
  24. echo '<script>alert("不要重复登录");location.assign("index.php");</script>';
  25. }
  26. ?>
  27. <!doctype html>
  28. <html lang="en">
  29. <head>
  30. <meta charset="UTF-8">
  31. <title>用户登录</title>
  32. </head>
  33. <body>
  34. <h3>用户登录</h3>
  35. <form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
  36. <p>
  37. <label for="tel">手机号:</label>
  38. <input type="tel" name="tel" id="tel">
  39. </p>
  40. <p>
  41. <label for="password">密码:</label>
  42. <input type="password" name="password" id="password">
  43. </p>
  44. <p>
  45. <button>提交</button>
  46. </p>
  47. </form>
  48. </body>
  49. </html>
  50. ######################################
  51. ######################################
  52. //登录验证 check.php
  53. <?php
  54. //验证是否通过post提交登陆
  55. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  56. //从数据库获取账户密码
  57. $stmt = $pdo->prepare('SELECT * FROM `user` WHERE `tel` = :tel AND `password` = :password LIMIT 1');
  58. //验证提交登陆的账户和密码是否再服务器中有对应的账户密码
  59. $stmt->execute( ['tel'=>$_POST['tel'], 'password'=>sha1($_POST['password']) ]);
  60. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  61. if($user === false || empty($_POST['tel'])){
  62. die('<script>alert("账户或密码错误,重新登陆");history.back();</script>');
  63. }else{
  64. setcookie('name',$user['name'],time() + 60 * 60 * 72); //表示3天,登陆成功后,cookie 3天有效,无需登陆
  65. die('<script>alert("登录成功");location.assign("index.php");</script>');
  66. }
  67. }else{
  68. die('<script>alert("非法操作");location.assign("/index.php");</script>');
  69. }
  70. ######################################
  71. ######################################
  72. //退出 logout.php
  73. <?php
  74. if(isset($_COOKIE['name'])){
  75. setcookie('name',null,time() - 3600);
  76. die('<script>alert("退出成功,返回首页");location.assign("index.php");</script>');
  77. }else{
  78. die('<script>alert("请先登录");location.assign("dispatch.php?action=login");</script>');
  79. }
  80. ######################################
  81. ######################################
  82. //注册 register.php
  83. <?php
  84. if(isset($_COOKIE['name'])){
  85. echo '<script>alert("不要重复登录");location.assign("index.php");</script>';
  86. }
  87. ?>
  88. <!doctype html>
  89. <html lang="en">
  90. <head>
  91. <meta charset="UTF-8">
  92. <title>用户注册</title>
  93. </head>
  94. <body>
  95. <h3>用户注册</h3>
  96. <form action="dispatch.php?action=checker" method="post" onsubmit="return isEmpty();">
  97. <p>
  98. <label for="tel">手机号:</label>
  99. <input type="tel" name="tel" id="tel">
  100. </p>
  101. <p>
  102. <label for="name">用户名:</label>
  103. <input type="name" name="name" id="name">
  104. </p>
  105. <p>
  106. <label for="password">密码:</label>
  107. <input type="password" name="password" id="password">
  108. </p>
  109. <p>
  110. <button>提交</button>
  111. </p>
  112. </form>
  113. <script>
  114. function isEmpty() {
  115. var phone = document.getElementById('phone').value;
  116. var password = document.getElementById('password').value;
  117. if (phone.length=== 0 || password.length===0) {
  118. alert('手机和密码不能为空');
  119. return false;
  120. }
  121. }
  122. </script>
  123. </body>
  124. </html>
  125. ######################################
  126. ######################################
  127. //注册验证 checker.php
  128. <?php
  129. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  130. //注册,新增用户
  131. $stmt = $pdo->prepare('INSERT INTO `user`(`name`, `tel`, `password`) VALUES(:name, :tel, :password) ');
  132. if( empty($_POST['tel']) || empty($_POST['password']) ){
  133. die('<script>alert("账户或密码为空,重新登陆");history.back();</script>');
  134. }elseif($stmt->execute(['name'=>$_POST['name'], 'tel'=>$_POST['tel'], 'password'=>sha1($_POST['password']) ]) ){
  135. if($stmt->rowCount() > 0){
  136. setcookie('name',$_POST['name'],time()+60*60*72);
  137. die('<script>alert("注册成功");location.assign("/index.php");</script>');
  138. }
  139. }
  140. }else{
  141. die('<script>alert("非法操作");location.assign("/index.php");</script>');
  142. }

数据库图片

首页部分css,用的grid和flex混搭

  1. .w1220{
  2. width: 1220px;
  3. margin: 10px auto;
  4. }
  5. main{
  6. width: 100%;
  7. }
  8. main>.one{
  9. height: 90px;
  10. margin-top: 20px;
  11. display: flex;
  12. justify-content: space-between;
  13. align-items: center;
  14. }
  15. main>.one>.one-r{
  16. display: flex;
  17. width: 400px;
  18. justify-content: space-between;
  19. }
  20. main>.one>.search{
  21. position: relative;
  22. }
  23. main>.one>.search>input{
  24. width: 280px;
  25. height: 30px;
  26. border-radius: 15px;
  27. padding-left: 20px;
  28. }
  29. main>.one>.search>i{
  30. font-size: 26px;
  31. position: absolute;
  32. right: 10px;
  33. top:5px;
  34. cursor: pointer;
  35. }
  36. main>.one>.one-r i{
  37. font-size: 30px;
  38. }
  39. /*首页导航区*/
  40. main>nav{
  41. display: flex;
  42. justify-content: space-between;
  43. font-size: 14px;
  44. height: 50px;
  45. }
  46. main>nav>ul{
  47. display: grid;
  48. /* grid-template-rows: repeat(2, 1fr);
  49. grid-template-columns: repeat(5, 1fr); */
  50. grid-template: repeat(2, 1fr)/repeat(5,1fr);
  51. align-items: center;
  52. justify-items: center;
  53. }
  54. main>nav>ul>span{
  55. grid-area: span 2;
  56. /* grid-row-end: span 2; */
  57. display: flex;
  58. margin-left: 18px;
  59. margin-top: 5px;
  60. border-right: solid 1px lightgray;
  61. }
  62. main>nav>ul>span>i:first-of-type{
  63. font-size: 40px;
  64. color: red;
  65. margin-right: 3px;
  66. display: block;
  67. }
  68. main>nav>ul>span>a{
  69. width: 40px;
  70. height: 35px;
  71. display: inline-block;
  72. margin-left: 5px;
  73. color: black;
  74. }
  75. main>nav>ul:nth-of-type(2),main>nav>ul:nth-of-type(3){
  76. grid-template-columns:repeat(3,1fr);
  77. width: 380px;
  78. }
  79. main>nav>ul:last-of-type{
  80. grid-template-columns:repeat(7,1fr);
  81. }
  82. /*banner*/
  83. main>.banner{
  84. display: flex;
  85. justify-content: space-between;
  86. margin: 30px auto;
  87. }
  88. /*首页新闻资讯*/
  89. main>.news>section{
  90. display: flex;
  91. justify-content: space-between;
  92. align-items: center;
  93. border-bottom: 1px solid rgba(156, 156, 156, 0.3);
  94. }
  95. main>.news>section>h1{
  96. font-size: 28px;
  97. padding-bottom: 10px;
  98. border-bottom: 3px solid red;
  99. }
  100. main>.news>article{
  101. display: grid;
  102. grid-template-columns: 380px 1fr 1fr;
  103. }
  104. main>.news>article>aside{
  105. display: grid;
  106. grid-template: repeat(2, 1fr)/repeat(2, 1fr);
  107. }
  108. main>.news>article>aside>a:first-of-type{
  109. grid-column-end: span 2;
  110. margin-top: 25px;
  111. }
  112. main>.news>article>aside>a{
  113. margin-top: 15px;
  114. }
  115. main>.news>article>aside img{
  116. width: 100%;
  117. display: block;
  118. }
  119. main>.news>article>aside>a:nth-of-type(2){
  120. margin-right: 5px;
  121. }
  122. main>.news>article>aside>a:last-of-type{
  123. margin-left: 5px;
  124. }
  125. main>.news>article>aside>a:nth-of-type(2)>img,
  126. main>.news>article>aside>a:last-of-type>img{
  127. width: 100%;
  128. height: 120px;
  129. }
  130. main>.news>article>article{
  131. margin-left: 30px;
  132. }
  133. main>.news>article>article>h3{
  134. height: 70px;
  135. line-height: 70px;
  136. text-align: center;
  137. white-space:nowrap;
  138. width:380px;
  139. overflow:hidden;
  140. text-overflow:ellipsis;
  141. }
  142. main>.news>article>article h3 a{
  143. color: red;
  144. font-size: 22px;
  145. }
  146. main>.news>article>article>ul>li{
  147. margin-top: 13px;
  148. }
  149. main>.news>article>article>ul>li:first-of-type{
  150. margin-top: 0;
  151. }
  152. main>.news>article>article>ul>li:nth-of-type(5){
  153. margin-top: 30px;
  154. }
  155. main>.news>article>article>ul a{
  156. color: black;
  157. }
  158. main>.news>article>article>ul a:hover{
  159. color: red;
  160. }
  161. main>.news>article>article>ul span{
  162. color: #666;
  163. }
  164. main>.news>article>article>ul>li span{
  165. margin-right: 10px;
  166. }
  167. .bg{
  168. width: 100%;
  169. background-color: rgba(156, 156, 156, 0.2);
  170. }
  171. main>.bg>.picture{
  172. padding: 35px 0;
  173. display: grid;
  174. grid-template: 70px 1fr/repeat(3,1fr);
  175. }
  176. main>.bg>.picture>h2{
  177. text-align: center;
  178. grid-column-end: span 3;
  179. }
  180. main>.bg>.picture>h2>span{
  181. padding-bottom: 5px;
  182. border-bottom: 3px solid rgba(253, 0, 0, 0.7);
  183. }
  184. main>.bg>.picture>dl{
  185. background-color: rgba(255, 255, 255, .7);
  186. margin-right: 20px;
  187. padding: 15px 10px;
  188. box-sizing: border-box;
  189. display: grid;
  190. grid-template: 40px 1fr 1fr/repeat(2,1fr);
  191. }
  192. main>.bg>.picture>dl>dt{
  193. grid-column: span 2;
  194. margin-left: 10px;
  195. font-size: 22px;
  196. font-weight: 600;
  197. border-bottom: 1px solid rgba(156, 156, 156, 0.2);
  198. }
  199. main>.bg>.picture>dl>dt>span{
  200. font-size: 14px;
  201. font-weight: 0;
  202. margin-left: 20px;
  203. }
  204. main>.bg>.picture>dl:last-of-type{
  205. margin-right: 0;
  206. }
  207. main>.bg>.picture>dl>dd{
  208. width: 160px;
  209. margin-top: 20px;
  210. align-self: center;
  211. justify-self:center;
  212. }
  213. main>.bg>.picture>dl>dd img{
  214. width: 160px;
  215. height: 120px;
  216. display: block;
  217. }

总结

1.首页部分自己手写了css,用的grid和flex混搭,复习了一下前端
2.复习了$_COOKIE 使用它完成了会员登录设置
3.全手写MVC流程,感觉基本掌握了
4.目前的问题就是:接口视图层,model数据库,控制反转到容器层后,不知道怎么传数据库到视图层

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议