Ho trovato questa classe per un commento filettato utilizzando PHP e MySQL:HTML separato dalla classe commenti php
<?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);
}
}
}
questo ha lavorato con questa matrice:
$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')
);
e per il risultato:
$tc = new Threaded_comments($comment);
$tc->print_comments();
e il risultato è:
Parent
Child
Child Third level
Second Parent
Second Child
ha funzionato correttamente, ma per defnigare la lista di commenti (html) ho bisogno di aggiungere tutto il codice html in private function format_comment($comment, $depth)
. questo non è un buon modo e penso che sia necessario separare lo html
da php class
.
EDIT: (questa è la mia pagina per mostra/stampa commento filettata)
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();
nella mia pagina filettato commenti mostrare con format_comment
e lavorare vero. nel caso in cui ho bisogno di progettare l'output utilizzando html separato dalla classe php.
come separare il codice html dalla classe in questo caso ?!
anche tu. MVC funziona alla grande. ma lavoro al progetto e ho bisogno di personalizzare questa classe. Quindi ho bisogno di separare il codice html dalla classe solo in questo caso – NewCod3r
Ho aggiornato la mia risposta un po '. Vedi la soluzione 1 per te. Crea un modello .php con il tuo html e passa l'array a loro. Questo modello contiene solo html e solo foreach/if-php. – schellingerht
vedi la mia modifica? – NewCod3r