2017-07-06 30 views
1

私は次のコードを2層ナビゲーションシステムを作成するために完璧に機能します。問題は、1つのセクションに3番目のレベルのページがあるという要件があることです。サブナビゲーションPHPの第3レベル

編集:

ページタイトル

  • サブページ1
  • :コードはそうのように上部の詳細な親アイテムと一緒に、このセクション内のページを一覧表示します2つのティアサイド・ナビゲーション・アイテムを生成
  • サブページ2
    • 別のサブページ1
      • さらにサブページ1
    • 別のサブページ1
  • サブページ3

任意の異常行動やエラーメッセージはありません、それがさらにサブページ1が表示されませんリスト内の項目。

function insection_make_ul($tree, $level=0) { 
$indent = str_repeat(" ", $level); 
$result = "\n".$indent."<ul>\n"; 
foreach($tree as $id => $item) { 
    $result .= $indent."<li><a href=\"".$item['permalink']."\" class=\"" 
     .($item['selected'] == true ? 'selected' : '') 
     .($level == 0 ? ' root' : '')."\" >" . $item['title']."</a>"; 
    if(count(@$item['items'])) { 
     $result .= insection_make_ul($item['items'], ($level+1)); 
     $result .= $indent."</li>\n"; 
    }else{ 
     $result .= "</li>\n"; 
    } 
} 
$result .= $indent."</ul>\n"; 
return $result; 

}

function insection($structure_id,$custom_selected=false){ 
$tree = insection_array($structure_id,$custom_selected); 
return insection_make_ul($tree); 

}

function insection_array($data,$custom_selected=false){ 
global $link; 
if(is_numeric($data)) 
    $data = fetch_row('SELECT * FROM content_structure WHERE id = '.$data); 
$selected_id = $data['id']; 

if($custom_selected) // dynamic item of 'real' parent 
    $selected_id .= '_'.$custom_selected; 

$insection = array(); 
if($data['parent_id'] > 0){ 
    if(HIDE_EMPTY_STRUCTURE){ 
     $sql = 'SELECT * FROM content_structure WHERE parent_id = '.$data['id'].' AND visible = 1 AND in_menu = 1 
                 AND (item_id > 0 OR redirect <> "")'; 
    }else{ 
     $sql = 'SELECT * FROM content_structure WHERE parent_id = '.$data['id'].' AND visible = 1 AND in_menu = 1'; 
    } 
    $result = mysqli_query($link, $sql); 
    if(mysqli_num_rows($result) > 0 || $data['children_php'] != ''){ 
     $parent_id = $data['id']; 
    }else{ 
     $parent_id = $data['parent_id']; 
    } 
}else{ 
    $parent_id = $data['id']; 
} 
while($parent_id > 0){ 
    $data = fetch_row('SELECT * FROM content_structure WHERE id = '.$parent_id); 
    $insection[$parent_id] = array('id' => $data['id'], 
     'title' => $data['menu_title'], 
     'permalink' => navlink($data), 
     'selected' => ($data['id'] == $selected_id ? true : false)); 

    if(HIDE_EMPTY_STRUCTURE){ 
     $sql = 'SELECT * FROM content_structure WHERE parent_id = '.$parent_id.' AND visible = 1 AND in_menu = 1 
                 AND (item_id > 0 OR redirect <> "") ORDER BY ' 
      .($data['sort_auto'] == 1 ? 'menu_title' : 'sort_order'); 
    }else{ 
     $sql = 'SELECT * FROM content_structure WHERE parent_id = '.$parent_id.' AND visible = 1 AND in_menu = 1 ORDER BY ' 
      .($data['sort_auto'] == 1 ? 'menu_title' : 'sort_order'); 
    } 
    $result = mysqli_query($link, $sql); 
    if(!$result){ die('error: '.mysqli_error($link)); } 
    while($row = mysqli_fetch_assoc($result)){ 
     $insection[$parent_id]['items'][$row['id']] = array('id' => $row['id'], 
      'title' => $row['menu_title'], 
      'permalink' => navlink($row), 
      'selected' => ($row['id'] == $selected_id ? true : false)); 
    } 

    // custom start 
    if($data['children_php'] != ''){ // custom sub items? 
     $sub_item_result = custom_navigation_array($data['children_php']); 
     foreach($sub_item_result as $sub_item){ 
      $id = $data['id'].'_'.$sub_item['id']; // realparent_customid 
      $insection[$parent_id]['items'][$id] = array('id' => $id, 
       'title' => $sub_item['menu_title'], 
       'permalink' => $sub_item['href'], 
       'selected' => ($id == $selected_id ? true : false)); 
     } 
    } 
    //custom end 
    $parent_id = $data['parent_id']; 
} 

$insection = array_reverse($insection,true); 
$temp = current($insection); 
$root_id = @$temp['id']; 
$insection_tree[$root_id] = current($insection); 

$found_selected = false; 
if(is_array(@$insection_tree[$root_id]['items'])){ 
    foreach($insection_tree[$root_id]['items'] as $id => $item){ 
     if(!empty($insection[$id])){ 
      if($insection_tree[$root_id]['items'][$id]['selected'] == true) 
       $found_selected = true; 
      $insection_tree[$root_id]['items'][$id] = $insection[$id]; 
     } 
    } 
} 
//if(!$found_selected){ 
// while(!$found_selected){ 
// 
// } 
//} 

return $insection_tree; 

}

私はこの作業を取得する可能性があります任意のポインタを配列を作成するためのコード。

おかげ

+1

'まあ、そのコードの多くは、誰もあなたの全体のコードを通過したいと思います。 http://stackoverflow.com/help/how-to-askに基づいて質問を編集して、うまくいかないことを教えてください。あなたの期待することと実際に何が起こったのかを教えてください。エラーメッセージ?不思議な行為?この情報がなければ、誰かがあなたを助けるとは思わない。 – Twinfriends

+0

こんにちは、返信いただきありがとうございます、私は何が働いていて、どの部分がうまくいかないかについての情報をいくつか追加して質問を修正しました。 – James

答えて

0

は個人的に私はあなたのコードで再見てお勧めします。同じことをするコードがたくさんあります。反復は悪いです。援助の手として、ここにあなたを行かせるものがあります。

このメニュー構造を例に挙げておきますが、実際にこの配列を作成するのはあなた自身であり、注目すべきは、配列を<ul><li>文字列に組み立てる関数です。

public function menuIterator($items) { 
    print "<ul>"; 
    foreach ($items as $item) { 
     print "<li>"; 
     print "<a>{$item['name']}</a>"; 
     if (isset($item['subs'])) { 
      $this->menuIterator($item['subs']); 
     } 
     print "</li>"; 
    } 
    print "</ul>"; 
    return; 
} 

そして結果は次のとおりです。

 $menuItems = array(
     array(// Top level items 
      "name" => "item1", 
      "subs" => array(// Second Level items 
       array(
        "name" => "1a", 
        "href" => "something" 
       ), 
       array(
        "name" => "1b", 
        "subs" => array(// Third Level Items 
         array("name" => "1ba", "href" => "something"), 
         array("name" => "1bb", array(
           array("name" => "1cca", "href" => "something"), 
          ) 
         ) 
        ) 
       ) 
      ) 
     ), 
     array(// Top level items 
      "name" => "item2", 
      "subs" => array(// Second Level items 
       array(
        "name" => "2a", 
        "href" => "something" 
       ), 
       array(
        "name" => "2b", 
        "subs" => array(// Third Level Items 
         array("name" => "2ba", "href" => "something"), 
         array("name" => "2bb", array(
           array("name" => "2cca", "href" => "something"), 
          ) 
         ) 
        ) 
       ) 
      ) 
     ) 
    ); 
     $this->menuIterator($menuItems); 
     die(); 

次のロジックが重要なビットです、それはあなたのメニューが深い、あなたがしたいと、それはまだ同じ結果を生成します任意のレベル可能性を意味します:どんなに私はそれがないしようと何

<ul><li><a>item1</a><ul><li><a>1a</a></li><li><a>1b</a><ul><li><a>1ba</a></li><li><a>1bb</a></li></ul></li></ul></li><li><a>item2</a><ul><li><a>2a</a></li><li><a>2b</a><ul><li><a>2ba</a></li><li><a>2bb</a></li></ul></li></ul></li></ul>

+0

提案してくれてありがとう、私は確かに時間が許せばコードを洗練することに目を向けます、これは私のものではないレガシーコードなので、あなたの提案も見ていきます。 – James

関連する問題