3. 新闻系统(1)

来源:互联网 发布:php毕业设计题目 编辑:程序博客网 时间:2024/05/21 07:51

1.创建表

create table news(    id int unsigned primary key auto_increment,/*新闻id*/    title varchar(128) not null default '',/*标题*/    content varchar(256) not null default '',/*新闻内容*/    filename varchar(32) not null default ''/*将来这个新闻对应的静态页面*/)engine=MyISAM charset utf8insert into news(title,content) values('hello1','北京你好');insert into news(title,content) values('hello2','四川你好');

2.代码

2.1 ob 缓存解决方案:

newsList.php

<?php//列出新闻列表$con = mysql_connect('localhost','root','123456');if (!$con) {   die('链接失败<br/>');}mysql_select_db('test3',$con);$sql = "select * from news";$res = mysql_query($sql,$con);while ($row = mysql_fetch_assoc($res)) {    $rows[] = $row;}mysql_free_result($res);mysql_close($con);?><html><head>    <meta http-equiv="content-type:text/html;charset=utf-8"/></head><body><h1>新闻列表</h1><a href="addNews.html">添加新闻</a><br/><table>    <tr>        <td>id</td>        <td>标题</td>        <td>查看内容</td>        <td>修改新闻</td>    </tr>    <?php foreach($rows as $row): ?>        <tr>            <td><?php echo $row['id']; ?></td>            <td><?php echo $row['title']; ?></td>            <td><a href="showNews.php?id=<?php echo $row['id']; ?>"><?php echo $row['content']; ?></a></td>            <td><a href="">修改新闻</a></td>        </tr>    <?php endforeach; ?></table></body></html>

showNews.php

<?php//接收新闻id,传统的方法查询数据库并显示数据$id = $_GET['id'];//先判断该新闻对应的静态页面是否存在,如果有,则直接返回,没有,则查询$html_file = './static/news-id' . $id . '.html';//保证文件30秒有效if (file_exists($html_file) && filemtime($html_file) + 30 > time()) {    echo '静态页面';    echo file_get_contents($html_file);    exit;}//这里大家可以使用工具完成$con = mysql_connect('localhost','root','123456');if (!$con) {    die('链接失败');}mysql_select_db('test3',$con);$sql = "select * from news where id='$id'";$res = mysql_query($sql);?><?php if ($row = mysql_fetch_assoc($res)): ?><?php ob_start(); ?><html><head>    <meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><body>    <table>    <tr>        <td>id</td>        <td>标题</td>        <td>查看内容</td>        <td>静态文件名字</td>    </tr>    <tr>        <td><?php echo $row['id']; ?></td>        <td><?php echo $row['title']; ?></td>        <td><?php echo $row['content']; ?></a></td>        <td><?php echo $row['filename']; ?></td>    </tr>    </table><?php endif; ?></body></html><?php$ob_str = ob_get_contents();file_put_contents('./static/news-id' . $id . '.html',$ob_str);?>

2.2模板技术解决方案:
思路:
这里写图片描述

这里写图片描述

这里写图片描述

0 0
原创粉丝点击