PHP导航,有更好的方法吗?

时间:2013-02-14 17:51:32

标签: php html html5 navigation nav

我是使用PHP的新手,在此导航之前我的PHP使用范围很简单。

我编写了一个navigation.php文件,该文件使用从variables.php中的数组中提取的变量动态生成导航链接

总体目标是仅通过编辑variables.php

来添加/减去链接,子动画,子子动画,子子动画。

我用来生成HTML结构的方法对我来说很笨拙,它运行得很好,但我觉得有一种更简单的方法可以做到这一点......

此外,我需要为任何存在子区域的LI提供额外的样式(箭头表示存在子区域)。我想动态生成它。

那说我的问题是:

  1. 有更简单的方法吗?
  2. 有没有办法根据是否存在子导航来动态生成我的?
  3. 每个页面都有以下代码,它会根据页面而变化。

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

    我的变量.php:

    <!-- NAVIGATION ARRAY -->
    
    <?php
    $nav_mini = array(
        'Home' => 'index.php',
        'About' => 'about.php',
        'Services' => 'services.php'
        );
    ?>
    
    <?php
    $nav_main = array(
        'Home' => 'index.php',
        'About' => 'about.php',
        'Services' => 'services.php',
        'Portfolio' => 'contact.php',
        'Clients' => 'about.php',
        'Gallery' => 'services.php',
        'News' => 'services.php',
        'Contact Us' => 'contact.php'
        );
    ?>
    
    <?php // SUB NAV ARRAYS
    
    $nav_sub_about = array(
        'Our Team' => 'about.php'
        );
    
    $nav_sub_michael = array(
        'Michael' => 'index.php',
        'Aaron' => 'about.php',
        'Kenny' => 'about.php',
        'David' => 'about.php'
        );
    
    $nav_sub_services = array(
        'Get Found' => 'about.php'
        );
    
    ?>
    

    导航PHP文件main-nav.php

        <?php 
    
            foreach ( $nav_main as $key => $value ) {
    
                echo "<li><a ";
                    echo "class='"; // STARTS CLASS=
                    if ( $title == $key) { 
                    echo "active "; }// ADDS ACTIVE CLASS TO LINK OF CURRENT PAGE
                echo "' "; // END QUOTES FOR CLASS="
    
    
                echo "href='$value'>$key";
    
                    if ( $key == "About") { // STARTS IF SEQUENCE FOR SUBNAV ARROWS
                        echo "<span class='arrow'> +</span>";}
                    elseif ( $key == "Services") {
                        echo "<span class='arrow'> +</span>";}
    
                echo "</a>\n";  // END ANCHOR TAG FOR MAIN NAV LINKS
    
    
    
    //----------------------------------------------------------------------------------------------------------//
    // PLEASE START SUB-NAVS HERE ------------------------------------------------------------------------------//
    // AT THIS POINT A NEW <LI> IS GENERATED FROM THE FIRST FOREACH, IF THE BELOW IS TRUE IT POPULATES A SUBNAV //      
    
    
            if ( $key == "About"){  // START SUBNAV FOR ABOUT
                echo "<ul class='main-sub-nav'>"; // THIS LINE WAS ADDED TO START THE <UL> FOR THE SUBNAV, IT ENDS ON LINE 14
    
                foreach ( $nav_sub_about as $key => $value ) {
                    echo "<li><a href='$value'>$key";
    
                        if ( $key == "Our Team") { // STARTS IF SEQUENCE FOR SUBNAV ARROWS
                        echo "<span class='arrow'> +</span>";}
    
                    echo "</a>\n"; // ENDS ANCHOR TAG FROM SUBNAV LI
    
                        // SUB-SUB-NAV STARTS HERE
                            if ( $key == 'Our Team') {
                            echo "<ul class='main-sub-sub-nav'>"; // THIS STARTS THE UL FOR SUB-SUB-NAV
    
                                foreach ($nav_sub_michael as $key => $value ) {
                            echo "<li><a href='$value'>$key</a></li>\n";
                        }
                        echo "</ul>"; // THIS ENDS THE SUB-SUB-NAV UL STARTED ON LINE 34
                    echo "</li>";}// THIS IS THE END IF <LI> FROM THE SECOND FOREACH ON LINE 35
    
            }
    
            echo "</ul>"; // THIS WAS ADDED TO END THE SUBNAV <UL> STARTED ON LINE 11
            echo "</li>";} // THIS IS THE END OF THE <LI> FROM THE FIRST FOREACH ECHO FROM LINE 8
    
    //----------------------------------------------------------------------------------------------------------//
    // AT THIS POINT A NEW <LI> IS GENERATED FROM THE FIRST FOREACH, IF THE BELOW IS TRUE IT POPULATES A SUBNAV //
    
                if ( $key == "Services"){
                    echo "<ul class='main-sub-nav'>"; // THIS LINE WAS ADDED TO START THE <UL> FOR THE SUBNAV, IT ENDS ON LINE 14
    
                    foreach ( $nav_sub_services as $key => $value ) {
                        echo "<li><a href='$value'>$key</a></li>\n";}
                    echo "</ul>"; // THIS WAS ADDED TO END THE SUBNAV <UL> STARTED ON LINE 11
                    echo "</li>";} // THIS IS THE END OF THE <LI> FROM THE FIRST FOREACH ECHO FROM LINE 8
    
    
    
            } // THIS BRACKET ENDS THE ENTIRE SET OF PHP.  ITS PAIR IS ON LINE 19
    
    ?>
    

    总体目标是仅通过编辑variables.php

    来添加/减去链接,子动画,子子动画,子子动画。

    以下是此导航结构工作的实例... http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/

2 个答案:

答案 0 :(得分:0)

你正在做的事情几乎就是现场,但是你正在自己编写自己的代码 而是允许它是通用的,所以编写一个函数,如果需要,可以调用自己来执行此操作:

$menu_items = array(
    'Home' => 'index.php'
    'About' => array('about.php', 
         array('Our Team'=>'ourteam.php'),
     etc
    );
?>
  • <ul>
  • 开始
  • 遍历给定数组
  • 添加<li>
  • 如果是is_string(),则只有一项附加<a href>
  • 如果is_array()有子项,
    • 添加<a href>
    • 将第二个元素发送到此函数并附加返回的html
  • 关闭</li>
  • 关闭</ul>
  • 返回

一个函数处理所有元素,无论它们是main还是sub,无论你有多少个子列表,都可以调用它一次完成列表。

现在,对于您的迷你菜单,只需通过键(Home,About,Services)从主数组中选择元素,然后使用一个函数创建html

答案 1 :(得分:0)

更清洁的解决方案是使用multidimensional arrays然后iterate through构建导航。如果数组的value是另一个数组,那么你知道它有一个子菜单。