如果未在父类别下添加子类别,如何删除“<span class =”caret“> </span>”

时间:2017-02-15 09:58:15

标签: codeigniter dropdown caret

我必须隐藏下拉列表“插入符号”。如果没有在codeigniter中添加子类别。

这是我的代码: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<ul class="nav navbar-nav">
 <?php
 $this->load->module('store_categories');
 foreach ($parent_categories as $key => $value) {
    $parent_cat_id = $key;
    $parent_cat_title = $value;
 ?>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><?= $parent_cat_title ?>
      <span class="caret"></span>
      </a>
      <ul class="dropdown-menu">
          <?php
          $query = $this->store_categories->get_where_custom('parent_cat_id', $parent_cat_id);
          foreach ($query->result() as $row) {
            echo '<li><a href="#">'.$row->cat_title.'</a></li>';
          }
          ?>
      </ul>
    </li>
    <?php
    }
    ?>
</ul>

1 个答案:

答案 0 :(得分:0)

编写一个函数来生成查询内容,然后检查并使用它的值。

<?php
    function getQueryContent($parent_cat_id, $that) {
        $queryContent = "";
        $query = $that->store_categories->get_where_custom('parent_cat_id', $parent_cat_id);
        foreach ($query->result() as $row) {
            $queryContent .= '<li><a href="#">'.$row->cat_title.'</a></li>';
        }
        return $queryContent;
    }
?>

<ul class="nav navbar-nav">
 <?php
 $this->load->module('store_categories');
 foreach ($parent_categories as $key => $value) {
    $parent_cat_id = $key;
    $parent_cat_title = $value;
    $queryContent = getQueryContent($parent_cat_id, $this);
 ?>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><?= $parent_cat_title ?>
      <?php
      if ($queryContent) {
      ?>
      <span class="caret"></span>
      <?php
      }
      ?>
      </a>
      <ul class="dropdown-menu">
          <?php
          echo $queryContent;
          ?>
      </ul>
    </li>
    <?php
    }
    ?>
</ul>