两个PHP递归导航问题


Two PHP Recursive Nav Questions

我几乎完成了这个导航。。。我有以下代码,它递归地生成我的导航,可以在这个链接上看到:http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/arraytest2.php

它还成功地将"active"类添加到标签=页面标题的任何链接中。这是我到目前为止的代码,请参阅底部的问题。我已经标记了javascript和jquery,但我更喜欢PHP解决方案,尽管我愿意接受建议。请注意,这个导航的全部目的是,我可以通过下面的数组编辑单个文件来添加/删除/编辑导航元素。

设置页面的$title:

<?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']);
?>

前臂:

<?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/

注意仅出现在具有子菜单的导航元素上的箭头。。。

这是通过在<A>标签内添加<span class='arrow'> +</span>来完成的。例如。。。(如果nav_label=title为TRUE,则class="可能使类处于活动状态(

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

问题是上面那一行的[这里是什么?]。此外,我意识到我还需要两个eifs。一个表示"如果active=true,如果sub_menu=true",那么这个。。。一个表示"如果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> 的链接

我已经警告过你了;-(。。。这是肮脏的代码。我会回答你的第二个问题,因为这是我的错:

/* $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;
}

很难读取代码这一事实正是改变数据结构的充分理由。

经过深思熟虑,我终于找到了解决箭头问题的方法。我只需要为active和arrow设置一个变量,并将它们放在Link中应该放的位置。然后将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
}
?>