Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separate html from php comments class

Tags:

html

php

mysql

I found this class for threaded comment using php and 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);
    }
}

}

this worked with this array:

$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')  
                );  

and for result:

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

and result is:

Parent
    Child
        Child Third level
Second Parent
    Second Child

this worked true But for defign comment list(html) I need to add all html code into private function format_comment($comment, $depth). this is not good way and I think need separate html from php class.

EDIT:(this is my page for show/print threaded comment)

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();

In my page threaded comments show with format_comment and work true. in case i need to design output using separate html from php class.

how do we separate html code from class in this case?!

like image 388
NewCod3r Avatar asked Sep 05 '15 05:09

NewCod3r


People also ask

How do I comment out a PHP tag?

Single-line PHP Comments To leave a single-line comment, type two forward slashes (//) followed by your comment text. All text to the right of the // will be ignored. You can also use a hash symbol (#) instead of // to make a single-line comment.

How do you comment out a multiple lines of PHP codes?

Similiar to the HTML comment, the multi-line PHP comment can be used to comment out large blocks of code or writing multiple line comments. The multiple line PHP comment begins with " /* " and ends with " */ ".


1 Answers

You're right.

1) Most simple solution is to pass your array to a template. This template doesn't contain logic, but only html and php (e.g. foreach of your your array).

2) For beter separation, use Model-View-Controller pattern:

Simple view of MVC

With this layer construction, each layer has its own responsibilities. Never html in model and controller. Queries in the models only.

Frameworks as Laravel and CodeIgniter have an implementation of this pattern.

like image 182
schellingerht Avatar answered Sep 19 '22 17:09

schellingerht