[mysql]PHP-与父创建注释列表

发布时间: 2017/4/9 21:36:26
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有这样的表结构︰

id_comment
body
date_created
date_updated    
user_id
parent_id

和现在这个函数︰

public function comments_list($comments)
    {
        $comments_list = "";
        foreach ($comments as $c) :
            if($c['parent_id'] == 0) :
                $comments_list .= '<div class="comment level-0">';
                $comments_list .= $c['body'];
                $comments_list .= '</div>';
            endif;
        endforeach;


    }

$comments是该数组的所有评论 (二维数组)。$c ['parent_id'] = = 0是父注释。有的子元素评论parent_id设置为父注释的 id。 如何可以创建与父/子关系的评论列表?

评论结构︰

array(5) {
  [0]=>
  array(9) {
    ["id_comment"]=>
    string(1) "1"
    ["body"]=>
    string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
    ["date_created"]=>
    string(19) "2014-07-01 00:00:00"
    ["date_updated"]=>
    string(19) "2014-07-02 02:34:51"
    ["post_id"]=>
    string(1) "4"
    ["user_id"]=>
    string(1) "1"
    ["parent_id"]=>
    string(1) "0"
    ["good_vote"]=>
    string(1) "0"
    ["bad_vote"]=>
    string(1) "0"
  }
  [1]=>
  array(9) {
    ["id_comment"]=>
    string(1) "2"
    ["body"]=>
    string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
    ["date_created"]=>
    string(19) "0000-00-00 00:00:00"
    ["date_updated"]=>
    string(19) "2014-07-02 02:34:54"
    ["post_id"]=>
    string(1) "4"
    ["user_id"]=>
    string(1) "1"
    ["parent_id"]=>
    string(1) "2"
    ["good_vote"]=>
    string(1) "0"
    ["bad_vote"]=>
    string(1) "0"
  }
  [2]=>
  array(9) {
    ["id_comment"]=>
    string(1) "3"
    ["body"]=>
    string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
    ["date_created"]=>
    string(19) "0000-00-00 00:00:00"
    ["date_updated"]=>
    string(19) "2014-07-02 02:34:56"
    ["post_id"]=>
    string(1) "4"
    ["user_id"]=>
    string(1) "1"
    ["parent_id"]=>
    string(1) "3"
    ["good_vote"]=>
    string(1) "0"
    ["bad_vote"]=>
    string(1) "0"
  }
  [3]=>
  array(9) {
    ["id_comment"]=>
    string(1) "4"
    ["body"]=>
    string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
    ["date_created"]=>
    string(19) "0000-00-00 00:00:00"
    ["date_updated"]=>
    string(19) "2014-07-02 02:34:59"
    ["post_id"]=>
    string(1) "4"
    ["user_id"]=>
    string(1) "0"
    ["parent_id"]=>
    string(1) "1"
    ["good_vote"]=>
    string(1) "1"
    ["bad_vote"]=>
    string(1) "0"
  }
  [4]=>
  array(9) {
    ["id_comment"]=>
    string(1) "5"
    ["body"]=>
    string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
    ["date_created"]=>
    string(19) "0000-00-00 00:00:00"
    ["date_updated"]=>
    string(19) "2014-07-02 02:35:02"
    ["post_id"]=>
    string(1) "4"
    ["user_id"]=>
    string(1) "1"
    ["parent_id"]=>
    string(1) "0"
    ["good_vote"]=>
    string(1) "0"
    ["bad_vote"]=>
    string(1) "0"
  }
}

解决方法 1:

这是你需要的基本查询︰

SELECT * FROM `comments` WHERE `parent_id` = 0 ORDER BY `date_created` DESC

假设你正在寻找的评论父具有 ID 0

起初,只抓"根"-评论︰

SELECT * FROM `comments` WHERE `parent_id` IS NULL ORDER BY `date_created` DESC

假设"根"评论已一个 null 值,因为它是父 id (你需要东西那里区别于子的评论)。

编辑

好吧我明白你的问题是什么;)如果不是,请说明 !

你应该采取两个步骤︰ 1) 抓住根评论和遍历它们,将其插入 DOM 的路上。2) 在每次迭代,抓住 '分评论' 和遍历它们,它们在 DOM 中插入

而如果你想多层次评论你应使用递归。

示例︰

$query = 'SELECT * FROM `comments` WHERE `parent_id` IS NULL ORDER BY `date_created` DESC';
$comments = fetch_results_with_your_own_database_function($query);

foreach($comments as $comment) {
    echo '<div class="comment">';
    echo $comment->body;
    echo subCommentList($comment->id);
    echo '</div>';
}

function subCommentList($parent_id) {
    $query = 'SELECT * FROM `comments` WHERE `parent_id` = '. $parent_id .' ORDER BY `date_created` DESC';
    $comments = fetch_results_with_your_own_database_function($query);
    foreach($comments as $comment) {
        echo '<div class="subcomment">';
        echo $comment->body;
        echo subCommentList($comment->id);
        echo '</div>';
    }
}

不请功能自称与当前的 id 作为它的下一次调用的父。每次它会深入你列堆栈中 (原谅我不好使用的术语)。

警告包括作为 ID-是在查询是非常不安全。我相信一个好的数据库库使用准备语句等。如果你不使用它 (尚),请谷歌

官方微信
官方QQ群
31647020