编程思路
1、分析页面哪些数据高度重复;
2、将数据存在数据库对应表里,然后获取为数组;
3、页面遍历数组数据实现网站数据动态填充!
作业总结
这次网页动态化我主要解决了两个问题:1、网页上哪些数据存放在数据库里;2、数据表如何设计比较好!
我的方法是,能够之后从表里获取为数组,方便循环遍历的数据我整理进数据表里,一个模块一张表,实在不行就两张,因为我想到的是“数据获取为二维数组填充网页”!整个首页,只能说是80%数据填充了,其他的我把它看做为“骨架”,就放在页面里了!
作业问题
这次作业有两个问题要请教老师。
1、商业项目动态化网站,主要是把数据作为二维数据来填充网页吗,还有其他方法吗?
2、这次我每个模块都单独写了获取数据库代码,我尝试全写一起,但是失败了,如何一张表呢?
蜂鸟网首页php改写
1、导航模块
- 数据表和效果图
- 从数据库获取数据
<?php
namespace PDO;
use pdo;
//1.连接数据库
$pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
//2.操作数据库
//2.1准备sql语句
$sql = "SELECT * FROM `index_main_nav`;";
//2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
$stmt = $pdo->prepare($sql);
//2.3执行sql语句
$stmt->execute();
//2.4数据保存为数组
$navs = [];
while ($nav = $stmt->fetch(PDO::FETCH_ASSOC)) {
// echo '<pre>' . print_r($comment,true) . '</pre>';
$navs[] = $nav;
}
//var_dump($navs);
//echo '<hr>';
//3.关闭数据库
unset($pdo);
?>
- php重写
<?php include "../components/index/index_header/index_header_connect.php"?>
<?php include "../components/index/index_header/index_icon_connect.php"?>
<!-- 首页头部导航与轮播-->
<div class="index-header">
<div class="content">
<!--logo与搜索框,快速入口-->
<div class="log-search">
<a href="" class="logo"><img src="../static/images/logo.png" alt=""></a>
<div class="search">
<input type="search" name="search" id="search">
<label for="search" class="iconfont icon-jinduchaxun"></label>
</div>
<div class="quick-entry">
<?php foreach ($icons as $icon) : ?>
<a href="" class="<?php echo $icon['icon_url'] ?>"></a>
<?php endforeach ?>
</div>
</div>
<!--主导航-->
<div class="main-nav">
<?php foreach($navs as $nav) : ;?>
<!--导航详情1-->
<div class="nav-detail">
<!--左侧图标与描述-->
<div class="pic">
<span class="<?php echo $nav['nav_class'] ?>"></span>
<div>
<span><?php echo $nav['nav_big1'] ?></span>
<span><?php echo $nav['nav_big2'] ?></span>
</div>
</div>
<!--右侧导航链接-->
<div class="links">
<a href=""><?php echo $nav['nav_name1'] ?></a>
<a href=""><?php echo $nav['nav_name2'] ?></a>
<a href=""><?php echo $nav['nav_name3'] ?></a>
<a href=""><?php echo $nav['nav_name4'] ?></a>
<a href=""><?php echo $nav['nav_name5'] ?></a>
<a href=""><?php echo $nav['nav_name6'] ?></a>
<a href=""><?php echo $nav['nav_name7'] ?></a>
<a href=""><?php echo $nav['nav_name8'] ?></a>
</div>
</div>
<?php endforeach; ?>
</div>
<!--轮播图-->
<div class="slider">
<a href=""><img src="../static/images/2.jpg" alt=""></a>
<a href=""><img src="../static/images/banner-right.jpg" alt=""></a>
</div>
</div>
</div>
2、新闻模块
- 数据表和效果图
- 从数据库获取数据
<?php
namespace PDO;
use function MongoDB\BSON\fromJSON;
use pdo;
//1.连接数据库
$pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
//2.操作数据库
//2.1准备sql语句
$sql = "SELECT * FROM `index_news`;";
//2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
$stmt = $pdo->prepare($sql);
//2.3执行sql语句
$stmt->execute();
//2.4数据保存为数组
$news = [];
while ($new = $stmt->fetch(PDO::FETCH_ASSOC)) {
// echo '<pre>' . print_r($comment,true) . '</pre>';
$news[] = $new;
}
//var_dump($news);
//echo '<hr>';
//3.关闭数据库
unset($pdo);
//处理数组
$newNews = [];
foreach ($news as $newNew) {
if ($newNew['new_name'] == "新闻") {
$newNews[] = $newNew;
}
}
$newSales = [];
foreach ($news as $newSale) {
if ($newSale['new_name'] == "促销") {
$newSales[] = $newSale;
}
}
//var_dump($newSales);
?>
- php重写
<?php include "../components/index/index_news/index_news_connect.php"?>
<!--引入大标题组件-->
<div class="public-headline">
<span>新闻资讯</span>
</div>
<div class="index-news">
<!-- 左侧图片列表-->
<div class="imgs">
<a href=""><img src="../static/images/news.jpg" alt="" class="first-img"></a>
<div>
<a href=""><img src="../static/images/n-2.jpg" alt=""></a>
<a href="">三星Note10发布搭载挖孔前摄</a>
</div>
<div>
<a href=""><img src="../static/images/n-3.jpg" alt=""></a>
<a href="">小米公布1亿像素手机信息</a>
</div>
</div>
<!-- 中间与右边信息列表共用样式-->
<div class="list">
<a href="">大隐于市的摄影师,薇薇安·迈尔</a>
<ul>
<?php foreach ($newNews as $newNew) : ?>
<li><span>[新闻]</span><a href=""><?php echo $newNew['new_title'] ?></a></li>
<?php endforeach ?>
</ul>
</div>
<div class="list">
<a href="">元旦春节,双节联动,备年货啦!!</a>
<ul>
<?php foreach ($newSales as $newSale) : ?>
<li><span>[促销]</span><a href=""><?php echo $newSale['new_title'] ?></a></li>
<?php endforeach ?>
</ul>
</div>
</div>
3.图片模块
数据表和效果图
获取数据
<?php
namespace PDO;
use function MongoDB\BSON\fromJSON;
use pdo;
//1.连接数据库
$pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
//2.操作数据库
//2.1准备sql语句
$sql = "SELECT * FROM `public_img_list`;";
//2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
$stmt = $pdo->prepare($sql);
//2.3执行sql语句
$stmt->execute();
//2.4数据保存为数组
$imgs = [];
while ($img = $stmt->fetch(PDO::FETCH_ASSOC)) {
// echo '<pre>' . print_r($comment,true) . '</pre>';
$imgs[] = $img;
}
//var_dump($imgs);
//echo '<hr>';
//3.关闭数据库
unset($pdo);
//处理数组
$imgJks = [];
foreach ($imgs as $imgJk) {
if ($imgJk['img_name'] == "健康") {
$imgJks[] = $imgJk;
}
}
$imgMns = [];
foreach ($imgs as $imgMn) {
if ($imgMn['img_name'] == "美女") {
$imgMns[] = $imgMn;
}
}
$imgQcs = [];
foreach ($imgs as $imgQc) {
if ($imgQc['img_name'] == "青春") {
$imgQcs[] = $imgQc;
}
}
//var_dump($imgMns);
const IMG_PATH = "../static/images/";
?>
- php重写
<?php include "../components/public/public_image_list/public_img_connect.php"?>
<div class="public-headline">
<span>图片专区</span>
</div>
<!-- 图文列表专区-->
<div class="public-image-list">
<div class="list">
<div class="title">
<a href="">美女</a>
<span>纵观摄影艺术</span>
</div>
<div class="img-list">
<?php foreach ($imgMns as $imgMn) : ?>
<div>
<a href=""><img src="<?php echo \PDO\IMG_PATH.$imgMn['img_url']?>" alt=""></a>
<a href=""><?php echo $imgMn['img_title']?></a>
</div>
<?php endforeach ?>
</div>
</div>
<div class="list">
<div class="title">
<a href="">健康</a>
<span>纵观摄影艺术</span>
</div>
<div class="img-list">
<?php foreach ($imgJks as $imgJk) : ?>
<div>
<a href=""><img src="<?php echo \PDO\IMG_PATH.$imgJk['img_url']?>" alt=""></a>
<a href=""><?php echo $imgJk['img_title']?></a>
</div>
<?php endforeach ?>
</div>
</div>
<div class="list">
<div class="title">
<a href="">青春</a>
<span>纵观摄影艺术</span>
</div>
<div class="img-list">
<?php foreach ($imgQcs as $imgQc) : ?>
<div>
<a href=""><img src="<?php echo \PDO\IMG_PATH.$imgQc['img_url']?>" alt=""></a>
<a href=""><?php echo $imgQc['img_title']?></a>
</div>
<?php endforeach ?>
</div>
</div>
</div>
4.二手交易模块
- 数据库表和效果图
- 获取数据
<?php
namespace PDO;
use function MongoDB\BSON\fromJSON;
use pdo;
//1.连接数据库
$pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
//2.操作数据库
//2.1准备sql语句
$sql = "SELECT * FROM `public_second_hand`;";
//2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
$stmt = $pdo->prepare($sql);
//2.3执行sql语句
$stmt->execute();
//2.4数据保存为数组
$goods = [];
while ($good = $stmt->fetch(PDO::FETCH_ASSOC)) {
// echo '<pre>' . print_r($comment,true) . '</pre>';
$goods[] = $good;
}
//var_dump($goods);
//echo '<hr>';
//3.关闭数据库
unset($pdo);
const SCIMG_PATH = "../static/images/shop/";
//echo SCIMG_PATH;
?>
- php重写
<?php include "../components/public/public_second_hand/public_sc_hand_connect.php"?>
<!--引入大标题组件-->
<div class="public-headline">
<span>二手交易</span>
</div>
<!--二手交易专区-->
<div class="public-second-hand">
<!-- 标题1-->
<div class="title1">
<a href="">抢好货</a>
<span>0低价, 便捷,安全,快速</span>
</div>
<!-- 标题2-->
<div class="title2">
<span>热门分类</span>
<a href="">美女写真</a>
<a href="">日本美女</a>
<a href="">美国美女</a>
<a href="">国内美女</a>
<a href="">AV美女</a>
</div>
<!--商品展示区-->
<div class="goods">
<!--左侧商品列表-->
<div class="goods-list">
<?php foreach ($goods as $good): ?>
<!-- 商品简介-->
<div class="intro">
<a href=""><img src="<?php echo \PDO\SCIMG_PATH.$good['sc_hand_url'] ?>" alt="" width="176" height="120"></a>
<a href=""><?php echo $good['sc_hand_title'] ?></a>
<div>
<span>¥<?php echo $good['sc_hand_price'] ?></span>
<span><?php echo $good['sc_hand_tip'] ?></span>
</div>
</div>
<?php endforeach ?>
</div>
<!--右侧功能快速入口-->
<div class="quick-entry">
<a href=""><img src="../static/images/ad/1.png" alt=""></a>
<a href=""><img src="../static/images/ad/2.png" alt=""></a>
<a href=""><img src="../static/images/ad/3.png" alt=""></a>
<a href=""><img src="../static/images/ad/4.png" alt=""></a>
<div>
<a href=""><img src="../static/images/ad/image.png" alt=""></a>
<a href=""><img src="../static/images/ad/ad2.jpg" alt=""></a>
</div>
</div>
</div>
</div>
5.合作单位模块
数据库表和效果图
获取数据
<?php
namespace PDO;
use pdo;
//1.连接数据库
$pdo = new pdo('mysql:host=localhost;dbname=phpedu', 'root', 'root');
//2.操作数据库
//2.1准备sql语句
$sql = "SELECT * FROM `index_friend_links`;";
//2.2创建这个sql语句的模板对象,预处理对象,防止SQL注入攻击
$stmt = $pdo->prepare($sql);
//2.3执行sql语句
$stmt->execute();
//2.4数据保存为数组
$links = [];
while ($link = $stmt->fetch(PDO::FETCH_ASSOC)) {
// echo '<pre>' . print_r($comment,true) . '</pre>';
$links[] = $link;
}
//var_dump($navs);
//echo '<hr>';
//3.关闭数据库
unset($pdo);
?>
- php重写
<?php include "../components/index/index_friend_links/index_friend_connect.php"?>
<!--合作单位大标题-->
<div class="public-headline">
<span>合作单位</span>
</div>
<!--合作友情链接-->
<div class="index-frend-links">
<?php foreach ($links as $link) : ?>
<a href="<?php echo $link['link_url'] ?>"><?php echo $link['link_name'] ?></a>
<?php endforeach ?>
</div>