2013-03-01 40 views
3

我正在检索自定义CMS的数据库中的所有页面。这些页面是嵌套的并且具有parent_id以查找父页面的子项。Array中的嵌套页面 - PHP

代码检索代码:

public function get_nested() 
{ 
    $pages = $this->db->get('pages')->result_array(); 

    $array = array(); 
    foreach ($pages as $page) 
    { 
     if (!$page['parent_id']) 
     { 
      $array[$page['id']] = $page; 
     } 
     else 
     { 
      $array[$page['parent_id']]['children'][] = $page; 
     } 
    } 

    return $array; 
} 

出于某种原因,其他条件不使用$阵列[$页[ 'PARENT_ID']工作。

的$页面转储给我

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [title] => Homepage 
      [slug] =>/
      [order] => 1 
      [body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do  eiusmod 
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
consequat. 
      [parent_id] => 0 
     ) 

    [1] => Array 
     (
      [id] => 3 
      [title] => Contact 
      [slug] => contact 
      [order] => 0 
      [body] => <p>This is my contact page.</p> 
      [parent_id] => 4 
     ) 

    [2] => Array 
     (
      [id] => 4 
      [title] => About 
      [slug] => about 
      [order] => 0 
      [body] => <p>All about our company.</p> 
      [parent_id] => 0 
     ) 

) 

所以我期待的接触出现在条件的其他部分。我试过在条件中只回显'parent'和'child',它有效,但是在写$ array [$ page ['parent_id']]时什么都没有出现,甚至没有一个块说它是空的。 目前我得到的回应是:

Array 
(
    [1] => Array 
     (
      [id] => 1 
      [title] => Homepage 
      [slug] =>/
      [order] => 1 
      [body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do  eiusmod 
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
consequat. 
      [parent_id] => 0 
     ) 

    [4] => Array 
     (
      [id] => 4 
      [title] => About 
      [slug] => about 
      [order] => 0 
      [body] => <p>All about our company.</p> 
      [parent_id] => 0 
     ) 

) 

有谁知道为什么$阵列[$页[“PARENT_ID”]没有做任何事情?我认为它会给我一个[4]内的儿童数组,但没有任何内容。我检查了数据库中parent_id的类型,它是INT(11),所以不应该造成问题。

在此先感谢。

+0

在MySQL内部分层的数据是很难实现的根本。你涉及的表格的模式是什么?页面是否具有确定的深度,或者它们是否能够嵌套* ad infinitum *? – BenM 2013-03-01 18:06:14

+0

嗨BenM,感谢您的快速回复。我在这里添加了数据库的视觉效果:[链接](http://i1097.photobucket.com/albums/g351/europcsolutions/sql_zps2d3c3b19.jpg) 理论上,在MySQL中,页面可以具有无限深度,但这是在PHP中处理,当添加一个新页面时,你只能选择深度为0的父母。所以最大深度真的是1. – xonorageous 2013-03-01 18:14:53

回答

3

它会设置您的阵列中的孩子..但是这个代码有一个错误。当孩子出现它的父之前,它首先是要设定孩子,但是当父母在以下患儿病情出现后要覆盖的$阵列..

if (!$page['parent_id']) 
{ 
    $array[$page['id']] = $page; 
} 

所以不会保持已设置的儿童。

你必须做这样的事情:

if (!$page['parent_id']) 
{ 
    if(!isset($array[$page['id']])) 
     $array[$page['id']] = $page; 
    else 
     $array[$page['id']] = array_merge($page,$array[$page['id']]); 
} 

它将保持孩子呢。让我知道这是否有效。


更新:我只是试图在上述情况下运行您的程序......它像这样给输出:

array 
    1 => 
    array 
     'id' => int 1 
     'title' => string 'homepage' (length=8) 
     'parent_id' => int 0 
    4 => 
    array 
     'id' => int 4 
     'title' => string 'about' (length=5) 
     'parent_id' => int 0 
     'children' => 
     array 
      0 => 
      array 
       'id' => int 3 
       'title' => string 'contact' (length=7) 
       'parent_id' => int 4 
+0

不错的catch @V_K – 2013-03-01 18:38:00

+0

谢谢@V_K。从未想过这个问题,谢谢指出。 – xonorageous 2013-03-01 19:22:23