Hi,
I am trying to do a script that is must print a tree of object (my
objects are some comments).
For more details, a "comment" is an object which can be at the top of
the tree (father), or which can be an answer to another comment
(children). There is no limit in father-children relation (a father can
have children, which can have children, which ...).
I have made 2 object, the comment and a comment contener (just an
array). Un comment is simply composed with a texte and an id.
Here is the code and my test failure.
I want to display the tree (commen id):
1->4
->5->7->8->9
->6
2->
3->
Explain : commen 1 has 3 answers (4-5-6), 2 & 3 have not answer. Comment
5 has one answer (7) which has one answer(8) ...
/*------------------------------------------*/
<?php
classe comments
{//contener
//comments array
var $tab_comments;
function comments()
{//constructor
$this->tab_comments=array();
}
function addcomment($comment)
{//add a comment into the array
$this->tab_comments[]=$comment;
}
}//end comments
class comment
{//the comment
var $id;
var $contenu;
//children array
var $tab_comments;
function comment($num, $texte)
{//constructor
$this->id=$num;
$this->contenu=$texte;
$this->tab_comments=array();
}
function outprint()
{
echo "I am comment number {$this->id}<br>";
$this->printFils();
}
function printFils()
{ //print all children
foreach ($this->tab_comments as $comment)
{
$comment->outprint();
}
}
function addcomment($comment)
{//add a children
$this->tab_comments[]=$comment;
}
}//fin classe comment
//Here is the test
//contener
$commentaires=new comments();
//comments
$commentaire1=new comment('1','commentaire 1');
$commentaire2=new comment('2','commentaire 2');
$commentaire3=new comment('3','commentaire 3');
$commentaire4=new comment('4','commentaire 4');
$commentaire5=new comment('5','commentaire 5');
$commentaire6=new comment('6','commentaire 6');
$commentaire7=new comment('7','commentaire 7');
$commentaire8=new comment('8','commentaire 8');
$commentaire9=new comment('9','commentaire 9');
//tree
//add fathers to contener
$commentaires->addcomment($commentaire1);
$commentaires->addcomment($commentaire2);
$commentaires->addcomment($commentaire3);
//children-father relationship
$commentaire1->addcomment($commentaire4);
$commentaire1->addcomment($commentaire5);
$commentaire1->addcomment($commentaire6);
$commentaire5->addcomment($commentaire7);
$commentaire5->addcomment($commentaire8);
$commentaire5->addcomment($commentaire9);
//Display $commentaire1
$commentaire1->outprint();
/* end test */
?>
The result is :
I am comment number 1
I am comment number 4
I am comment number 5
I am comment number 6
This mean that the tree is not completly read and that I need help !!
Best regards
Pepete