Blogger Information
Blog 20
fans 0
comment 0
visits 8629
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用流程控制替换语法,将演示网站的首页和产品页进行动态替换(1124)
A 管志岗-电商策划
Original
450 people have browsed it

使用流程控制替换语法

将演示网站的首页和产品页进行动态替换

  1. 创建一个data文件夹
    文件夹里面创建header.php和items.php
  2. 在对应的相关文件位置进行替换,代码如下:

header.php

  1. <?php
  2. return [
  3. [
  4. 'id' => 1,
  5. 'url' => 'index.php',
  6. 'title' => '首页',
  7. ],
  8. [
  9. 'id' => 2,
  10. 'url' => 'news.php',
  11. 'title' => '新闻',
  12. ],
  13. [
  14. 'id' => 3,
  15. 'url' => 'items.php',
  16. 'title' => '产品',
  17. ],
  18. [
  19. 'id' => 4,
  20. 'url' => 'contact.php',
  21. 'title' => '联系',
  22. ],
  23. [
  24. 'id' => 5,
  25. 'url' => 'login.php',
  26. 'title' => '登陆',
  27. ]
  28. ]
  29. ?>

items.php

  1. <?php
  2. // 索引数组语义化:关联数组
  3. return [
  4. [
  5. 'id' => 1,
  6. 'title' => '微信小程序1',
  7. 'img' => 'static/images/item1.jpeg',
  8. ],
  9. [
  10. 'id' => 2,
  11. 'title' => '网站设计2',
  12. 'img' => 'static/images/item2.jpeg',
  13. ],
  14. [
  15. 'id' => 3,
  16. 'title' => '微信小程序3',
  17. 'img' => 'static/images/item3.jpeg',
  18. ],
  19. [
  20. 'id' => 4,
  21. 'title' => '微信小程序4',
  22. 'img' => 'static/images/item4.jpeg',
  23. ],
  24. [
  25. 'id' => 5,
  26. 'title' => '微信小程序5',
  27. 'img' => 'static/images/item5.jpeg',
  28. ],
  29. [
  30. 'id' => 6,
  31. 'title' => '微信小程序6',
  32. 'img' => 'static/images/item6.jpeg',
  33. ],
  34. [
  35. 'id' => 7,
  36. 'title' => '微信小程序7',
  37. 'img' => 'static/images/item7.jpeg',
  38. ],
  39. [
  40. 'id' => 8,
  41. 'title' => '微信小程序8',
  42. 'img' => 'static/images/item8.jpeg',
  43. ],
  44. ];

插入文件相应位置 index.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>首页</title>
  8. <link rel="stylesheet" href="static/css/style.css" />
  9. </head>
  10. <body>
  11. <header>
  12. <nav>
  13. <?php
  14. $news = include './data/header.php';
  15. ?>
  16. <?php foreach ($news as $new) : ?>
  17. <a href="<?= $new['url'] ?>"><?= $new['title'] ?></a>
  18. <?php endforeach ?>
  19. </nav>
  20. </header>
  21. <!-- 主体 -->
  22. <main>
  23. <!-- 产品列表 -->
  24. <?php
  25. $items = include './data/items.php';
  26. ?>
  27. <div class="items">
  28. <h3>产品列表</h3>
  29. <div class="list">
  30. <?php foreach ($items as $item) : extract($item) ?>
  31. <div class="item">
  32. <img src="<?= $img ?>" alt="" />
  33. <a href=""><?= $title ?></a>
  34. </div>
  35. <?php endforeach ?>
  36. </div>
  37. </div>
  38. <!-- 分页条 -->
  39. <p class="pagebar">
  40. <a href="">1</a>
  41. <a href="">...</a>
  42. <a href="">6</a>
  43. <a href="">7</a>
  44. <a href="" class="active">8</a>
  45. <a href="">9</a>
  46. <a href="">10</a>
  47. <a href="">...</a>
  48. <a href="">20</a>
  49. </p>
  50. </main>
  51. <?php
  52. include './data/footer.php'
  53. ?>
  54. </body>
  55. </html>

效果图:


Correcting teacher:PHPzPHPz

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
Author's latest blog post