shibbir ahmed
shibbir ahmed

Reputation: 1024

How can I show css active class using php?

Here is my site nav.php is look like :

<li <?php echo $active; ?>><a href="index">Home</a></li>
<li <?php echo $active; ?>><a href="report">Report</a></li>
<li <?php echo $active; ?>><a href="chart">Chart</a></li>
<li <?php echo $active; ?>><a href="export">Export</a></li>

and header.php page is look like :

<?php
$pageName = basename($_SERVER['PHP_SELF']);
$explode    = explode('.', $pageName);
$title  = ucfirst($explode[0]);    

if($title == "Report") {
    $active     = 'class="active"';
} else {
    $active = '';
}
?>

I am using above code to show css .active class on currently view any page. but it's adding .active class to all pages.

Note : I have 4 pages. e.g: index, report, chart and export.

and My full page structure is like that :

In index.php page I am calling following page.

header.php
nav.php
js.php
footer.php

Upvotes: 1

Views: 159

Answers (6)

anwerj
anwerj

Reputation: 2488

Wrapping will give you additional flexibility over static content

$pageName = basename($_SERVER['PHP_SELF']);
$explode    = explode('.', $pageName);
$title  = ucfirst($explode[0]);   
$nav = [
    'index' => 'Home',
    'report' => 'Report',
    'chart' => 'Chart',
    'export' => 'Export'
];

foreach ($nav as $key => $value) {
    echo '<li class="'.($key == $title ? 'active' : '').'>';
    echo '<a href="'.$key.'">'.$value.'</a>' ;
    echo '</li>'
}

Upvotes: 1

Yuriy Perevoznikov
Yuriy Perevoznikov

Reputation: 81

Try to do it like this:

<li class="<?= ('Home' == $title) ? 'active' : ''; ?>">
    <a href="index">Home</a>
</li>
<li class="<?= ('Report' == $title) ? 'active' : ''; ?>">
    <a href="report">Report</a>
</li>
<li class="<?= ('Chart' == $title) ? 'active' : ''; ?>">
    <a href="chart">Chart</a>
</li>
<li class="<?= ('Export' == $title) ? 'active' : ''; ?>">
    <a href="export">Export</a>
</li>

Upvotes: 1

Matt Backslash
Matt Backslash

Reputation: 804

Because you're defining $active only once and for all at the same time.

You probably should work with an array:

$navigation = array('index', 'report', 'chart', 'export);

And now you loop through that array, checking, which is active:

foreach($navigation as $n) {
    if(ucfirst($explode('.', $pageName) == $n) {
        echo '<li class="active"><a href="' . $n . '">' . ucfirst($n) . '</a></li>';
    } else {
        echo '<li><a href="' . $n . '">' . ucfirst($n) . '</a></li>';
    }
}

Upvotes: 1

Noman
Noman

Reputation: 1487

Easiest way:

$pages = array();
$pages["home.php"] = "Home";
$pages["report.php"] = "Report";
$pages["chart.php"] = "Chart";
$pages["export.php"] = "Export";

$active = "home.php";

<?php foreach($pages as $url=>$title):?>
  <li>
       <a <?php if($url === $active):?>class="active"<?php endif;?> href="<?php echo $url;?>">
         <?php echo $title;?>
      </a>
  </li>
<?php endforeach; ?>

Upvotes: 1

Sasikumar
Sasikumar

Reputation: 863

try doing the following

<li <?php echo ($pageName=="index.php")?" class='active'" :""; ?>><a href="index">Home</a></li>
<li <?php echo ($pageName=="report.php")?" class='active'" :""; ?>><a href="report">Report</a></li>
<li <?php echo ($pageName=="chart.php")?" class='active'" :""; ?>><a href="chart">Chart</a></li>
<li <?php echo ($pageName=="export.php")?" class='active'" :""; ?>><a href="export">Export</a></li>

Upvotes: 0

shubham715
shubham715

Reputation: 3302

use this

$activePage = basename($_SERVER['PHP_SELF'], ".php");

and in your li

 <li  <?php if($activePage=="Report") { echo "class='active'"; } ?>><a href="index">Home</a></li></li>

Upvotes: 2

Related Questions