php - how to set a page active automatically when it is loaded


Question: 

i want to change the active class when each page loaded. my jquery code is

$(document).ready(function () {
$('.nav li a').click(function(e) {

    $('.nav li a.active').removeClass('active');

    $(this).addClass('active');
    e.preventDefault();
});
});

and my html code is

<ul class="nav nav-stacked bg-nav">
            <li class="active"><a href="<? echo base_url(); ?>index.php/admin/admin_home"><span     class="glyphicon glyphicon-home"></span>&nbsp;Home</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_student">Student</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_teacher">Teacher</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_parent">Parent</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_subject">Subject</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_class">Class</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_exam">Exam</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_mark_attendence">Marks</a>    </li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_grade">Exam Grade</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_routine">Class Routine</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_payment">Payment</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_library">Library</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_transport">Transport</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_notice">Notice Board</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_settings">Settings</a></li>
            <li><a href="<? echo base_url(); ?>index.php/admin/admin_profile">Profile</a></li>
        </ul>

the problems are 1. it does not shows the first page ie. home.. as active. others are okey 2. it does not redirect to the url defined in the href.




1 Answer: 

The logic can be used in Codeigniter using PHP, I think you don't need to use jquery here like,

if you are calling admin_student function in controller then pass active class for that li like,

// in controller
$activeClass='admin_student';

And in the view page use a switch-case like,

$activeClass=isset($activeClass) ? $activeClass : 'admin_home';
$admin_student='';//make variables for all li
$admin_home='';
switch($activeClass){
    case 'admin_student':
        $admin_student='active';
        break;
    ....
    default:
        $admin_home='active';
        break;
}

And add this variable to your li like,

<li class="<?php echo $admin_home;?>"><a href="...">Home</a></li>
<li class="<?php echo $admin_student;?>"><a href="...">Student</a></li>

This will solve your first problem, and the second problem occurs because of using e.preventDefault() so don't use it if you want redirection of URLs.

 

More Articles


php - Laravel default selection on form when loading page

I have a select tag on a form, and I want to populate a default option, this default option will be the one that is searched. I can get it through something like $_GET but it does not work in my case, as I am looping through and getting all the values from the database. <select name="location"&

exchange server - What e-mail address ("From" vs. "Mail From") is considered by my block list?

We are getting a lost of spam /phishing e-mails where the [From] is showing our own domain (e.g. "john@foobar.com"), but the [Mail From] (return-path) in the e-mail header is showing "root@foo-574401.hostbar.com".Question is now, which e-mail address is actually considered by the Exchange Online blo

How to use binary matrix in CSV for apriori alogorithm in r

I am trying to use the binary matrix containing transactions for the apriori algorithm I don't know how to implement it data_purchase Txn Bag Blush Nail.Polish Brushes Concealer Eyebrowpencil Bronzer1 1 0 1 1 1 1 0 12 2 0 0 1


email - Getting Mail body In PHP

What I am doing:First I am trying to get email and save the reply of user in my database.For this I am using PHP imap method to get mail from my email address.Code:function get_email(){ //define server, email , password $server = '{server.example.com:143}INBOX'; $user_name = 'login';

Facebook Like Button: Unify a Url & a Facebook Page

In Facebook docs it says:Can I link the Like button to my Facebook page?Yes. Simply specify the URL of your Facebook page inthe href parameter of the button.My problem is that I don't want the Facebook page to be the target of the 'like'.So currently, the like button on my site attributes the likes

macos - uninstall python on mac Mavericks

I'm a new Mac user. I installed python 2.7.6 but I cannot run my scripts (through IDLE or simply a script). I want to reinstall it but I don't want to mess with the version which comes pre-installed on mac.Thank you for your help.note: there are questions on stackOverflow that are similar to mine, y


R: turning list items into objects

I have a list of objects that I've created manually, like this:rand1 <- rnorm(1e3)rand2 <- rnorm(1e6)myObjects <- NULLmyObjects[[1]] <-rand1myObjects[[2]] <-rand2names(myObjects) <- c("rand1","rand2")I'm working on some code that bundles up objects and puts them up in S3. Then I ha

scalatest - Confusing type mismatch in Scala

I have:val words = List("all", "the", "words", "all", "the", "counts", "all", "day")val map = Exercise02.count(words.iterator)val expected = Map("all" -> 3, "the" -> 2, "words" -> 1, "counts" -> 1, "day" -> 1)where Exercise02.count is java.util.Iterator[String] => Map[String, Int]

Apply function to one element of a list in Python

I'm looking for a concise and functional style way to apply a function to one element of a tuple and return the new tuple, in Python.For example, for the following input:inp = ("hello", "my", "friend")I would like to be able to get the following output:out = ("hello", "MY", "friend")I came up with t

authentication - Sitecore - Prevent access to a page, but still show it in the navigation

In Sitecore I have denied access to a particular page for the anonymous user. This works correctly, but it also means that the page does not appear in the navigation menus and sitemap (both XSLT).What I would like is for the user to be able to see the link, but be redirected to a Register/Login page