Jump to content

extracting database data as an array for use in PHP class for threaded comments


terungwa

Recommended Posts

I need to set up a threaded comments system in a PHP project and I got this script shown below from http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/#comment-436261

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

Here’s the example usage with the data provided as an array:

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

$threaded_comments = new Threaded_comments($comments);

$threaded_comments->print_comments();

I have a sample select query that pulls data from a database and stores the result in the $comments array as shown below. The $comments array is then passed as an argument to the $threaded_comments object:

$sql = 'SELECT * FROM test_comments';
// submit the query and capture the result
$result = $conn->query($sql);
$comments = array();
while ($row = $result->fetch_assoc()) {
    $comments[] = $row;
}

The challenge is that nothing is printed to the screen when I run the script.

Inspection of the comments array with the var_dump function is shown below:

array (size=4)
  0 => 
    array (size=3)
      'id' => string '1' (length=1)
      'parent_id' => string '0' (length=1)
      'text' => string 'comment' (length=7)
  1 => 
    array (size=3)
      'id' => string '2' (length=1)
      'parent_id' => string '0' (length=1)
      'text' => string 'comment' (length=7)
  2 => 
    array (size=3)
      'id' => string '3' (length=1)
      'parent_id' => string '1' (length=1)
      'text' => string 'comment ' (length=
  3 => 
    array (size=3)
      'id' => string '4' (length=1)
      'parent_id' => string '3' (length=1)
      'text' => string 'comment ' (length=

I was wondering if the array format from my select query is the issue? Could anyone provide a clue as to how to fix this?

 

Thanks.

Edited by terungwa
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.