日期:2014-05-16  浏览次数:20544 次

怎么利用后台来发布文章后,在前台显示。
每次手工在前台写代码 来添加文章,实在很麻烦,也很累人。我想在后台写个功能,输入标题,内容 就可以直接发表。  我想利用文件读取。然后利用正则来读取php文件的某个位置,然后添加文章,比如
我想在index.php显示文章。而index.php的内容为

<div style="float: left;height: 2%;padding-top: 1%;padding-left: 232px;padding-bottom: 4%;"><h1>文章作者——————————————文章标题—————————————文章时间</h1><hr></div>
<table class="article">
<div>
<tr>
<th width="100"><a style="font-size:18px;text-decoration:none;" href="www.baidu.com">Black-Hole</a></th>
<td>
<a style="padding-left: 273px;" href="http://www.baidu.com">PHP代码</a>
</td>
<td>
<a style="padding-left:25%;" href="http://www.baidu.com">2014年2月28号</a>
</td>
</tr>
</div>

我先在后台写个标题 内容 作者  时间。点击发布文章后,在写个正则读取index.php里的article">与<div>之间的内容。擦除内容,写入我刚刚写的 标题 内容 作者 时间。

这样又可以保证新帖子永远在第一位。又省去手工写代码来添加文章的麻烦。

因为 本人学php不到1年。所以,又很多地方不懂,望各位程序员照顾下新人,谢谢了
------解决方案--------------------
这是很基础的东西。
需要用到数据库,可以查看mysql 的dbconnect,select,insert,update,delete语法。
写了个demo,可以插入数据库,从数据库中按时间倒序显示记录,希望对你有帮助。

dbname是 demo

连接数据库
$conn=@mysql_connect("localhost","root","")  or die(mysql_error());
@mysql_select_db('demo',$conn) or die(mysql_error());

localhost 是服务器ip,本机用localhost
root是数据库用户名
密码为空。

db结构

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) unsigned NOT NULL,
  `addtime` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


db.php 用于显示记录

<?php

//打开数据库
function opendb(){
$conn=@mysql_connect("localhost","root","")  or die(mysql_error());
@mysql_select_db('demo',$conn) or die(mysql_error());
}

//关闭数据库
function closedb(){
@mysql_close() or die("關閉數據庫出錯!");
}

    opendb();

    echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';

    $sqlstr = "select * from test order by addtime desc";
    $query = mysql_query($sqlstr) or die(mysql_error());

    while($thread=mysql_fetch_assoc($query)){
        $result[] = $thread;
    }

    if($result){
        foreach($result as $val){
            echo $val['id'].' '.$val['name'].' '.$val['age'].' '.$val['addtime'].'<br>';
        }
    }

?>


add.php 用于新增记录

<?php

//打开数据库
function opendb(){
    $conn=@mysql_connect("localhost","root","")  or die(mysql_error());
    @