I use a template system which is little bit similar to MVC. It uses .tpl files for outputing html. And I am trying to use a comment class with it. for example you create comments.tpl
<THEME Name={Comments} Var={Comment}>
<div class="well">
<VAR>Comment</VAR>
</div>
</THEME>
And to echo this you would do this
<?php
// Comments class
$Comment = new GetComments($dbc, $data['ContentID']);
$Comments = t_Comments($Comment->print_comments());
// Echo
$_PAGE = t_Comments($Comments);
?>
In the comment class it uses simple echo command but my template system does not like echo. it does not show properly unless you use $_PAGE = "";
string
This is the comment class. I tried to change echo with return but it does not show anything. I tried to add return to other private functions it shows only the comment not the parents for example.
Could you guys please give an idea how do I approch this problem or what is the solutions? thanks.
comment_class.php
<?php
class GetComments {
public $contentid;
public $results = array();
public $parents = array();
public $children = array();
protected $db;
public function __construct(PDO $db, $contentid)
{
$sql = $db->prepare(" SELECT * FROM comments WHERE ContentID = :ContentID ");
$sql->execute(array(":ContentID" => $contentid));
while($ROW = $sql->fetch(PDO::FETCH_ASSOC)){
$this->results[] = $ROW;
}
foreach ($this->results as $comment)
{
if ($comment['Parent'] < 1)
{
$this->parents[$comment['CommentsID']][] = $comment;
}
else
{
$this->children[$comment['Parent']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "--";
}
echo $comment['Comment'];
echo "<br />";
}
/**
* @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['CommentsID']]))
{
$this->print_parent($this->children[$c['CommentsID']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
?>