2015-09-05 81 views
7

我发现这个类螺纹评论使用PHP和MySQL:独立的HTML

<?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, 'parent_id'=>NULL, '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代码添加到private function format_comment($comment, $depth)。这是不好的方式,我认为需要从php class分开html

编辑:(这是我的页面显示/打印螺纹评论)

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代码?

回答

0

你说得对。

1)最简单的解决方案是将你的数组传递给模板。这个模板不包含逻辑,但只包含html和php(例如你的数组的foreach)。

2)对于尤为明显的分离,使用模型 - 视图 - 控制器模式:

Simple view of MVC

采用这种层结构,每一层具有自己的责任。永远不要在模型和控制器的HTML。仅在模型中查询。

作为Laravel和CodeIgniter的框架有这种模式的实现。

+0

你也对。 MVC的工作很好。但我在项目工作,需要定制这个类。所以我需要从课堂上分离HTML代码 – NewCod3r

+0

我更新了一些答案。请参阅解决方案1。用你的html创建一个.php模板并将你的数组传递给它们。这个模板只包含html和简单的foreach/if-php。 – schellingerht

+0

你看我的编辑? – NewCod3r