【问题标题】:Setting "active" drop down menu include "nav.php";设置“活动”下拉菜单包括“nav.php”;
【发布时间】:2014-09-29 00:38:08
【问题描述】:

当包含 nav.php 文件时,在下拉菜单中设置“活动”状态。

这篇文章不是一个问题,而是一个常见问题的php新手级解决方案。

我使用以下常用方法构建了我的网站:

<?php      
include "admin.php"; // handles MySQL connection and mysqli prepared stmts. etc. 
include "header.php";  
include "nav.php";  // nav with drop down menu        
?>
<!-- html of the page -->

在我尝试将适当的选项卡和子选项卡设置为“活动”之前,一切都很好。当然,任何网站设计师都遇到过这个问题。我试着用谷歌搜索没有真正满意的答案。所以我想出了我自己的。

假设一个网站有 4 个标签(about_us products faq contact),最深的下拉菜单是 6 个子标签。

我在 admin.php 的底部添加了这一行:

$about = $prod = $faq = $cont = $sub1 = $sub2 = $sub3 = $sub4 = $sub5 = $sub6 = "nothing";

不确定是否需要,但我将其添加到 CSS 中

.nothing { }

网站每一页的顶部是:

<?php          
include "admin.php"; 

/** if this page corresponds to the products tab and the fifth sub menu **/ 
$prod = $sub5 = "active"; // or whatever css uses as the class name 

include "header.php";  
include "nav.php";  // nav with drop down menu            
?>

在 nav.php 中回显 css 类名

<div id="centeredmenu">
 <nav id="nav">
  <ul>
    <li class="<?php echo $about; ?>"><a href="about.php">About Us</a>
       </li>    
    <li class="<?php echo $prod; ?>"><a href="/products/index.php">Our Products</a>
      <ul>
        <li class="<?php echo $sub1; ?>"><a href="/products/index.php">Wedgets</a></li>
        <li class="<?php echo $sub2; ?>"><a href="/products/grommets.php">Grommets</a></li>
        <li class="<?php echo $sub3; ?>"><a href="/products/some.php">Some</a></li>
        <li class="<?php echo $sub4; ?>"><a href="/products/more.php">More</a></li>
        <li class="<?php echo $sub5; ?>"><a href="/products/useless.php">Useless</a></li>
        <li class="<?php echo $sub6; ?>"><a href="/products/stuff.php">Stuff</a></li>
     </ul>
   </li>

所以这是我的新手设置激活包含的导航文件的方法。

我很想看看专业人士如何解决这个问题。让我们为获得同行投票最多的解决方案打勾。我敢肯定它不会是我的。

【问题讨论】:

标签: php css drop-down-menu


【解决方案1】:

用 CSS 处理所有这些,隐藏样式,并使当前页面中的样式处于活动状态(取消隐藏它)会更容易吗,这样类将始终相同但不会显示,更少的 if 语句,更少我猜作业更容易维护。

【讨论】:

  • 我希望您或其他人会发布一些代码来展示如何操作。
【解决方案2】:

如果我正确地遵循了 Fred -ii- 丢弃的面包屑,我将提供另一种设置“actve”的方法。

http://example.com/someone/has/too/complicated/of_a/dirctory/structure.php

在我的情况下,因为我已经有一个管理文件,所以 php 代码将进入 admin.php

admin.php

<?php
  $path = $_SERVER['PHP_SELF'];

  // get filename
  $file = $_SERVER[PHP_SELF]; 
  $file = basename($file);  //structure.php

  // get directory
  $dir = dirname($path); 
  $dir = explode("/", $dir);
  $dir = array_slice($dir, -1, 1); // by adjusting the offset each directory name can be isolated 
  $dir = implode($dir);
  ?>

在 nav.php 中

<div id="centeredmenu">
 <nav id="nav">
  <ul>
    <li <?php if($file === "about.php")echo 'class="active"'; ?>><a href="about.php">About Us</a>
       </li>    
    <li <?php if($dir === "products")echo 'class="active"'?>><a href="/products/index.php">Our Products</a>
      <ul>
        <li <?php if ($file === "index.php")echo 'class="active"'; ?>><a href="/products/index.php">Wedgets</a></li>
      </ul>
   </li>

我看到在此方法中获取目录名称的唯一问题是您正在向后工作(从 /structure.php 回到 /someone/

当使用深层目录结构时,这将特别令人困惑,其中哪个目录级别应该是设置为“活动”的目录级别不一致。

因此,出于下拉菜单的目的,我个人会使用:

admin.php

  <?php
  // get directory
  $dir = dirname($path); 
  $dir = explode("/", $dir);
  $dir = array_reverse ($dir, true); // Reverse the order 

  // and then select the directory names by referring to their depth in the directory structure.

  $dir1 = isset($dir[1])? $dir[1]) : NULL; //someone
  $dir2 = isset($dir[2])? $dir[2]) : NULL; //has 
  $dir3 = isset($dir[3])? $dir[3]) : NULL; //too
  $dir4 = isset($dir[4])? $dir[4]) : NULL; //complicated
  $dir5 = isset($dir[5])? $dir[5]) : NULL; //of_a
  $dir6 = isset($dir[6])? $dir[6]) : NULL; //directory
  ?>

导航.php

<li <?php if ($dir4 === "complicated")echo 'class="active"'; ?>>
<a href="/someone/has/too/complicated/index.php">Wedgets</a></li>

// another area of the website could be:
<li <?php if ($dir4 === "this")echo 'class="active"'; ?>>
<a href="/someone/keeps/making/this/directory/structure/too/long/index.php">More_Wedgets</a></li>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多