2017-07-06 70 views
1

我有下面的代碼,它完美地適用於生成雙層導航系統,問題是我有一個要求,其中一個部分有第三級的網頁下面。第三級的子導航php

編輯:代碼產生列出本節中的頁面在頂部像這樣詳述的父項沿着兩層側導航項目:

頁標題

  • 子1
  • 子頁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>字符串的函數。

 $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(); 

下面的邏輯是最重要的一點,這將意味着你的菜單可以是任何層次深,你想和它仍然會產生同樣的結果:

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

,其結果是:

<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