Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If this usecase is a common one in your application, just store the root id (id of the root of the tree. this can be the id of the post to which these comments belong). Now when you need to get the whole tree of comments, you just need to do :</p> <pre><code>SELECT * FROM comments WHERE root_id = &lt;root_id&gt; </code></pre> <p>or an equivalent query for your design. If you provide your table definitions, I can help you out with specific queries.</p> <p>Update:</p> <pre><code>$dbh = new PDO($dsn, $user, $password); $sql = "SELECT A.*, GROUP_CONCAT(descendant) as descendants FROM Comments AS A INNER JOIN Paths AS B ON A.id = B.ancestor WHERE A.item = ? GROUP BY A.id"; $stmt = $dbh-&gt;prepare($sql); $stmt-&gt;execute(array($item)); $data = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); $adjacency_list = array(); $comments = array(); foreach($data as $row) { $comments[$row['id']] = $row; $descendants = explode(',', $row['descendants']); $adjacency_list[$row['id']] = $descendants; } echo '&lt;UL&gt;'; foreach($adjacency_list[$item] as $top_level_comment) { printTree($top_level_comment, $adjacency_list[$top_level_comment]); } echo '&lt;/UL&gt;'; function printTree($node, $descendants) { echo '&lt;LI&gt;'.$node; if(sizeof($descendants) &gt; 0) { echo '&lt;UL&gt;'; foreach($descendants as $descendant) { $d = array(); if(!empty($adjacency_list[$descendant])) $d = $adjacency_list[$descendant]; printTree($descendant, $adjacency_list[$descendant]); } echo '&lt;/UL&gt;'; } echo '&lt;/LI&gt;'; } </code></pre> <p>Nested Set Model, on the other hand, is not a good solution for commenting system because of the high inserts and updates. It is an efficient solution if you your data is rarely updated.</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. This table or related slice is empty.
    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