2013-02-22 53 views
0

我非常接近这个导航做...我有以下的代码生成递归我的导航,可在此链接中可以看出:http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/arraytest2.php两个PHP递归导航问题

它也成功地增加了一类“活动“的任何链接的标签=页面标题。下面是我的代码,看到我的问题在底部。我已经标记了javascript和jquery,但我更喜欢PHP解决方案,尽管我愿意接受建议。请注意,此导航的全部目的是通过编辑单个文件通过下面的数组添加/删除/编辑导航元素。

设置页面的标题$:

<?php $title = 'Home'; ?> 

导航阵列:

<?php 

$nav_array = array ('Home' => 'index.php', 
        'About' => array ('about.php', array (
         'Michael' => array('michael.php', array (
          'Blog' => 'blog.php', 
          'Portfolio' => 'portfolio.php')), 
         'Aaron' => 'aaron.php' , 
         'Kenny' => 'kenny.php', 
         'David'=> 'david.php')), 

        'Services' => array ('services.php', array (
         'Get Noticed' => 'getnoticed.php', 
         'Hosting' => 'hosting.php')), 

        'Clients' => 'clients.php', 
        'Contact Us' => 'contact.php' 
    ); 

    $base = basename($_SERVER['PHP_SELF']); 
?> 

在foreach:

<?php 

echo "<ul>"; 
foreach ($nav_array as $nav_title => $nav_data) { 
    echo buildLinks($nav_title, $nav_data, $base, $title); 
} 
echo "</ul>"; 
?> 

的buildLinks功能:

<?php // Building the links 

function buildLinks ($label_name, $file_name, $active_class, $title) { 

    $theLink = ''; 

    $navigation_list = false; 
    if (is_array($file_name)) { 
    $navigation_list = $file_name[1]; 
    $file_name = $file_name[0]; 
    } 


    // build the links with active class 
    if ($label_name == $title) { 
    $theLink = "<li><a class=\"active\" href=\"$file_name\">$label_name</a></li>\n"; 
    } else { 
    $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n"; 
    } 

    // recursively loop back through build links function 
    if ($navigation_list) { 
    $theLink .= "<ul>"; 
    foreach ($navigation_list as $nav_title => $nav_data) { 
     $theLink .= buildLinks($nav_title, $nav_data, $active_class, $title); 
    } 
    $theLink .= "</ul>"; 
    } 

    return $theLink; // print the nav 
} 

所以,我有这样的导航视觉风格已经但是它是使用非递归讨厌的代码生成。尽管你可以在下面的链接中看到我将要引用的箭头,所以你明白了我正在努力实现的目标。 http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/

注意,只出现在有一个子菜单导航存在的元素箭......

这是通过添加<span class='arrow'> +</span><A>标签内部完成。因此,例如......(类=“”可能有类积极的,如果nav_label =标题是TRUE)

<a class="" href="about.php">About<span class='arrow'> +</span></a> 

所以我试图找出如何将此功能添加到正在递归产生什么.. 。我想我最大的问题是,我不太清楚如何确定是否存在子导航,如果这是真实的,然后添加<span>

我会假设我会扩大我的原在使用elseif的buildLinks函数中的IF语句。所以......

//建立与活动类

if ($label_name == $title) { 
    $theLink = "<li><a class=\"active\" href=\"$file_name\">$label_name</a></li>\n"; 
    } elseif ([what goes here?]) { 
    $theLink = "<li><a href=\"$file_name\">$label_name<span class='arrow'> +</span></a></li>\n"; 
    } else { 
    $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n"; 
    } 

的问题是,[什么放在这里?]在上述ELSEIF线的链接。另外,我意识到我需要两个elseifs。其中一个用于“if active = true且如果sub_menu = true”,那么这...以及一个用于“if sub_menu = ture”,那么这...

在那一点上面变成......

if ($label_name == $title) { 
    $theLink = "<li><a class=\"active\" href=\"$file_name\">$label_name</a></li>\n"; 
    } elseif ($label_name == $title && [what goes here?]) { 
    $theLink = "<li><a class=\"active\" href=\"$file_name\">$label_name<span class='arrow'> +</span></a></li>\n"; 
    } elseif ([what goes here?]) { 
    $theLink = "<li><a href=\"$file_name\">$label_name<span class='arrow'> +</span></a></li>\n"; 
    } else { 
    $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n"; 
    } 

我现在是在一个完全丧失,因为我有帮助生成这个递归导航,我理解它是如何工作居多,但我不完全明白发生了什么这里:

$theLink = ''; 

    $navigation_list = false; 
    if (is_array($file_name)) { 
    $navigation_list = $file_name[1]; 
    $file_name = $file_name[0]; 
    } 

而且我怀疑搞清楚这需要上面的东西^

所以我的问题再次...我需要把[我的代码]位做什么?

我的第二个问题,更是对我进一步的研究,这是我有什么办法可以养活上述阵列更动态,最好不MySQL的?

编辑︰我已经看了这一些,我仍然坚持这一点我没有让自己的代码。

$navigation_list = false; 
     if (is_array($file_name)) { 
     $navigation_list = $file_name[1]; 
     $file_name = $file_name[0]; 
     } 

我明白大部分是is_array操作符。通过上述,说$file_name[1];,是说$file_name[TRUE]?然后对于0 = FALSE以下的行?

我需要了解如何检测如果数组存在,以打印包括<span class="arrow"> +</span>

+0

你有没有考虑使用这个数据库?而不是从文件中读取,并且不得不编辑文件。 – 2013-02-22 17:25:11

+0

我曾一度考虑它,它的东西我愿意并希望在未来的探索。目前的简短答案是,我没有时间自我介绍数据库以实现这一目标。这也是我稍后可以更新的内容。目前,我有两个网站需要快速完成此工作,而我目前缺少的唯一功能是箭头。 – Michael 2013-02-22 17:33:45

回答

0

我已经警告过你一个链接;-) ...这是肮脏的代码的帮助。 我来回答你的第二个问题,因为这是我的错:

/* $file_name is a mixed parameter, denpending on when 
* buildLinks() is called, it will be $file_name (as in your first element 
* for 'Home' or an array with another navigation menu (as in your second 
* element for 'About' 
*/ 
function buildLinks ($label_name, $file_name, $active_class, $title) { 
    $theLink = ''; 
    // if this is false, there will be no recursive call 
    $navigation_list = false; 
    if (is_array($file_name)) { 
     /* $file_name is an array, as in 'About' */ 
     /* so we get the second element to build the navigation list */ 
     $navigation_list = $file_name[1]; 
     /* and then set $file_name to the real filename string, which 
     * in your data structure is the first element of the array. 
     * This is why I suggested reconsidering your data structure. 
     */ 
     $file_name = $file_name[0]; 
    } 

    // build the links with active class 
    if ($label_name == $title) { 
     $theLink = "<li><a class=\"active\" href=\"$file_name\">$label_name</a></li>\n"; 
    } else { 
     $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n"; 
    } 

    // since we assigned $navigation_list to an array, this is true 
    if ($navigation_list) { 
     $theLink .= "<ul>"; 
     foreach ($navigation_list as $nav_title => $nav_data) { 
      $theLink .= buildLinks($nav_title, $nav_data, $active_class, $title); 
     } 
     $theLink .= "</ul>"; 
    } 

    return $theLink; 
} 

的事实,这是很难读代码只是足够多的理由来改变数据结构。

+0

非常感谢!我主要想到了这一点,但并不完全确定顶端发生了什么。我很感激! – Michael 2013-02-23 01:14:32

0

左思右想,解决我的问题箭头打我。我只需要设置一个活跃的变量和箭头的变量,把它们内部theLink,他们应该是。然后移动if之外的语句。这不是完全动态的,因为我必须手动声明哪些标签有一个子菜单,以让箭头类打印,但在目前还没有有一个子菜单,目前很多环节,这不来烦我说现在很多。

下面是解决方案的代码:

<?php // Building the links recursively 

function buildLinks ($label_name, $file_name, $active_class, $title) { 

    $theLink = ''; 
    $navigation_list = false; 
    if (is_array($file_name)) { 
     $navigation_list = $file_name[1]; 
     $file_name = $file_name[0]; 
    } 

    if ($label_name == $title) { // print active if it is true 
     $active = 'active'; 
    } 

    if ($label_name == 'About') { // print arrow if it is true 
     $arrow = '<span class=\"arrow\"> +</span>'; 
    } elseif ($label_name == 'Michael') { 
     $arrow = '<span class=\"arrow\"> +</span>'; 
    } elseif ($label_name == 'Services') { 
     $arrow = '<span class=\"arrow\"> +</span>'; 
    } 


    $theLink = "<li><a class=\"$active\" href=\"$file_name\">$label_name $arrow</a></li>\n"; 


    // recursively loop back through build links function 
    if ($navigation_list) { 
    $theLink .= "<ul class=\"sub-nav\">"; 
    foreach ($navigation_list as $nav_title => $nav_data) { 
     $theLink .= buildLinks($nav_title, $nav_data, $active_class, $title); 
    } 
    $theLink .= "</ul>"; 
    } 

    return $theLink; // print the nav 
} 

?>