How can I set class=“active” to navigation menu in codeigniter?

后端 未结 4 1409
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 18:23

I just start up to be a web developer. Now I create a dynamic website for the first time. I don\'t know how to set class=\"active\" to the navigation menu. Here is

相关标签:
4条回答
  • 2021-02-09 18:45

    I have used Codeigniter URI Class $this->uri->segment();

    Please look at my code:

    <li class="<?=($this->uri->segment(1)==='technology')?'active':''?>"><a href="<?php echo base_url('technology')?>">Tech</a></li>
    

    Please note....

    class="<?=($this->uri->segment(1)==='technology')?'active':''?>"
    

    Also you can use URI Class $this->uri->uri_string()

    Here is my active class for home page(index)

    <li class="<?=($this->uri->uri_string()=== '')?'active':''?>"><a href="<?php echo base_url('')?>">Home</a></li>
    
    0 讨论(0)
  • 2021-02-09 18:55

    Try this one. I think no need of javascript or jquery.

    If you are using codeigniter then you can use URI Class. Get the menu from url using

    $this->uri->segment();  
    

    and apply to you code like below

    <li>
        <a href="<?php echo base_url(); ?>patient/listpatient"><i class="glyphicon glyphicon-list-alt fa-lg <?php if($this->uri->segment(1)=="memu_name"){echo "active";}?>"> </i> List Patients </a> 
    
    </li>
    
    0 讨论(0)
  • 2021-02-09 19:00

    You can use $this->uri->segment();

    <li>
        <a href="<?php echo base_url(); ?>patient/createpatient" <?php if($this->uri->segment(1)=="menu_name"){echo 'class="active"';}?> ><i class="fa fa-users fa-lg"></i> Create Patient </a>
    </li>
    <?php } ?>
        <li>
           <a href="<?php echo base_url(); ?>patient/listpatient" <?php if($this->uri->segment(1)=="menu_name"){echo 'class="active"';}?> ><i class="glyphicon glyphicon-list-alt fa-lg"> </i> List Patients </a> 
        </li>
    <?php if( $usertype == "Admin"){?>
        <li>
        <a href="<?php echo base_url(); ?>user/" <?php if($this->uri->segment(1)=="menu_name"){echo 'class="active"';}?> ><i class="fa fa-list fa-lg"> </i> List Users </a> 
        </li>
    
    0 讨论(0)
  • 2021-02-09 19:06

    I do this on most projects. How I achieve this is like this;

    <a href="<?php echo site_url('patient/listpatient'); ?>" class="<?php if($this->uri->uri_string() == 'patient/listpatient') { echo 'active'; } ?>"><i class="glyphicon glyphicon-list-alt fa-lg"></i> List Patients</a>
    

    It's matching the current uri string, with the links href. If it matches, it add's an active class to the link.

    Hope this helps.

    I have also used site_url over base_url

    0 讨论(0)
提交回复
热议问题