如何在PHP中根据当前页面动态添加“active”类到导航菜单项?

huangapple go评论64阅读模式
英文:

How to dynamically add the "active" class to the navigation menu item based on the current page in PHP?

问题

Sure, here's the translated content you requested:

我有一个PHP脚本,它在我的inc_header.php文件中包含了导航菜单。

Index.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <?php include('inc_metadata.php');?>
  </head>
  <body>
    <!--Header-->
    <?php include('inc_header.php');?>
    <?php
        $allowed = array('about','home','gallery','videogallery','faculties','contact','admission','facilities','activity','achievements',);
        $page = ( isset($_GET['page']) ) ? $_GET['page'] : 'index';
       if ( in_array($page, $allowed) )
           {
             include("$page.php");
             include('inc_config.php');
           }
      else {
           include("home.php");
         }
        ?>
      <!--footer-->
    <?php include('inc_footer.php'); ?>
    <?php include('inc_bottom.php'); ?>
  </body>
</html>

我想动态向与当前页面对应的

  • 元素添加类"active"。例如,如果当前页面是"home.php",我希望"Home"对应的
  • 元素上动态添加类"active"。

    根据我的代码,导航栏是固定在'index.php'中的。

    我该如何使用PHP实现这一点?我希望根据当前页面的URL动态应用"active"类。谢谢!

    我尝试使用ChatGPT,但没有成功。我是一个自学的初学者,希望有人能帮助我。

    英文:

    I have a PHP script that includes the navigation menu in my inc_header.php file.

    &lt;header id=&quot;header&quot; class=&quot;fixed-top&quot;&gt;
      &lt;div class=&quot;container-fluid d-flex justify-content-between align-items-center&quot;&gt;
        &lt;a href=&quot;index.php&quot; class=&quot;logo&quot;&gt;
          &lt;img src=&quot;assets/img/logo.png&quot; alt=&quot;IHRD logo&quot; class=&quot;img-fluid&quot;&gt;
        &lt;/a&gt;
        &lt;h1 class=&quot;logo me-auto me-lg-0&quot;&gt;
          &lt;a class=&quot;d-none d-lg-block&quot; href=&quot;index.php&quot;&gt;Technical Higher Secondary School Puthuppally&lt;/a&gt;
          &lt;a class=&quot;d-block d-lg-none&quot; href=&quot;index.php&quot;&gt;THSS Puthuppally&lt;/a&gt;
        &lt;/h1&gt;
        &lt;nav id=&quot;navbar&quot; class=&quot;navbar order-last order-lg-0&quot;&gt;
          &lt;ul&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=home&quot;&gt;Home&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=about&quot;&gt;About&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=admission&quot;&gt;Admissions&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=faculties&quot;&gt;Faculties&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=facilities&quot;&gt;Facilities&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=activity&quot;&gt;Activities&lt;/a&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=achievements&quot;&gt;Achievements&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class=&quot;dropdownlist&quot;&gt;
              &lt;button class=&quot;dropbtn&quot;&gt;Gallery&lt;/button&gt;
              &lt;div class=&quot;dropdownlist-content&quot;&gt;
                &lt;a href=&quot;?page=gallery&quot;&gt;Images&lt;/a&gt;
                &lt;a href=&quot;?page=videogallery&quot;&gt;Videos&lt;/a&gt;
              &lt;/div&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;a href=&quot;?page=contact&quot;&gt;Contact&lt;/a&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
          &lt;i class=&quot;bi bi-list mobile-nav-toggle&quot;&gt;&lt;/i&gt;
        &lt;/nav&gt;
        &lt;!-- .navbar --&gt;
        &lt;!--social links--&gt;
      &lt;/div&gt;
    &lt;/header&gt;
    

    Index.php

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
        &lt;?php include(&#39;inc_metadata.php&#39;);?&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;!--Header--&gt;
        &lt;?php include(&#39;inc_header.php&#39;);?&gt;
        &lt;?php
            $allowed = array(&#39;about&#39;,&#39;home&#39;,&#39;gallery&#39;,&#39;videogallery&#39;,&#39;faculties&#39;,&#39;contact&#39;,&#39;admission&#39;,&#39;facilities&#39;,&#39;activity&#39;,&#39;achievements&#39;,);
            $page = ( isset($_GET[&#39;page&#39;]) ) ? $_GET[&#39;page&#39;] : &#39;index&#39;;
           if ( in_array($page, $allowed) )
               {
                 include(&quot;$page.php&quot;);
                 include(&#39;inc_config.php&#39;);
               }
          else {
               include(&quot;home.php&quot;);
             }
            ?&gt;
          &lt;!--footer--&gt;
        &lt;?php include(&#39;inc_footer.php&#39;); ?&gt;
        &lt;?php include(&#39;inc_bottom.php&#39;); ?&gt;
      &lt;/body&gt;
    &lt;/html&gt;
    

    I want to dynamically add the class "active" to the <li> element that corresponds to the current page. For example, if the current page is "home.php", I want the corresponding <li> element for "Home" to have the class "active" added to it.
    According to my code Navbar is fixed in 'index.php'.
    How can I achieve this using PHP? I want the active class to be applied dynamically based on the current page URL. Thank you!

    I tried chatgpt but didn't work.
    I'm a self taught beginner, I hope someone help me.

    答案1

    得分: 2

    首先,你需要将你的 include 语句放在 GET 值测试之后。

    <?php 
        $allowed = array('about','home','gallery','videogallery','faculties','contact','admission','facilities','activity','achievements',);
        $page = ( isset($_GET['page']) ) ? $_GET['page'] : 'index';
        if ( in_array($page, $allowed) ) {
            // 在这里测试你要显示的页面,以便检查相应的菜单项
            // $page = $_GET['page'];
            include('inc_header.php');
            include("$page.php");
            include('inc_config.php');
        } else {
            include("home.php");
        }
    ?>
    

    在你的菜单中,按照以下方式修改你的链接:

    <li><a href="?page=home" class="<?php if($page == "home") { echo "selected"; } ?>">Home</a></li>
    <li><a href="?page=about" class="<?php if($page == "about") { echo "selected"; } ?>">About</a></li>
    // 其中 .selected 是你要应用的 CSS 样式
    
    英文:

    First, you'll need to move your include after you test the GET value.

    &lt;?php 
        $allowed = array(&#39;about&#39;,&#39;home&#39;,&#39;gallery&#39;,&#39;videogallery&#39;,&#39;faculties&#39;,&#39;contact&#39;,&#39;admission&#39;,&#39;facilities&#39;,&#39;activity&#39;,&#39;achievements&#39;,);
        $page = ( isset($_GET[&#39;page&#39;]) ) ? $_GET[&#39;page&#39;] : &#39;index&#39;;
        if ( in_array($page, $allowed) )
           {
           // here you test the page you&#39;ll show, so the corresponding menu item can be checked
           // $page = $_GET[&#39;page&#39;];
             include(&#39;inc_header.php&#39;);
             include(&quot;$page.php&quot;);
             include(&#39;inc_config.php&#39;);
           }
           else {
           include(&quot;home.php&quot;);
         }
        ?&gt;
    

    in your menu, modify your links as follow:

     &lt;li&gt;&lt;a href=&quot;?page=home&quot; class=&quot;&lt;?php if($page == &quot;home&quot;) { echo&quot;selected&quot;; } ?&gt;&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href=&quot;?page=about&quot; class=&quot;&lt;?php if($page == &quot;about&quot;) { echo&quot;selected&quot;; } ?&gt;&quot;&gt;About&lt;/a&gt;&lt;/li&gt;  
    // where .selected is the CSS style you want to apply
    

  • huangapple
    • 本文由 发表于 2023年5月13日 15:51:25
    • 转载请务必保留本文链接:https://go.coder-hub.com/76241641.html
    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定