在php中动态创建类

时间:2016-06-09 21:13:37

标签: php mysql css menu nav

我正在根据dataBase上注册的类别动态创建菜单,我需要的是:    当有人点击链接时,此选项应该使css类为“active”,在页面中显示用户,甚至页面是动态创建的。    我不知道怎么做,因为我是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>

3 个答案:

答案 0 :(得分:0)

首先,您循环遍历$ bundle exec teaspoon -f documentation component: tree $ctrl.fetch_book(book_id) fetches the book with id=book_id (FAILED - 1) # Selecting options 5 # Received: {"title":"Sanctuary","id":"5"} # Options: {"title":"Sanctuary","id":"5"} # constructor: "Resource" Failures: 1) component: tree $ctrl.fetch_book(book_id) fetches the book with id=book_id Failure/Error: Expected Resource({ title: 'Sanctuary', id: '5', $promise: Promise({ $$state: Object({ status: 1, value: <circular reference: Object> }) }), $resolved: true }) to equal Object({ title: 'Sanctuary', id: '5' }). Finished in 0.02600 seconds 1 example, 1 failure 循环并添加&#34;行&#34;的ID每个while元素。我建议你为每个列表项创建唯一的ID。我没有必要提倡以您拥有的方式使用代码,例如,您的代码编辑就是为了这样做:

<li>

然后当有人点击你的链接时,只需使用类似这样的东西:

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;
      }
}

答案 1 :(得分:0)

尝试类似的东西:

<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>

答案 2 :(得分:0)

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

menu.php:

templateUrl: "http://" + window.location.host + "/AngularApp/Templates/inputcontrol.html"

在我的index.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>

和我的categories.php:

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

当然在我的CSS中:

  <?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...

这对我来说非常合适,谢谢你的帮助。