1

大家好,只是想问一个简单的问题。这都是关于创建导航链接的。因为我有一个导航链接。如果用户选择一个链接,该链接将被突出显示,如果用户单击另一个链接,该链接将被突出显示,而前一个链接不会。在创建我的链接时,我使用一个数组并循环它来调用动作。

这是我的代码

MY CONTROLLER

 public function homepage(){

        $data['title'] = "Welcome";
        $data['copyright'] = date('Y');
        $data['queryViewEntries'] = $this->category_model->viewAllEntry();
        $data['link'] = "category";     
        $this->load->view('common/header_common',$data);
        $this->load->view('common/navigation',$data);
        $this->load->view('User/contents/homepage');
        $this->load->view('common/footer_common',$data);

    }   

MY VIEW

 <li class="nav-header"></li>
          <?php

                $highlight = $link;

                $section = array(
                    'CATEGORIES' => 'user_controller/homepage',
                    'ITEMS' => 'item_controller/index',
                    'SUPPLIERS' => 'supplier_controller/index'
                );

                foreach($section as $key => $value){

                    echo "<li class='active'>".anchor($value,$key)."</li>"; //this is the problem how can i set the cliked link to active and the other will be not.


                }

          ?>
</li>

我希望你们能帮助我。谢谢。

4

2 回答 2

1

https://www.codeigniter.com/user_guide/libraries/uri.html

 //add to your controller 
  $data['url_link'] = $this->uri->segment(1, 0); //your URL segment /www.website/items


  //view
  foreach($section as $key => $value){
         //class name
        $className = ($key === $url_link) ? 'active' : 'no-active';
        echo "<li class='$className'>".anchor($value,$key)."</li>";
    }
于 2013-07-18T09:47:45.693 回答
0

首先,您可能希望将列表包含在 <ul> 中,而不是在打开列表后立即关闭它。(你的编辑可能帮了你这个忙)。在您的循环中,您需要根据当前链接检查每个键并相应地应用样式。就像是 ....

    <ul class="nav-header">
          <?php

                $highlight = $link;

                $section = array(
                    'CATEGORIES' => 'user_controller/homepage',
                    'ITEMS' => 'item_controller/index',
                    'SUPPLIERS' => 'supplier_controller/index'
                );



                  foreach($section as $key => $value){ 
                      if ($key == $link){
                           echo "<li class='active'>";
                      } else{ 
                         echo "<li class='inactive'>";
                      }
                      echo anchor($value,$key)."</li>"; 

                    }

          ?>
</ul>

您必须确保视图中的 'section' 数组和 $data['link'] 具有相同的值。即重命名它们以匹配例如在您的控制器中

$data['link'] = 'CATEGORIES'

或者在你看来`$section = array('category' => 'user_controller/homepage', .....);

于 2013-07-18T09:57:15.517 回答