Php 构建基于AJAX的新闻系统

Php 构建基于AJAX的新闻系统,php,ajax,Php,Ajax,我一直在建立一个新闻系统有一段时间了。我将获得每个新闻帖子的标记,如下所示: $newsArray = array(); $result = News::getNews($database, 5); while($row = mysql_fetch_assoc($result)) $newsArray[] = new News($row); foreach($newsArray as $news)

我一直在建立一个新闻系统有一段时间了。我将获得每个新闻帖子的标记,如下所示:

        $newsArray = array();
        $result = News::getNews($database, 5);

        while($row = mysql_fetch_assoc($result))
              $newsArray[] = new News($row);

        foreach($newsArray as $news)
            echo $news->getMarkup($database);
这是接收我的新闻帖子的有效方式吗?我有一个文件,其中包含我的实际新闻文章标记newspost.html,我的getMarkup()函数如下所示:

    public function getMarkup($database) {

        $html = file_get_contents('include/html/newspost.html');

        $find = array("{ID}", "{TITLE}", "{CONTENT}", "{USERNAME}", "{TIME}");
        $replace = array($this->data['news_id'], $this->data['title'], $this->data['content'], $this->data['username'], $this->data['time']);

         for ($i = 0; $i < count($find); ++$i) {
            $html = str_replace($find[$i], $replace[$i], $html);
         }

        return $html;
    }
公共函数getMarkup($database){
$html=file_get_contents('include/html/newspost.html');
$find=array(“{ID}”,“{TITLE}”,“{CONTENT}”,“{USERNAME}”,“{TIME}”);
$replace=array($this->data['news_id'],$this->data['title'],$this->data['content'],$this->data['username'],$this->data['time']);
对于($i=0;$i
显然,我无法获取.php文件,因此我必须制定自己的解决方案,在每篇文章中添加特定的新闻信息。然而,我觉得这是非常不合理的

我很想得到一些关于如何以更优雅的方式解决这个问题的建议。我不想将postnews代码直接嵌入到PHP函数中,因为我不喜欢混合结构和内容


感谢您的反馈

1快速修复,移动
$html=file\u get\u contents('include/html/newspost.html')
getMarkup
函数之外


还有一个想法,在客户端进行html处理,只需将原始post数据(json_encode)发送到ajax代码,让ajax代码进行处理。

看起来基本上没问题。其他评论员指出的问题其实并不是什么大问题。您的数据库查询比再次从内存中抓取文件要慢,而且这里和那里都没有stru_替换


要告诉我们的是AJAX与此有什么关系,在一些地方(比如“无法获取PHP文件”)不清楚您在谈论什么。

1我注意到,您正在阅读每个项目的include/html/newspost.html,这太糟糕了。