Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the solution that might be helpfull:</p> <p>First you need 2 helper recursive function:</p> <pre><code>// Building comments. function buildComments($list, $parent = 0) { // Creating result array. $result = array(); //looping... foreach ($list as $item) { //iteration starts with 0 as default. if ($item-&gt;parent == $parent) { // add to the result $result[$item-&gt;commentId] = array( 'author' =&gt; $item-&gt;author, // ... other definitions 'child' =&gt; buildComments($list, $item-&gt;commentId) //execute this function for child. ); } } return $result; } function printComments($arg, $depth = 1) { foreach ($arg as $item) { // Printing comment... echo str_repeat('&amp;nbsp; ', $depth) . $item['author'] . "&lt;br /&gt;\r\n"; // extra echoes... // if it has a child comment... if (count($item['child'] &gt; 0)) { printComments($item['child'], $depth + 1); } } } </code></pre> <p>A little explaining:</p> <p>The <code>buildComments()</code> function will starts with rows that parents has 0. Then it will execute itself for child. if child as a child, it will add it. In the end, result will be like this:</p> <pre><code>$result = array( 1 =&gt; array( 'author' =&gt; 'John', 'child' =&gt; array( 8 =&gt; array( 'author' =&gt; 'Jane', 'child' =&gt; array( 3 =&gt; array( 'author' =&gt; 'Jamie', 'child =&gt; array() ) ) ), 6 =&gt; array( 'author' =&gt; 'Jackie', 'child =&gt; array() ), 9 =&gt; array( 'author' =&gt; 'Harry', 'child =&gt; array() ) ) ), 4 =&gt; array( 'author' =&gt; 'Jack', 'child' =&gt; array() ), 10 =&gt; array( 'author' =&gt; 'Clark', 'child' =&gt; array( 11 =&gt; array( 'author =&gt; 'Lois', 'child' =&gt; array() ) ) ), 12 =&gt; array( 'author' =&gt; 'Luthor', 'child' =&gt; array() ) ); </code></pre> <p>In the <code>printComments()</code> function we are printing results recursive. for each child, function repeats itself. You will get result like this:</p> <pre><code> John Jane Jamie Jackie Harry Jack Clark Lois Luthor </code></pre> <p>For more information about recursive functions Look this <a href="https://stackoverflow.com/questions/2648968/what-is-a-recursive-function-in-php">answer</a></p> <p><strong>USAGE</strong></p> <pre><code>$this-&gt;db-&gt;where('postName',$postName); $this-&gt;db-&gt;where('year(date)',$year); $this-&gt;db-&gt;where('month(date)',$month); $this-&gt;db-&gt;order_by('parent', 'asc'); $query = $this-&gt;db-&gt;get('comments'); $comments = buildComments($query-&gt;result()); printComments($comments); </code></pre> <p>that'is that simple...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload