【问题标题】:creating class active dynamically in php在php中动态创建类活动
【发布时间】:2016-10-10 17:10:21
【问题描述】:

我正在根据数据库上注册的类别动态创建一个菜单,我需要的是: 当有人点击链接时,这个选项应该有 css 类“活动”,显示用户所在的页面,甚至页面是动态创建的。 我不知道该怎么做,因为我是 php 学生,我在谷歌中找不到“动态菜单”的信息,非常感谢。

<nav class="menu">
   <ul class="list">
      <li class="line"><a id="item" href="index.php">Home</a></li>

       <?php
       $categories = $db->select("select * from tbl_category");
       if($categories){
          while ($result = $categories->fetch_assoc()){
             echo <<<HTML
             <li id="line" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
HTML;
          }
       }
       ?>
   </ul>
<nav>

【问题讨论】:

    标签: php mysql css menu nav


    【解决方案1】:

    首先,您正在循环通过 while 循环并将“line”的 id 添加到每个 &lt;li&gt; 元素。我建议您为每个列表项创建唯一的 ID。我不需要提倡按照你的方式使用代码,只是作为一个例子,这里是你的代码编辑来做到这一点:

    if($categories){
          $i = 1;
          while ($result = $categories->fetch_assoc()){
             $i++;
             echo <<<HTML
             <li id="line$i" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
             HTML;
          }
    }
    

    然后当有人点击你的链接时,只需使用 jQuery 就可以了:

    $(".item").click(function() {
        $(this).parent('li').addClass('active');
    });
    

    【讨论】:

      【解决方案2】:

      试试这样的:

      <nav class="menu">
         <ul class="list">
            <li class="line"><a id="item" href="index.php">Home</a></li>
      
             <?php
             $current_page = isset($_GET['category']) ? $_GET['category']: -1;
      
             $categories = $db->select("select * from tbl_category");
             if($categories){
                while ($result = $categories->fetch_assoc()){
                   echo '<li id="line">';
                   if($result['category'] === $current_page) {
                      // If we're currently in the active page then add the active class to the <a>
                      echo   '<a class="item active" href="categories.php?category='. $result[id] .'">';
                   } else {
                      echo   '<a class="item" href="categories.php?category='. $result[id] .'">';
                   }
                   echo       $result['name'];
                   echo   '</a>';
                   echo  '</li>';
                }
             }
             ?>
         </ul>
      <nav>
      

      【讨论】:

        【解决方案3】:

        谢谢大家的帮助,根据你的代码,我解决了这个问题:

        menu.php:

        <nav class="menu" id="menu">
        
           <ul id="list" class="list">
        
              <li id="line" class="<?php if($presentPage == '0')echo 'active';?>"><a id="item" class="item" href="index.php">Home</a></li>
        
           <?php
           function checked($pp, $id){
              if($pp == $id){
                 $status = 'active';
                 return $status;
              }
           }
           $query = "select * from tbl_category";
           $category = $db->select($query);
           if($category){
              while ($result = $category->fetch_assoc()){
        
                 echo "
                    <li id='line' class='".checked($presentPage, $result['id'])."'><a id='item' class='item' href='categories.php?category=".$result['id']."'>".$result['name']."</a></li>
                 ";//end echo
        
              }//end while
           }//end if
           ?>
           </ul>
        </nav>
        

        在我的 index.php 中:

          <?php $presentPage = 0;?>
          <?php require_once 'header.php';?>
          <?php require_once 'menu.php';?>
        

        在我的 categories.php 中:

          <?php
             if (!isset($_GET['category']) || $_GET['category'] == NULL) {
                $presentPage = 0;
             }else{
                $presentPage = intval($_GET['category']);//just to make sure  that the variable is a number
             }
          ?>
        
          <?php require_once 'header.php';?>
          <?php require_once 'menu.php';?>
          ///...my code...
        

        当然在我的 CSS 中:

        .active{
           background: linear-gradient(#821e82, #be5abe);
        }
        

        这对我来说非常有效,感谢所有帮助。

        【讨论】:

          猜你喜欢
          • 2011-12-28
          • 1970-01-01
          • 2015-12-28
          • 1970-01-01
          • 2012-09-02
          • 2016-03-19
          • 1970-01-01
          • 2011-04-16
          • 2020-06-22
          相关资源
          最近更新 更多