Blogger Information
Blog 32
fans 2
comment 2
visits 23318
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php改写蜂鸟网春节大作业
暴风战斧
Original
1050 people have browsed it

编程思路

1、分析页面哪些数据高度重复;
2、将数据存在数据库对应表里,然后获取为数组;
3、页面遍历数组数据实现网站数据动态填充!

作业总结

这次网页动态化我主要解决了两个问题:1、网页上哪些数据存放在数据库里;2、数据表如何设计比较好!
我的方法是,能够之后从表里获取为数组,方便循环遍历的数据我整理进数据表里,一个模块一张表,实在不行就两张,因为我想到的是“数据获取为二维数组填充网页”!整个首页,只能说是80%数据填充了,其他的我把它看做为“骨架”,就放在页面里了!

作业问题

这次作业有两个问题要请教老师。
1、商业项目动态化网站,主要是把数据作为二维数据来填充网页吗,还有其他方法吗?
2、这次我每个模块都单独写了获取数据库代码,我尝试全写一起,但是失败了,如何一张表呢?

蜂鸟网首页php改写

1、导航模块
  • 数据表和效果图



  • 从数据库获取数据
  1. <?php
  2. namespace PDO;
  3. use pdo;
  4. //1.连接数据库
  5. $pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  6. //2.操作数据库
  7. //2.1准备sql语句
  8. $sql = "SELECT * FROM `index_main_nav`;";
  9. //2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
  10. $stmt = $pdo->prepare($sql);
  11. //2.3执行sql语句
  12. $stmt->execute();
  13. //2.4数据保存为数组
  14. $navs = [];
  15. while ($nav = $stmt->fetch(PDO::FETCH_ASSOC)) {
  16. // echo '<pre>' . print_r($comment,true) . '</pre>';
  17. $navs[] = $nav;
  18. }
  19. //var_dump($navs);
  20. //echo '<hr>';
  21. //3.关闭数据库
  22. unset($pdo);
  23. ?>
  • php重写
  1. <?php include "../components/index/index_header/index_header_connect.php"?>
  2. <?php include "../components/index/index_header/index_icon_connect.php"?>
  3. <!-- 首页头部导航与轮播-->
  4. <div class="index-header">
  5. <div class="content">
  6. <!--logo与搜索框,快速入口-->
  7. <div class="log-search">
  8. <a href="" class="logo"><img src="../static/images/logo.png" alt=""></a>
  9. <div class="search">
  10. <input type="search" name="search" id="search">
  11. <label for="search" class="iconfont icon-jinduchaxun"></label>
  12. </div>
  13. <div class="quick-entry">
  14. <?php foreach ($icons as $icon) : ?>
  15. <a href="" class="<?php echo $icon['icon_url'] ?>"></a>
  16. <?php endforeach ?>
  17. </div>
  18. </div>
  19. <!--主导航-->
  20. <div class="main-nav">
  21. <?php foreach($navs as $nav) : ;?>
  22. <!--导航详情1-->
  23. <div class="nav-detail">
  24. <!--左侧图标与描述-->
  25. <div class="pic">
  26. <span class="<?php echo $nav['nav_class'] ?>"></span>
  27. <div>
  28. <span><?php echo $nav['nav_big1'] ?></span>
  29. <span><?php echo $nav['nav_big2'] ?></span>
  30. </div>
  31. </div>
  32. <!--右侧导航链接-->
  33. <div class="links">
  34. <a href=""><?php echo $nav['nav_name1'] ?></a>
  35. <a href=""><?php echo $nav['nav_name2'] ?></a>
  36. <a href=""><?php echo $nav['nav_name3'] ?></a>
  37. <a href=""><?php echo $nav['nav_name4'] ?></a>
  38. <a href=""><?php echo $nav['nav_name5'] ?></a>
  39. <a href=""><?php echo $nav['nav_name6'] ?></a>
  40. <a href=""><?php echo $nav['nav_name7'] ?></a>
  41. <a href=""><?php echo $nav['nav_name8'] ?></a>
  42. </div>
  43. </div>
  44. <?php endforeach; ?>
  45. </div>
  46. <!--轮播图-->
  47. <div class="slider">
  48. <a href=""><img src="../static/images/2.jpg" alt=""></a>
  49. <a href=""><img src="../static/images/banner-right.jpg" alt=""></a>
  50. </div>
  51. </div>
  52. </div>
2、新闻模块
  • 数据表和效果图


  • 从数据库获取数据
  1. <?php
  2. namespace PDO;
  3. use function MongoDB\BSON\fromJSON;
  4. use pdo;
  5. //1.连接数据库
  6. $pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  7. //2.操作数据库
  8. //2.1准备sql语句
  9. $sql = "SELECT * FROM `index_news`;";
  10. //2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
  11. $stmt = $pdo->prepare($sql);
  12. //2.3执行sql语句
  13. $stmt->execute();
  14. //2.4数据保存为数组
  15. $news = [];
  16. while ($new = $stmt->fetch(PDO::FETCH_ASSOC)) {
  17. // echo '<pre>' . print_r($comment,true) . '</pre>';
  18. $news[] = $new;
  19. }
  20. //var_dump($news);
  21. //echo '<hr>';
  22. //3.关闭数据库
  23. unset($pdo);
  24. //处理数组
  25. $newNews = [];
  26. foreach ($news as $newNew) {
  27. if ($newNew['new_name'] == "新闻") {
  28. $newNews[] = $newNew;
  29. }
  30. }
  31. $newSales = [];
  32. foreach ($news as $newSale) {
  33. if ($newSale['new_name'] == "促销") {
  34. $newSales[] = $newSale;
  35. }
  36. }
  37. //var_dump($newSales);
  38. ?>
  • php重写
  1. <?php include "../components/index/index_news/index_news_connect.php"?>
  2. <!--引入大标题组件-->
  3. <div class="public-headline">
  4. <span>新闻资讯</span>
  5. </div>
  6. <div class="index-news">
  7. <!-- 左侧图片列表-->
  8. <div class="imgs">
  9. <a href=""><img src="../static/images/news.jpg" alt="" class="first-img"></a>
  10. <div>
  11. <a href=""><img src="../static/images/n-2.jpg" alt=""></a>
  12. <a href="">三星Note10发布搭载挖孔前摄</a>
  13. </div>
  14. <div>
  15. <a href=""><img src="../static/images/n-3.jpg" alt=""></a>
  16. <a href="">小米公布1亿像素手机信息</a>
  17. </div>
  18. </div>
  19. <!-- 中间与右边信息列表共用样式-->
  20. <div class="list">
  21. <a href="">大隐于市的摄影师,薇薇安·迈尔</a>
  22. <ul>
  23. <?php foreach ($newNews as $newNew) : ?>
  24. <li><span>[新闻]</span><a href=""><?php echo $newNew['new_title'] ?></a></li>
  25. <?php endforeach ?>
  26. </ul>
  27. </div>
  28. <div class="list">
  29. <a href="">元旦春节,双节联动,备年货啦!!</a>
  30. <ul>
  31. <?php foreach ($newSales as $newSale) : ?>
  32. <li><span>[促销]</span><a href=""><?php echo $newSale['new_title'] ?></a></li>
  33. <?php endforeach ?>
  34. </ul>
  35. </div>
  36. </div>

3.图片模块

  • 数据表和效果图

  • 获取数据

  1. <?php
  2. namespace PDO;
  3. use function MongoDB\BSON\fromJSON;
  4. use pdo;
  5. //1.连接数据库
  6. $pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  7. //2.操作数据库
  8. //2.1准备sql语句
  9. $sql = "SELECT * FROM `public_img_list`;";
  10. //2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
  11. $stmt = $pdo->prepare($sql);
  12. //2.3执行sql语句
  13. $stmt->execute();
  14. //2.4数据保存为数组
  15. $imgs = [];
  16. while ($img = $stmt->fetch(PDO::FETCH_ASSOC)) {
  17. // echo '<pre>' . print_r($comment,true) . '</pre>';
  18. $imgs[] = $img;
  19. }
  20. //var_dump($imgs);
  21. //echo '<hr>';
  22. //3.关闭数据库
  23. unset($pdo);
  24. //处理数组
  25. $imgJks = [];
  26. foreach ($imgs as $imgJk) {
  27. if ($imgJk['img_name'] == "健康") {
  28. $imgJks[] = $imgJk;
  29. }
  30. }
  31. $imgMns = [];
  32. foreach ($imgs as $imgMn) {
  33. if ($imgMn['img_name'] == "美女") {
  34. $imgMns[] = $imgMn;
  35. }
  36. }
  37. $imgQcs = [];
  38. foreach ($imgs as $imgQc) {
  39. if ($imgQc['img_name'] == "青春") {
  40. $imgQcs[] = $imgQc;
  41. }
  42. }
  43. //var_dump($imgMns);
  44. const IMG_PATH = "../static/images/";
  45. ?>
  • php重写
  1. <?php include "../components/public/public_image_list/public_img_connect.php"?>
  2. <div class="public-headline">
  3. <span>图片专区</span>
  4. </div>
  5. <!-- 图文列表专区-->
  6. <div class="public-image-list">
  7. <div class="list">
  8. <div class="title">
  9. <a href="">美女</a>
  10. <span>纵观摄影艺术</span>
  11. </div>
  12. <div class="img-list">
  13. <?php foreach ($imgMns as $imgMn) : ?>
  14. <div>
  15. <a href=""><img src="<?php echo \PDO\IMG_PATH.$imgMn['img_url']?>" alt=""></a>
  16. <a href=""><?php echo $imgMn['img_title']?></a>
  17. </div>
  18. <?php endforeach ?>
  19. </div>
  20. </div>
  21. <div class="list">
  22. <div class="title">
  23. <a href="">健康</a>
  24. <span>纵观摄影艺术</span>
  25. </div>
  26. <div class="img-list">
  27. <?php foreach ($imgJks as $imgJk) : ?>
  28. <div>
  29. <a href=""><img src="<?php echo \PDO\IMG_PATH.$imgJk['img_url']?>" alt=""></a>
  30. <a href=""><?php echo $imgJk['img_title']?></a>
  31. </div>
  32. <?php endforeach ?>
  33. </div>
  34. </div>
  35. <div class="list">
  36. <div class="title">
  37. <a href="">青春</a>
  38. <span>纵观摄影艺术</span>
  39. </div>
  40. <div class="img-list">
  41. <?php foreach ($imgQcs as $imgQc) : ?>
  42. <div>
  43. <a href=""><img src="<?php echo \PDO\IMG_PATH.$imgQc['img_url']?>" alt=""></a>
  44. <a href=""><?php echo $imgQc['img_title']?></a>
  45. </div>
  46. <?php endforeach ?>
  47. </div>
  48. </div>
  49. </div>

4.二手交易模块

  • 数据库表和效果图


  • 获取数据
  1. <?php
  2. namespace PDO;
  3. use function MongoDB\BSON\fromJSON;
  4. use pdo;
  5. //1.连接数据库
  6. $pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  7. //2.操作数据库
  8. //2.1准备sql语句
  9. $sql = "SELECT * FROM `public_second_hand`;";
  10. //2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
  11. $stmt = $pdo->prepare($sql);
  12. //2.3执行sql语句
  13. $stmt->execute();
  14. //2.4数据保存为数组
  15. $goods = [];
  16. while ($good = $stmt->fetch(PDO::FETCH_ASSOC)) {
  17. // echo '<pre>' . print_r($comment,true) . '</pre>';
  18. $goods[] = $good;
  19. }
  20. //var_dump($goods);
  21. //echo '<hr>';
  22. //3.关闭数据库
  23. unset($pdo);
  24. const SCIMG_PATH = "../static/images/shop/";
  25. //echo SCIMG_PATH;
  26. ?>
  • php重写
  1. <?php include "../components/public/public_second_hand/public_sc_hand_connect.php"?>
  2. <!--引入大标题组件-->
  3. <div class="public-headline">
  4. <span>二手交易</span>
  5. </div>
  6. <!--二手交易专区-->
  7. <div class="public-second-hand">
  8. <!-- 标题1-->
  9. <div class="title1">
  10. <a href="">抢好货</a>
  11. <span>0低价, 便捷,安全,快速</span>
  12. </div>
  13. <!-- 标题2-->
  14. <div class="title2">
  15. <span>热门分类</span>
  16. <a href="">美女写真</a>
  17. <a href="">日本美女</a>
  18. <a href="">美国美女</a>
  19. <a href="">国内美女</a>
  20. <a href="">AV美女</a>
  21. </div>
  22. <!--商品展示区-->
  23. <div class="goods">
  24. <!--左侧商品列表-->
  25. <div class="goods-list">
  26. <?php foreach ($goods as $good): ?>
  27. <!-- 商品简介-->
  28. <div class="intro">
  29. <a href=""><img src="<?php echo \PDO\SCIMG_PATH.$good['sc_hand_url'] ?>" alt="" width="176" height="120"></a>
  30. <a href=""><?php echo $good['sc_hand_title'] ?></a>
  31. <div>
  32. <span>&yen<?php echo $good['sc_hand_price'] ?></span>
  33. <span><?php echo $good['sc_hand_tip'] ?></span>
  34. </div>
  35. </div>
  36. <?php endforeach ?>
  37. </div>
  38. <!--右侧功能快速入口-->
  39. <div class="quick-entry">
  40. <a href=""><img src="../static/images/ad/1.png" alt=""></a>
  41. <a href=""><img src="../static/images/ad/2.png" alt=""></a>
  42. <a href=""><img src="../static/images/ad/3.png" alt=""></a>
  43. <a href=""><img src="../static/images/ad/4.png" alt=""></a>
  44. <div>
  45. <a href=""><img src="../static/images/ad/image.png" alt=""></a>
  46. <a href=""><img src="../static/images/ad/ad2.jpg" alt=""></a>
  47. </div>
  48. </div>
  49. </div>
  50. </div>

5.合作单位模块

  • 数据库表和效果图

  • 获取数据

  1. <?php
  2. namespace PDO;
  3. use pdo;
  4. //1.连接数据库
  5. $pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  6. //2.操作数据库
  7. //2.1准备sql语句
  8. $sql = "SELECT * FROM `index_friend_links`;";
  9. //2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
  10. $stmt = $pdo->prepare($sql);
  11. //2.3执行sql语句
  12. $stmt->execute();
  13. //2.4数据保存为数组
  14. $links = [];
  15. while ($link = $stmt->fetch(PDO::FETCH_ASSOC)) {
  16. // echo '<pre>' . print_r($comment,true) . '</pre>';
  17. $links[] = $link;
  18. }
  19. //var_dump($navs);
  20. //echo '<hr>';
  21. //3.关闭数据库
  22. unset($pdo);
  23. ?>
  • php重写
  1. <?php include "../components/index/index_friend_links/index_friend_connect.php"?>
  2. <!--合作单位大标题-->
  3. <div class="public-headline">
  4. <span>合作单位</span>
  5. </div>
  6. <!--合作友情链接-->
  7. <div class="index-frend-links">
  8. <?php foreach ($links as $link) : ?>
  9. <a href="<?php echo $link['link_url'] ?>"><?php echo $link['link_name'] ?></a>
  10. <?php endforeach ?>
  11. </div>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:尽管还有许多问题, 但做为初学者, 能写出来已经很不错了, 加油
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