我对使用 PHP 相当陌生,在此导航之前,我的 PHP 使用范围很简单。
我编写了一个 navigation.php 文件,该文件使用从 variables.php 中的数组中提取的变量动态生成导航链接
总体目标是仅通过编辑 variables.php 来添加/减去链接、子导航、子子导航、子子子导航
我用来生成 HTML 结构的方法对我来说感觉很笨拙,它工作得很好,但我觉得有一种更简单的方法可以做到这一点......
此外,我需要为任何存在 subnav 的 LI 附加样式(箭头表示存在 subnav)。我想动态生成它。
那就是说我的问题是:
- 有没有更简单的方法来做到这一点?
- 有没有办法根据是否存在子导航来动态生成我的?
每个页面都有以下代码,它会根据页面而变化。
<?php $title = 'Home'; ?>
我的 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'
);
?>
导航 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/