System komentowania hierarchii php

Chcę zrobić system komentowania discus/reddit/ like, mam pole" id_answer "(ustawione na 0 przez defaut) w mojej bazie komentarzy i gdy użytkownik odpowiada na inny komentarz to pole jest " id " komentarza rodzica.

Mam komentarze wątku w tablicy ale nie wiem jak filtrować każdą pętlę aby uzyskać coś takiego:

Comment lvl 1 ($array[id_answerer] is 0)

------- komentarz lvl 2 ($array [id_answerer] jest id_of_level_one_comment)

--------------- skomentuj lvl 3 ...

Author: hakre, 2011-10-11

1 answers

Użyj trzech pól w bazie danych;

  • "id", unikalne dla każdego komentarza
  • "parent_id", który jest albo 0 (komentarz najwyższego poziomu), albo " id " komentarza rodzica
  • "thread_id" , czyli id tego co komentujesz

Załóżmy, że chcesz pokazać drzewo komentarzy do artykułu o id " 123 "

Wybierając z mysql, Wybierz wszystko o tym thread_id:

SELECT id, parent FROM comments WHERE thread_id = 123

Następnie powinieneś wstępnie parsować tablicę, aby dać komentarze dziecka do rodziców i użyj rekursywnego wyświetlacza, aby wyświetlić każdy komentarz i jego listę Potomków.

Dla exemple:

// getting the comments from mysql, I'm obviously not bothering
//   to check the return value, but in your code you should do it
$result = mysqli_query("SELECT id, parent FROM comments WHERE thread_id = 123");

$comments = array();
while ($row = mysqli_fetch_array($result)) {
  $row['childs'] = array();
  $comments[$row['id']] = $row;
}

// This is the array you get after your mysql query
// Order is non important, I put the parents first here simply to make it clearer.
/*
$comments = array(
    // some top level (parent == 0)
    1 => array('id' => 1, 'parent' => 0, 'childs' => array()),
    5 => array('id' => 5, 'parent' => 0, 'childs' => array()),
    2 => array('id' => 2, 'parent' => 0, 'childs' => array()),
    10 => array('id' => 10, 'parent' => 0, 'childs' => array()),
    // and some childs
    3 => array('id' => 3, 'parent' => 1, 'childs' => array()),
    6 => array('id' => 6, 'parent' => 2, 'childs' => array()),
    4 => array('id' => 4, 'parent' => 2, 'childs' => array()),
    7 => array('id' => 7, 'parent' => 3, 'childs' => array()),
    8 => array('id' => 8, 'parent' => 7, 'childs' => array()),
    9 => array('id' => 9, 'parent' => 6, 'childs' => array()),
);
*/

// now loop your comments list, and everytime you find a child, push it 
//   into its parent
foreach ($comments as $k => &$v) {
  if ($v['parent'] != 0) {
    $comments[$v['parent']]['childs'][] =& $v;
  }
}
unset($v);

// delete the childs comments from the top level
foreach ($comments as $k => $v) {
  if ($v['parent'] != 0) {
    unset($comments[$k]);
  }
}

// now we display the comments list, this is a basic recursive function
function display_comments(array $comments, $level = 0) {
  foreach ($comments as $info) {
    echo str_repeat('-', $level + 1).' comment '.$info['id']."\n";
    if (!empty($info['childs'])) {
      display_comments($info['childs'], $level + 1);
    }
  }
}

display_comments($comments);

Daje to następujący wynik:

- comment 1
-- comment 3
--- comment 7
---- comment 8
- comment 5
- comment 2
-- comment 6
--- comment 9
-- comment 4
- comment 10

Zostawiam zamówienie przez I takie do ciebie, ponieważ nie powinno stanowić żadnego problemu.

 21
Author: Lepidosteus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-23 23:04:05