1

I have this organized array which works nice with below recursion function.

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
            )

    )

)

However, it works nice and recursively if I start to query from where id_parent = 0. When I wanted to select a random page and all its below levels, it works only one level down of selected page. I suppose that it is about index but unable to solve it.

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;
}

Any light to move on?

EDITED AFTER COMMENTS

Well now I am using the offered way by @Baba and it works unless I can't control starting and ending depth.

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

How to control start and stop depth?

quantme
  • 3,609
  • 4
  • 34
  • 49
  • 1
    The most useful thing you can do before writing another single line of code is to [read this](http://php.net/manual/en/language.references.php). – DaveRandom Oct 08 '12 at 15:07
  • 1
    How are you calling _populate_list() ? because upon it's default parameters is doing it right with the default 1 max_depth.. --> $level='0',$max_depth='1' – Nelson Oct 08 '12 at 15:08
  • 2
    @AnzaVR how is what you want to achieve different from http://stackoverflow.com/a/12772019/1226894 – Baba Oct 08 '12 at 15:09
  • @Baba, not much difference, thank you. I put a comment there about tracking depth. –  Oct 08 '12 at 17:31
  • @Nelson, it comes from another function a for example '3' or what I input. –  Oct 08 '12 at 17:53

0 Answers0