Pregunta

I am working now with comments system and i reached some problems. I am making a Hierarchical comment system like this:

comment1

--comment2

----comment4

comment3

--comment5

I use to make this query:

$sql = "SELECT id, parent_id, name, comment,
    DATE_FORMAT(add_date, '%d %M %Y %H:%i') as add_date
    FROM comments  ORDER BY id DESC";

And then use this function:

function mapTree( $dataset )
{
    $tree = array();

    foreach ($dataset as $id=>&$node)
    {
        if (!$node['parent_id'])
        {
            $tree[$id] = &$node;
        }
        else
       {
            $dataset[$node['parent_id']]['childs'][$id] = &$node;
        }
    }

    return $tree;
}

but if I want to limit number of comments to 3 or 5 or eth. I don't have a tree any more:

example:

SELECT id, parent_id, name, comment,
        DATE_FORMAT(add_date, '%d %M %Y %H:%i') as add_date
        FROM comments LIMIT 2

comment1

--comment2

I am loosing comments that have parent id.

Could U help me to solve it or show some other way to build comment tree script?

¿Fue útil?

Solución

I think for your code to work, the dataset ids need to match the node ids. Other than that it seems to work.

You my be better ordering by date, or parent id. (Auto incrementating ids might not actually be in date order.)

If your SQL has an ORDER by add_date, and you limit the query, a later reply to an earlier comment might not be available to place in an earlier thread. That I assume is your issue.

I would ask why you want the LIMIT in there in the first place? I assume it is for comment paging. That could be indicitive of allowing too many comments (thinking of web page comments here). You could have a maximum amount of allowed submissions. Perhaps a better way to make the comment tree more readable is by having collapsible threads rather than using paging.

You'll probably want to add a thread/tree/topic id to your table, and add a corresponding WHERE clause to your SQL.

I had to add an index for your dataset to get your code working:

<?php

// Similar format to rowsets from a database...
// node is an array containing: id, parent id, comment
$commentRows = array(
    array('a', '0', 'Hi, great stuff.'),
    array('b', 'a', 'No, not really!'),
    array('c', '0', 'Trees are good!'),
    array('d', 'b', 'That is like; your opinion man.'),
    array('e', 'c', 'Teas are good')
);

function rowsToTree($rows)
{
    $tree = array();
    $index = array(); // node id => row id

    // Build an index
    foreach($rows as $id=>&$node) {
        $index[$node[0]] = $id;
    }

    foreach ($rows as $id=>&$node)
    {
        if (!$node[1]) // No parent
        {
            $tree[] = &$node;
        }
        else // Has parent, add this as a child to parent
       {
            $parentKey = $index[$node[1]];
            $rows[$parentKey]['kids'][$id] = &$node;
        }
    }

    return $tree;
}

$data = $commentRows;

// Uncomment to emulate an SQL limit
// $chunks = array_chunk($commentData, 2);
// $data = $chunks[0];

var_dump(rowsToTree($data));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top