简体   繁体   English

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

[英]PHP Navigation, is there a better way to do this?

I am fairly new to using PHP, before this navigation the extent of my PHP use was simple includes. 我对使用PHP很陌生,在此导航之前,我对PHP的使用范围很简单。

I have written a navigation.php file that generates links for a navigation dynamically using variables pulled from an array inside variables.php 我已经编写了一个navigation.php文件,该文件使用从variables.php内部数组中提取的变量来动态生成导航链接。

The overall goal is to add/subtract links, subnavs, sub-subnavs, sub-sub-subnavs only by editing variables.php 总体目标是仅通过编辑variables.php来添加/减去链接,子导航,子子导航,子子子导航。

The method I am using to generate the HTML structure feels clunky to me, it works just fine but I feel like there is a simpler way of doing this... 我用来生成HTML结构的方法对我来说很笨拙,它可以正常工作,但是我觉得有一种更简单的方法可以...

Also, I require additional styling for any LI that has a subnav present (an arrow to indicate a subnav is present). 此外,对于任何存在subnav的LI(对于存在subnav的箭头,我都需要额外的样式)。 I would like to generate that dynamically. 我想动态生成。

That said my questions are: 那就是我的问题是:

  1. Is there a simpler way of doing this? 有没有更简单的方法可以做到这一点?
  2. Is there a way to generate my 's dynamically based on whether there is a sub-nav present? 有没有一种方法可以根据是否存在子导航动态生成my?

Each page has the following code, it changes depending on the page. 每个页面都有以下代码,它随页面而变化。

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

My variables.php : 我的variables.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'
    );

?>

Navigation PHP File main-nav.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

?>

The overall goal is to add/subtract links, subnavs, sub-subnavs, sub-sub-subnavs only by editing variables.php 总体目标是仅通过编辑variables.php来添加/减去链接,子导航,子子导航,子子子导航。

Here is a live example of this nav structure working... http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/ 这是此导航结构工作的实时示例... http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/

What you're doing is almost spot on but you're making yourself write around your own code Instead allow it to be generic, so write a function, that can call itself if it needs to, to do this: 您正在做的事情几乎就位了,但是您正在使自己编写自己的代码,而不是允许它是通用的,因此编写一个函数,可以在需要时调用自身来执行此操作:

$menu_items = array(
    'Home' => 'index.php'
    'About' => array('about.php', 
         array('Our Team'=>'ourteam.php'),
     etc
    );
?>
  • Start with <ul> <ul>开头
  • Loop through the given array 遍历给定数组
  • Add <li> 添加<li>
  • If is_string() it's one item append <a href> 如果is_string()是一项,请附加<a href>
  • If is_array() there are sub items, 如果is_array()有子项,
    • add the <a href> 添加<a href>
    • send the second element to this function and append the html it returns 将第二个元素发送到此函数,并附加返回的html
  • close </li> 关闭</li>
  • close </ul> 关闭</ul>
  • return it 把它返还

One function handles all elements whether they are main or sub and calling it once completes your list no matter how many sub lists you have. 一个函数处理所有元素,无论它们是主要元素还是子元素,并且无论您有多少个子列表,调用它都会完成列表。

Now, for your mini menu just pick out the elements from the main array by their key (Home, About, Services) before creating the html with the one function 现在,对于您的迷你菜单,在使用一个功能创建html之前,只需通过键(首页,关于,服务)从主数组中选取元素即可

A much cleaner solution is to build the navigation using multidimensional arrays and then iterate through them. 一个更干净的解决方案是使用多维数组构建导航,然后遍历它们。 If the value of an array is another array, then you know it has a sub menu. 如果一个数组的value是另一个数组,则您知道它具有一个子菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM