0

I'm having some trouble using a custom library to create threaded comments in Codeigniter 3.

The library is working when i manually declare an array in the controller, however when i try and pass an array from the model, it doesnt.

Example using an array coded into the controller:

$comments = array(  
array (
  'first_name' => 'James',
  'last_name' => 'Smith',
  'country' => 'UK',
  'profile' => 'avatar87.jpg',
  'id' => '1',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => NULL),
   array (
  'first_name' => 'Peter',
  'last_name' => 'Green',
  'country' => 'Australia',
  'profile' => 'avatar88.jpg',
  'id' => '2',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => '1'),
   array (
  'first_name' => 'Ollie',
  'last_name' => 'Ford',
  'country' => 'Australia',
  'profile' => 'avatar85.jpg',
  'id' => '3',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => '1'),
         );

 $this->data['comments'] = $comments;

With the above all is working, however when i try this:

$comments = $this->question_model->get_policy_comments($id);
$this->data['comments'] = $comments;

It doesnt work.

Here is the $comments array returned from the model.

 array (size=3)
  0 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '1' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>This is my comment.Do you agree?</p>' (length=39)
      'submitted' => string '2015-09-22 07:20:21' (length=19)
      'parent_id' => string 'NULL' (length=4)
  1 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '2' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>Blah blah blah</p>' (length=21)
      'submitted' => string '2015-09-22 15:00:00' (length=19)
      'parent_id' => string '1' (length=1)
  2 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '3' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>Hello is it me your looking for.</p>' (length=39)
      'submitted' => string '2015-09-22 13:39:46' (length=19)
      'parent_id' => string '1' (length=1)

Am i missing something with the array?

Any help would be appreciated.

For reference, here the library its using.

 <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Threaded
{

    public $parents  = array();
    public $children = array();

    /**
     * @param array $comments
     */
    public function arrange($comments)
    {
        foreach ($comments as $comment)
        {

            if ($comment['parent_id'] === NULL)
            {
                $this->parents[$comment['id']][] = $comment;
            }
            else
            {
                $this->children[$comment['parent_id']][] = $comment;
            }
        }
        $this->print_comments();
    }

    private function tabulate($depth)
    {
        for ($depth; $depth > 0; $depth--)
        {
           // echo "t";
        }
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function format_comment($comment, $depth)
    {

        //echo "n";

        $this->tabulate($depth+1);

        echo "<li>";
        echo $comment['first_name'];
        echo $comment['last_name'];
        echo $comment['country'];
        echo $comment['profile'];
        echo $comment['member_id'];
        echo $comment['policy_id'];
        echo $comment['comment'];
        echo $comment['submitted'];
        echo $comment['id'];
        echo $comment['parent_id'];
        echo "</li>";
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function print_parent($comment, $depth = 0)
    {
        $this->tabulate($depth);
        echo "<ul>";
        foreach ($comment as $c)
        {
            $this->format_comment($c, $depth);

            if (isset($this->children[$c['id']]))
            {
                $this->print_parent($this->children[$c['id']], $depth + 1);
            }
        }
        $this->tabulate($depth);
        echo "</ul>";
    }

    private function print_comments()
    {
        foreach ($this->parents as $c)
        {
            $this->print_parent($c);
        }
    }

}
1
  • As @DOfficial mentioned, it seems the structure of the array is different. Any idea on how i can get it to match? Commented Sep 23, 2015 at 3:11

2 Answers 2

0

You can assign to comments in a single line

$this->data['comments'] = $this->question_model->get_policy_comments($id);

and just check it by below code and see the pattern of your array

echo '<pre>'; print_r($comments); die();
Sign up to request clarification or add additional context in comments.

Comments

0

So the arrays look to be correct now. However I think your issue is that in your static array you are actually passing a NULL value where in the returned array your 'NULL' value is actually interpreted as a string not NULL.

Before passing the array:

for($i=0; $i<count($comments); $i++){
    foreach($comments[$i] as $k => $v){
        if(strtoupper($v) === 'NULL'){
           $comments[$k] = NULL;
        }
    }
}

or in your model

public function arrange($comments)
{
    foreach ($comments as $comment)
    {

        if ($comment['parent_id'] === NULL || $comment['parent_id']==='NULL')
        {
            $this->parents[$comment['id']][] = $comment;
        }
        else
        {
            $this->children[$comment['parent_id']][] = $comment;
        }
    }
    $this->print_comments();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.