rochellecanale
rochellecanale

Reputation: 205

Change anchor class when navigating through pages using Codeigniter

Hello guys just want to ask a simple question. It is all about creating a navigation link. Because I have a navigation link. And if the user choose a link the link will be highlighted and if the user click another link that link will be highlighted and the previous link will not. In creating my link I use an array and i loop it to call the action.

Here's my code

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>

I hope guys you can help me. Thanks.

Upvotes: 1

Views: 472

Answers (2)

Dezigo
Dezigo

Reputation: 3256

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

Upvotes: 1

Loopo
Loopo

Reputation: 2195

first, you probably want to enclose your list in a < ul> and not close your list right after you open it. (your editor probably did you that favour). In your loop, you need to check each key against the current link and apply a style accordingly. something like ....

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

You have to make sure that your 'section' array in your view and your $data['link'] have the same value. i.e. rename them to match e.g. in your controller

$data['link'] = 'CATEGORIES'

OR in your view `$section = array( 'category' => 'user_controller/homepage', ..... );

Upvotes: 0

Related Questions