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

时间:2011-05-27 15:09:28

标签: php codeigniter multidimensional-array

嘿伙计们,我正在努力学习codeigniter,但我又一次感到困难,我寻求帮助(像往常一样:P)

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

好吧,我已经完成了标记部分[链接到答案,它帮助了我:Returning and using multidimensional array of records from database in CodeIgniter 2.0]但是comment part让我疯了。
那么开始这里是我的评论表

Comments
+---------------+-------------+
| Field         | Type        |
+---------------+-------------+
| commentId     | int(10)     |
| PostId        | int(10)     |
| author        | varchar(30) |
| email         | varchar(30) |
| url           | varchar(50) |
| date          | datetime    |
| comment       | text        |
| parent        | int(10)     | 
+---------------+-------------+

我正在使用父字段来跟踪嵌套子注释的父级。默认情况下,该值为0,表示它是父级。儿童评论将具有其父评论的荣誉

    public function getPost($postName = NULL , $year = NULL, $month = NULL ){
        if($postName != NULL && $year != NULL && $month != NULL){
            //single post
            $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 as &$p) {
                    $p->tags = $this->getAllTags($p->postId);   

                      /* getting the comments */
                    $com = $this->comment_model->getComments($p->postId); 

                    /*echo count($com).' is the total count'; output= 4 */
                    foreach ($com as &$c) {
                                      /* trying to filter the comment. but all I get is 1 comment as the output*/
                        if($c->parent==0){
                            $p->comments->parentComment = $c;
                        }elseif($c->commentId==$c->parent){
                            $p->comments->childComment = $c;
                        }
                    }
                }
                return $post;
            }else{
                return array();
            }
        }
    }

任何帮助肯定会受到赞赏。 如果您有任何其他技术/想法来显示多级评论,请告诉我。 :)

1 个答案:

答案 0 :(得分:1)

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

首先你需要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的行开始。然后它将为子项执行。如果孩子是孩子,它会添加它。最后,结果将是这样的:

$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

有关递归函数的更多信息,请查看answer

<强> USAGE

$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);

那就是那么简单......