2015-09-05 73 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