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。ランダムなページとその下のすべてのレベルを選択したい場合、選択したページの 1 レベル下でのみ機能します。私はそれについて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