春节大作业,用数据库里面的数据渲染自己的网站
整站效果图
整站代码
<?php include 'public/config.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $mysql->select_site()['name'] ?></title>
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="static/font/iconfont.css">
</head>
<body>
<!-- 公共头部 -->
<?php include __DIR__ .'/public/public_header/public_header.php' ?>
<!-- 主体 -->
<main>
<!-- 公共头部 -->
<div class="index-header">
<!-- logo+搜索框+快捷入口 -->
<div class="content">
<a href="">
<img src="static/images/logo1.png" alt="">
</a>
<div class="search">
<input type="search" id="search" placeholder="搜索文章,帖子">
<label class="iconfont icon-sousuo1" for="search"></label>
</div>
<div class="quick-entry">
<a href="" class="iconfont icon-huiyuan1"></a>
<a href="" class="iconfont icon-danmu1"></a>
<a href="" class="iconfont icon-fabu"></a>
<a href="" class="iconfont icon-fangda"></a>
<a href="" class="iconfont icon-huiyuan2"></a>
<a href="" class="iconfont icon-dianzan"></a>
</div>
</div>
<!-- 导航详情 -->
<div class="main-nav">
<div class="nav-detail">
<div class="pic">
<span class="iconfont icon-gongdan"></span>
<div>
<span>资讯</span>
<span>学习</span>
</div>
</div>
<div class="links">
<?php foreach ($mysql->select_main_links() as $key => $links): ?>
<?php if($links['cid'] === number_format(1)) : ?>
<a href=""><?php echo $links['name'] ?></a>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
<div class="nav-detail">
<div class="pic">
<span class="iconfont icon-gongdan"></span>
<div>
<span>爱好</span>
<span>姐妹</span>
</div>
</div>
<div class="links">
<?php foreach ($mysql->select_main_links() as $key => $links): ?>
<?php if($links['cid'] === number_format(2)) : ?>
<a href=""><?php echo $links['name'] ?></a>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
<div class="nav-detail">
<div class="pic">
<span class="iconfont icon-gongdan"></span>
<div>
<span>软件</span>
<span>技能</span>
</div>
</div>
<div class="links">
<?php foreach ($mysql->select_main_links() as $key => $links): ?>
<?php if($links['cid'] === number_format(3)) : ?>
<a href=""><?php echo $links['name'] ?></a>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
<div class="nav-detail">
<div class="pic">
<span class="iconfont icon-gongdan"></span>
<div>
<span>编程</span>
<span>美女</span>
</div>
</div>
<div class="links">
<?php foreach ($mysql->select_main_links() as $key => $links): ?>
<?php if($links['cid'] === number_format(4)) : ?>
<a href=""><?php echo $links['name'] ?></a>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
</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 class="public-headline">
<span>新闻咨询</span>
</div>
<!-- 新闻资讯 -->
<div class="news">
<div class="details">
<a href="">新闻分类1</a>
<ul>
<?php foreach ($mysql->select_news() as $key => $news) :?>
<?php if($news['cid'] === number_format(1)) : ?>
<li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
<?php endif ?>
<?php endforeach?>
</ul>
</div>
<div class="details">
<a href="">新闻分类2</a>
<ul>
<?php foreach ($mysql->select_news() as $key => $news) :?>
<?php if($news['cid'] === number_format(2)) : ?>
<li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
<?php endif ?>
<?php endforeach?>
</ul>
</div>
<div class="details">
<a href="">新闻分类3</a>
<ul>
<?php foreach ($mysql->select_news() as $key => $news) :?>
<?php if($news['cid'] === number_format(3)) : ?>
<li><span>[新闻]</span><a href="<?php echo $news['url'] ?>"><?php echo $news['title'] ?></a></li>
<?php endif ?>
<?php endforeach?>
</ul>
</div>
</div>
<!-- 友情链接 -->
<div class="public-headline">
<span>友情链接</span>
</div>
<div class="friend">
<?php foreach ($mysql->select_friend() as $key => $friend) :?>
<a href=""><?php echo $friend['name'] ?></a>
<?php endforeach ?>
</div>
</main>
<!-- 公共脚部 -->
<?php include __DIR__ .'/public/public_footer/public_footer.php' ?>
</body>
</html>
自己封装的数据库类
<?php
namespace pdo;
use PDO;
class mysql
{
private $db;
private $username;
private $password;
private $pdo;
private $sql;
private $stmt;
public function __construct()
{
$this->dn = 'mysql:host=localhost;dbname=zuoye';
$this->username = 'root';
$this->password = 'root';
$this->pdo = new PDO($this->dn, $this->username, $this->password);
}
// 关闭数据库链接
public function __destruct()
{
unset($pdo);
}
// 执行sql语句
public function query()
{
$stmt = $this->pdo->prepare($this->sql);
$stmt->execute();
return $stmt;
}
// 创建sql语句
// 查询头部
public function select_site()
{
$this->sql = "SELECT * FROM `site` where `id` = 1";
$this->query();
return $this->query()->fetch(PDO::FETCH_ASSOC);
}
// 查询头部导航
public function select_header_site()
{
$this->sql = "SELECT * FROM `header_site`";
$this->query();
return $this->query()->fetchAll(PDO::FETCH_ASSOC);
}
// 查询主体导航
public function select_main_links()
{
$this->sql = "SELECT * FROM `main_links`";
$this->query();
return $this->query()->fetchAll(PDO::FETCH_ASSOC);
}
// 查询赞助
public function select_friend()
{
$this->sql = "SELECT * FROM `friend`";
$this->query();
return $this->query()->fetchAll(PDO::FETCH_ASSOC);
}
//查询新闻全部
public function select_news()
{
$this->sql = "SELECT * FROM `news`";
$this->query();
return $this->query()->fetchAll(PDO::FETCH_ASSOC);
}
}
$mysql = new mysql();
数据库表结构