排序并存储方法返回的数组作为父数据和子数据,用于在codeigner 2.0中显示嵌套/多级注释


Sorting and storing array returned by a method as parent and child data for displaying nested/multi level comments in codeigniter 2.0

嘿,伙计们,我正在努力学习代码点火器,但我又一次被卡住了,我寻求帮助(像往常一样:p)

What I need to do?
->我需要从数据库中获取与文章相关的数据,以及其他东西,如文章的标签和所有comments。我正在考虑为文章保留单层嵌套评论。

好吧,我已经完成了标签部分[链接到帮助我完成同样任务的答案:在CodeIgniter 2.0中从数据库返回并使用多维记录数组],但comment part让我抓狂
好的,开始这里是我的意见表

评论+---------------+-------------+|字段|类型|+---------------+-------------+|commentId|int(10)||PostId|int(10)||作者|varchar(30)||电子邮件|varchar(30)||url|varchar(50)||日期|日期时间||注释|文本||parent|int(10)|+---------------+-------------+

我使用父字段来跟踪嵌套子注释的父字段。默认情况下,该值为0,这意味着它是父级。子评论将具有其父评论的评论

公共函数getPost($postName=NULL,$year=NULL,$month=NULL){if($postName!=NULL&&$year!=NULL&&$month!=NULL){//单桩$this->load->model('comment_model');$this->db->where('postName',$postName);$this->db->where('year(date)',$year);$this->db->where('month(date)',$month);$q=$this->db->get('mstack_Post');if($q->num_rows()>0){$post=$q->result();foreach($post为&$p){$p->tags=$this->getAllTags($p->postId);/*获取评论*/$com=$this->comment_model->getComments($p->postId);/*回声计数($com)。'是总计数';输出=4*/foreach($com为&$c){/*正在尝试筛选评论。但我得到的只是1条评论作为输出*/if($c->parent=0){$p->comments->parentComment=$c;}elseif($c->commentId==$c->父级){$p->comments->childComment=$c;}}}return$post;}其他{return array();}}}

任何帮助都将不胜感激。如果你有任何其他显示多级评论的技巧/想法,请告诉我。:)

以下是可能有用的解决方案:

首先,您需要2个辅助递归函数:

// 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->parent == $parent)
        {
            // add to the result
            $result[$item->commentId] = array(
                'author' => $item->author,
                // ... other definitions
                'child' => buildComments($list, $item->commentId) //execute this function for child.
            );
        }
    }
    return $result;
}
function printComments($arg, $depth = 1)
{
    foreach ($arg as $item)
    {
        // Printing comment...
        echo str_repeat('&nbsp; ', $depth) . $item['author'] . "<br />'r'n";
        // extra echoes...
        // if it has a child comment...
        if (count($item['child'] > 0))
        {
            printComments($item['child'], $depth + 1);
        }
    }
}

一点解释:

buildComments()函数将从父行为0的行开始。然后它会为孩子执行自己。如果child是child,它会添加它。最后,结果会是这样的:

$result = array(
    1 => array(
        'author' => 'John',
        'child' => array(
            8 => array(
                'author' => 'Jane',
                'child' => array(
                    3 => array(
                        'author' => 'Jamie',
                        'child => array()
                    )
                )
            ),
            6 => array(
                'author' => 'Jackie',
                'child => array()
            ),
            9 => array(
                'author' => 'Harry',
                'child => array()
            )
        )
    ),
    4 => array(
        'author' => 'Jack',
        'child' => array()
    ),
    10 => array(
        'author' => 'Clark',
        'child' => array(
            11 => array(
                'author => 'Lois',
                'child' => array()
            )
        )
    ),
    12 => array(
        'author' => 'Luthor',
        'child' => array()
    )
);

printComments()函数中,我们将递归打印结果。对于每个子项,函数都会重复自身。你会得到这样的结果:

  John
    Jane
      Jamie
    Jackie
    Harry
  Jack
  Clark
    Lois
  Luthor

有关递归函数的更多信息,请查看此答案

用法

$this->db->where('postName',$postName);
$this->db->where('year(date)',$year);
$this->db->where('month(date)',$month);
$this->db->order_by('parent', 'asc');
$query = $this->db->get('comments');
$comments = buildComments($query->result());
printComments($comments);

就这么简单。。。