加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从php评论类中分离html

发布时间:2020-12-13 17:56:41 所属栏目:PHP教程 来源:网络整理
导读:我发现这个类使用php和 MySQL进行线程评论: ?phpclass Threaded_comments{public $parents = array();public $children = array();/** * @param array $comments */ function __construct($comments){ foreach ($comments as $comment) { if ($comment['par
我发现这个类使用php和 MySQL进行线程评论:
<?php
class Threaded_comments
{

public $parents  = array();
public $children = array();

/**
 * @param array $comments
 */ 
function __construct($comments)
{
    foreach ($comments as $comment)
    {
        if ($comment['parent_id'] === NULL)
        {
            $this->parents[$comment['id']][] = $comment;
        }
        else
        {
            $this->children[$comment['parent_id']][] = $comment;
        }
    }
}

/**
 * @param array $comment
 * @param int $depth
 */ 
private function format_comment($comment,$depth)
{
    for ($depth; $depth > 0; $depth--)
    {
        echo "t";
    }

    echo $comment['text'];
    echo "n";
}

/**
 * @param array $comment
 * @param int $depth
 */ 
private function print_parent($comment,$depth = 0)
{
    foreach ($comment as $c)
    {
        $this->format_comment($c,$depth);

        if (isset($this->children[$c['id']]))
        {
            $this->print_parent($this->children[$c['id']],$depth + 1);
        }
    }
}

public function print_comments()
{
    foreach ($this->parents as $c)
    {
        $this->print_parent($c);
    }
}

}

这适用于这个数组:

$comment = array(  array('id'=>1,'parent_id'=>NULL,'text'=>'Parent'),array('id'=>2,'parent_id'=>1,'text'=>'Child'),array('id'=>3,'parent_id'=>2,'text'=>'Child Third level'),array('id'=>4,'text'=>'Second Parent'),array('id'=>5,'parent_id'=>4,'text'=>'Second Child')  
                );

结果:

$tc = new Threaded_comments($comment);
$tc->print_comments();

结果是:

Parent
    Child
        Child Third level
Second Parent
    Second Child

这工作正常但是对于defign评论列表(html)我需要将所有html代码添加到私有函数format_comment($comment,$depth)中.这不是好方法,我认为需要从php类中单独的html.

编辑:(这是我的show / print线程评论页面)

function _comments_($id,$type){
    $DB_QUERY = mySqli::f("SELECT id,user,email,message,timestamp,parent_id,rfield_1,rfield_2,rfield_3,rfield_4,rfield_5 FROM " . NEWS_COMMENTS . " LEFT JOIN " . NEWS_REVIEWS . " ON " . NEWS_COMMENTS . ".id = " . NEWS_REVIEWS . ".cid WHERE 
    pid = ? AND type = ? AND approved = 1 ORDER BY timestamp DESC LIMIT 12",$id,$type);

        foreach($DB_QUERY as $row){
         $commentdata[] = $row;
        }
    return $commentdata;
}
$comments_list = _comments_('125','book');
foreach($comments_list as $row){
        $comments[] = array(
        'id' => $row['id'],'parent_id' => $row['parent_id'],'name' => $row['user'],'text' => $row['message'],'datetime' => $row['timestamp']
        );
        }

        $tc = new Threaded_comments($comments);
        $tc->print_comments();

在我的页面中,使用format_comment显示线程注释并且工作正常.以防我需要使用php类中的单独html设计输出.

在这种情况下我们如何从类中分离html代码?!

你是对的.

1)最简单的解决方案是将数组传递给模板.此模板不包含逻辑,但仅包含html和php(例如,您的数组的foreach).

2)对于beter分离,使用Model-View-Controller模式:

通过这种层结构,每一层都有自己的职责.永远不要在模型和控制器中使用HTML.仅在模型中查询.

作为Laravel和CodeIgniter的框架具有此模式的实现.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读