如何在codeigniter中设置活动类以进行导航?

时间:2017-10-04 07:19:54

标签: codeigniter

我有一个由不同页面组成的导航,我想将当前页面设置为活动状态。我的代码如下

    <div class="container tablistitem">
     <ul>
     <li class="active"><a href="<?= site_url('user/dashboard'); ?>">Timeline</a></li>
     <li><a href="<?= site_url('user/pages/about'); ?>">About</a></li>
     <li><a href="<?= site_url('user/pages/friends'); ?>">Friends</a></li>
     <li><a href="<?= site_url('user/pages/photos'); ?>">Photos</a></li>
   </ul>
  </div>

我使用以下代码来获取结果但不起作用:

    <div class="container tablistitem">
     <ul>
     <li class="<?php if($currentpage="") { echo "active"; } ?>"><a href="<?= site_url('user/dashboard'); ?>">Timeline</a></li>
     <li class="<?php if($currentpage="about") { echo "active"; } ?>"><a href="<?= site_url('user/pages/about'); ?>">About</a></li>
     <li class="<?php if($currentpage="friends") { echo "active"; } ?>"><a href="<?= site_url('user/pages/friends'); ?>">Friends</a></li>
     <li class="<?php if($currentpage="photos") { echo "active"; } ?>"><a href="<?= site_url('user/pages/photos'); ?>">Photos</a></li>
   </ul>
  </div>

1 个答案:

答案 0 :(得分:0)

您可以针对您的问题尝试此解决方案

<?php 
$controller = $this->router->fetch_class(); // You can Match controller name
$method = $this->router->fetch_method(); // You can Match mathod name
?>



<div class="container tablistitem">
    <ul>
        <li class="<?php
        if ($controller = "dashboard" && $method = "index") {
            echo "active";
        }
        ?>"><a href="<?= site_url('user/dashboard'); ?>">Timeline</a></li>

        <li class="<?php
        if ($controller = "pages" && $method = "about") {
            echo "active";
        }
        ?>"><a href="<?= site_url('user/pages/about'); ?>">About</a></li>

        <li class="<?php
            if ($controller = "pages" && $method = "friends") {
                echo "active";
            }
            ?>"><a href="<?= site_url('user/pages/friends'); ?>">Friends</a></li>

        <li class="<?php
            if ($controller = "pages" && $method = "photos") {
                echo "active";
            }
            ?>"><a href="<?= site_url('user/pages/photos'); ?>">Photos</a></li>
    </ul>
</div>