Blogger Information
Blog 18
fans 0
comment 0
visits 10875
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
春节大作业
- 迷舍人
Original
818 people have browsed it

春节大作业,用数据库里面的数据渲染自己的网站

整站效果图

整站代码

  1. <?php include 'public/config.php' ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title><?php echo $mysql->select_site()['name'] ?></title>
  7. <link rel="stylesheet" href="index.css">
  8. <link rel="stylesheet" href="static/font/iconfont.css">
  9. </head>
  10. <body>
  11. <!-- 公共头部 -->
  12. <?php include __DIR__ .'/public/public_header/public_header.php' ?>
  13. <!-- 主体 -->
  14. <main>
  15. <!-- 公共头部 -->
  16. <div class="index-header">
  17. <!-- logo+搜索框+快捷入口 -->
  18. <div class="content">
  19. <a href="">
  20. <img src="static/images/logo1.png" alt="">
  21. </a>
  22. <div class="search">
  23. <input type="search" id="search" placeholder="搜索文章,帖子">
  24. <label class="iconfont icon-sousuo1" for="search"></label>
  25. </div>
  26. <div class="quick-entry">
  27. <a href="" class="iconfont icon-huiyuan1"></a>
  28. <a href="" class="iconfont icon-danmu1"></a>
  29. <a href="" class="iconfont icon-fabu"></a>
  30. <a href="" class="iconfont icon-fangda"></a>
  31. <a href="" class="iconfont icon-huiyuan2"></a>
  32. <a href="" class="iconfont icon-dianzan"></a>
  33. </div>
  34. </div>
  35. <!-- 导航详情 -->
  36. <div class="main-nav">
  37. <div class="nav-detail">
  38. <div class="pic">
  39. <span class="iconfont icon-gongdan"></span>
  40. <div>
  41. <span>资讯</span>
  42. <span>学习</span>
  43. </div>
  44. </div>
  45. <div class="links">
  46. <?php foreach ($mysql->select_main_links() as $key => $links): ?>
  47. <?php if($links['cid'] === number_format(1)) : ?>
  48. <a href=""><?php echo $links['name'] ?></a>
  49. <?php endif ?>
  50. <?php endforeach ?>
  51. </div>
  52. </div>
  53. <div class="nav-detail">
  54. <div class="pic">
  55. <span class="iconfont icon-gongdan"></span>
  56. <div>
  57. <span>爱好</span>
  58. <span>姐妹</span>
  59. </div>
  60. </div>
  61. <div class="links">
  62. <?php foreach ($mysql->select_main_links() as $key => $links): ?>
  63. <?php if($links['cid'] === number_format(2)) : ?>
  64. <a href=""><?php echo $links['name'] ?></a>
  65. <?php endif ?>
  66. <?php endforeach ?>
  67. </div>
  68. </div>
  69. <div class="nav-detail">
  70. <div class="pic">
  71. <span class="iconfont icon-gongdan"></span>
  72. <div>
  73. <span>软件</span>
  74. <span>技能</span>
  75. </div>
  76. </div>
  77. <div class="links">
  78. <?php foreach ($mysql->select_main_links() as $key => $links): ?>
  79. <?php if($links['cid'] === number_format(3)) : ?>
  80. <a href=""><?php echo $links['name'] ?></a>
  81. <?php endif ?>
  82. <?php endforeach ?>
  83. </div>
  84. </div>
  85. <div class="nav-detail">
  86. <div class="pic">
  87. <span class="iconfont icon-gongdan"></span>
  88. <div>
  89. <span>编程</span>
  90. <span>美女</span>
  91. </div>
  92. </div>
  93. <div class="links">
  94. <?php foreach ($mysql->select_main_links() as $key => $links): ?>
  95. <?php if($links['cid'] === number_format(4)) : ?>
  96. <a href=""><?php echo $links['name'] ?></a>
  97. <?php endif ?>
  98. <?php endforeach ?>
  99. </div>
  100. </div>
  101. </div>
  102. <!-- 轮播图 -->
  103. <div class="slider">
  104. <a href="">
  105. <img src="static/images/2.jpg" alt="">
  106. </a>
  107. <a href="">
  108. <img src="static/images/banner-right.jpg" alt="">
  109. </a>
  110. </div>
  111. </div>
  112. <!-- 大标题 -->
  113. <div class="public-headline">
  114. <span>新闻咨询</span>
  115. </div>
  116. <!-- 新闻资讯 -->
  117. <div class="news">
  118. <div class="details">
  119. <a href="">新闻分类1</a>
  120. <ul>
  121. <?php foreach ($mysql->select_news() as $key => $news) :?>
  122. <?php if($news['cid'] === number_format(1)) : ?>
  123. <li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
  124. <?php endif ?>
  125. <?php endforeach?>
  126. </ul>
  127. </div>
  128. <div class="details">
  129. <a href="">新闻分类2</a>
  130. <ul>
  131. <?php foreach ($mysql->select_news() as $key => $news) :?>
  132. <?php if($news['cid'] === number_format(2)) : ?>
  133. <li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
  134. <?php endif ?>
  135. <?php endforeach?>
  136. </ul>
  137. </div>
  138. <div class="details">
  139. <a href="">新闻分类3</a>
  140. <ul>
  141. <?php foreach ($mysql->select_news() as $key => $news) :?>
  142. <?php if($news['cid'] === number_format(3)) : ?>
  143. <li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
  144. <?php endif ?>
  145. <?php endforeach?>
  146. </ul>
  147. </div>
  148. </div>
  149. <!-- 友情链接 -->
  150. <div class="public-headline">
  151. <span>友情链接</span>
  152. </div>
  153. <div class="friend">
  154. <?php foreach ($mysql->select_friend() as $key => $friend) :?>
  155. <a href=""><?php echo $friend['name'] ?></a>
  156. <?php endforeach ?>
  157. </div>
  158. </main>
  159. <!-- 公共脚部 -->
  160. <?php include __DIR__ .'/public/public_footer/public_footer.php' ?>
  161. </body>
  162. </html>

自己封装的数据库类

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. class mysql
  5. {
  6. private $db;
  7. private $username;
  8. private $password;
  9. private $pdo;
  10. private $sql;
  11. private $stmt;
  12. public function __construct()
  13. {
  14. $this->dn = 'mysql:host=localhost;dbname=zuoye';
  15. $this->username = 'root';
  16. $this->password = 'root';
  17. $this->pdo = new PDO($this->dn, $this->username, $this->password);
  18. }
  19. // 关闭数据库链接
  20. public function __destruct()
  21. {
  22. unset($pdo);
  23. }
  24. // 执行sql语句
  25. public function query()
  26. {
  27. $stmt = $this->pdo->prepare($this->sql);
  28. $stmt->execute();
  29. return $stmt;
  30. }
  31. // 创建sql语句
  32. // 查询头部
  33. public function select_site()
  34. {
  35. $this->sql = "SELECT * FROM `site` where `id` = 1";
  36. $this->query();
  37. return $this->query()->fetch(PDO::FETCH_ASSOC);
  38. }
  39. // 查询头部导航
  40. public function select_header_site()
  41. {
  42. $this->sql = "SELECT * FROM `header_site`";
  43. $this->query();
  44. return $this->query()->fetchAll(PDO::FETCH_ASSOC);
  45. }
  46. // 查询主体导航
  47. public function select_main_links()
  48. {
  49. $this->sql = "SELECT * FROM `main_links`";
  50. $this->query();
  51. return $this->query()->fetchAll(PDO::FETCH_ASSOC);
  52. }
  53. // 查询赞助
  54. public function select_friend()
  55. {
  56. $this->sql = "SELECT * FROM `friend`";
  57. $this->query();
  58. return $this->query()->fetchAll(PDO::FETCH_ASSOC);
  59. }
  60. //查询新闻全部
  61. public function select_news()
  62. {
  63. $this->sql = "SELECT * FROM `news`";
  64. $this->query();
  65. return $this->query()->fetchAll(PDO::FETCH_ASSOC);
  66. }
  67. }
  68. $mysql = new mysql();

数据库表结构





Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:自己封装的类不错, 但不要再用关键字pdo当空间名称, 这样风险极大
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