1

我有这个有组织的数组,它与下面的递归函数配合得很好。

Array
(
[6] => Array
    (
        [0] => Array
            (
                [id_parent] => 6
                [level] => 2
                [lang] => en
                [id_page] => 15
                [title] => test 3
                [nav_title] => 
                [url] => test-3
            )

    )

[13] => Array
    (
        [0] => Array
            (
                [id_parent] => 13
                [level] => 3
                [lang] => en
                [id_page] => 14
                [title] => test 2
                [nav_title] => 
                [url] => test-2
            )

    )

[11] => Array
    (
        [0] => Array
            (
                [id_parent] => 11
                [level] => 2
                [lang] => en
                [id_page] => 13
                [title] => Test
                [nav_title] => 
                [url] => test
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 11
                [title] => Spare Parts
                [nav_title] => 
                [url] => spare-parts
            )

        [1] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 10
                [title] => İndustrial İroning Table
                [nav_title] => 
                [url] => industrial-ironing-table
            )

        [2] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 9
                [title] => İndustrial İroning Boiler
                [nav_title] => 
                [url] => industrial-ironing-boiler
            )

        [3] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 8
                [title] => Steam Boılers
                [nav_title] => 
                [url] => steam-boilers
            )

        [4] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 7
                [title] => Home Type Steam Boilers
                [nav_title] => 
                [url] => home-type-steam-boilers
            )

        [5] => Array
            (
                [id_parent] => 3
                [level] => 1
                [lang] => en
                [id_page] => 6
                [title] => Used İrons
                [nav_title] => 
                [url] => used-irons
            )

    )

[0] => Array
    (
        [0] => Array
            (
                [id_parent] => 0
                [level] => 0
                [lang] => en
                [id_page] => 1
                [title] => Home
                [nav_title] => 
                [url] => home
            )

        [1] => Array
            (
                [id_parent] => 0
                [level] => 0
                [lang] => en
                [id_page] => 2
                [title] => About Us
                [nav_title] => 
                [url] => about-us
            )

        [2] => Array
            (
                [id_parent] => 0
                [level] => 0
                [lang] => en
                [id_page] => 3
                [title] => Products
                [nav_title] => 
                [url] => products
            )

        [3] => Array
            (
                [id_parent] => 0
                [level] => 0
                [lang] => en
                [id_page] => 5
                [title] => Quality
                [nav_title] => 
                [url] => quality
            )

        [4] => Array
            (
                [id_parent] => 0
                [level] => 0
                [lang] => en
                [id_page] => 4
                [title] => Contact Us
                [nav_title] => 
                [url] => contact-us
            )

    )

)

但是,如果我从 where 开始查询,它会很好地递归id_parent = 0。当我想选择一个随机页面及其所有以下级别时,它仅适用于所选页面的下一级。我想它是关于index但无法解决它。

function _populate_list($items, $parent = null, $level = '0', $max_depth = '1', $ul_class = '', $active_class = '', $active_segment = 2, $use_span = false, &$data = '')
{
  $this->load->config('route_page', TRUE);
  $route_page = $this->config->item('route_page', 'route_page');

  $index = ($parent == null) ? '0' : $parent;
  $space = str_repeat("\t", $level);

  if (isset($items[$index]))
  {
    $data .= "\n" . $space . '<ul';
    $data .= $parent == null ? ' id="navigation"' : '';
    if ($ul_class != '')
      $data .= ' class="' . $ul_class . '"';
    $data .= '>' . "\n";

    foreach ($items[$index] as $child)
    {
      $data .= $space . '<li';

      if ($active_class)
      {
        $tmp = $route_page[$child['lang'] . '/' . $child['id_page']];
        $tmp = explode('/', $tmp);
        if ($this->uri->segment($active_segment) == $tmp[$active_segment - 1])
        {
          $data .= ' class="' . $active_class . '"';
        }
      }

      $data .= '>';

      $data .= /*id="' . $child['lang']. '-' .$child['id_page'] . '">'.*/ "\n" . $space . '<a href="' . base_url() . $route_page[$child['lang'] . '/' . $child['id_page']] . '">';

      if ($use_span)
        $data .= '<span>';

      if ($child['nav_title'] != '')
        $data .= $child['nav_title'];
      else
        $data .= $child['title'];

      $data .= ($use_span) ? '</span></a>' . "\n" : '</a>' . "\n";

      if (($level + 1) < $max_depth)
      {
        $this->_populate_list($items, $child['id_page'], $level + 1, $max_depth, '', '', $active_segment + 1, $use_span, $data);
      }

      $data .= $space . '</li>' . "\n";
    }

    $data .= $space . '</ul>' . "\n";
  }
  return $data;
}

有什么灯可以继续吗?

评论后编辑

好吧,现在我正在使用@Baba 提供的方式,除非我无法控制开始和结束深度,否则它可以工作。

function build_navigation(array $array, $no = 0, $ul_class = '', $active_class = 'active', $active_segment = '2', $use_span = true)
{
  $this->load->config('route_page', TRUE);
  $route_page = $this->config->item('route_page', 'route_page');

  $child = $this->build_navigation_children_setter($array, $no);
  if (empty($child)) return "";
  $data = '<ul';
  if('' != $ul_class) $data .= ' class="' . $ul_class . '"';
  $data .= '>'."\n";

  foreach ( $child as $value ) 
  {

    //$content .= sprintf("\t<li>%s</li>\n", $value['title']);
    $data .= "\t" . '<li';
    if($active_class)
    {
      $tmp = $route_page[$value['lang'] . '/' . $value['id_page']];
      $tmp = explode('/', $tmp);
      if($this->uri->segment($active_segment) == $tmp[$active_segment-1])
      {
         $data .=  ' class="' . $active_class . '"';
      }
    }
    $data .= '>';

    $data .= '<a href="'. base_url() . $route_page[$value['lang'] . '/' . $value['id_page']] . '">';

    if ($use_span) $data .= '<span>';

    if('' != $value['nav_title']) $data .= $value['nav_title'];
    else $data .= $value['title'];

    $data .= ($use_span) ? '</span></a>' . "\n" : '</a>';
    $data .= $this->build_navigation($array, $value['id_page'], $level+1, $max_depth);
    $data .= "\t" . '</li>' . "\n";
  }
  $data .= "</ul>\n";
  return $data;
}

function build_navigation_children_setter($array, $id_page)
{
  return array_filter(
    $array, 
    function ($var) use($id_page)
    {
      return $var['id_parent'] == $id_page;
    }
  );
}

如何控制开始和停止深度?

4

0 回答 0