How to set current page active in php
It would be easier if you would build an array of pages in your script and passed it to the view file along with the currently active page:
//index.php or controller
$pages = array();
$pages["offnungszeiten.php"] = "Öffnungszeiten";
$pages["sauna.php"] = "Sauna";
$pages["frauensauna.php"] = "Frauensauna";
$pages["custom.php"] = "Beauty Lounge";
$pages["feiertage.php"] = "Feiertage";
$activePage = "offnungszeiten.php";
//menu.php
<?php foreach($pages as $url=>$title):?>
<li>
<a <?php if($url === $activePage):?>class="active"<?php endif;?> href="<?php echo $url;?>">
<?php echo $title;?>
</a>
</li>
<?php endforeach;?>
With a templating engine like Smarty your menu.php would look even nicer:
//menu.php
{foreach $pages as $url=>$title}
<li>
<a {if $url === $activePage}class="active"{/if} href="{$url}">
{$title}
</a>
</li>
{/foreach}
Add class=active to active page using PHP
Figured out the ANSWER...I was over thinking it.
HTML
<ul id="mainnav">
<li class="<?php if ($first_part=="") {echo "active"; } else {echo "noactive";}?>"><a href="#">Home</a></li>
<li class="<?php if ($first_part=="tutorials") {echo "active"; } else {echo "noactive";}?>"><a href="#">Tutorials</a></li>
<li class="<?php if ($first_part=="resources") {echo "active"; } else {echo "noactive";}?>"><a href="#">Resources</a></li>
<li class="<?php if ($first_part=="library") {echo "active"; } else {echo "noactive";}?>"><a href="#">Library</a></li>
<li class="<?php if ($first_part=="our-projects") {echo "active"; } else {echo "noactive";}?>"><a href="#">Our Projects</a></li>
<li class="<?php if ($first_part=="community") {echo "active"; } else {echo "noactive";}?>"><a href="#">Community</a></li>
</ul>
PHP
<?php
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>
Set active class on current page
You can use $_GET
:
<nav class="menu">
<ul class="menu-ul navigation">
<div class="menu-first">
<li><a href="index.php"><span class="main-title">X-BIKES</span></a></li>
</div>
<div class="menu-second">
<li class="<?php echo $_GET['page'] == "events" ? "active" : ""; ?>" ><a href="index.php?page=events">Events</a></li>
<?php if (empty($_SESSION['user'])): ?>
<li class="<?php echo $_GET['page'] == "login" ? "active" : ""; ?>"><a href="index.php?page=login">Login</a></li>
<li>/</li>
<li class="<?php echo $_GET['page'] == "register" ? "active" : ""; ?>"><a href="index.php?page=register">Register</a></li>
<?php else: ?>
<li class="<?php echo $_GET['page'] == "logout" ? "active" : ""; ?>"><a href="index.php?page=logout">Logout</a></li>
<li class="menu-cart <?php echo $_GET['page'] == "cart" ? "active" : ""; ?>">
<a href="index.php?page=cart">Cart (<?php
if(empty($_SESSION['cart'])) {
echo '0';
}else {
echo sizeof($_SESSION['cart']);
};
?>)</a>
</li>
<?php endif; ?>
</div>
</ul>
</nav>
And then in the CSS, you can change the link colour:
.navigation a {
color: blue;
}
.navigation .active a {
color: red;
}
Active Navigation Bar with PHP?
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><a href="home.php"><b>Bienvenue</b></a></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><a href="livres.php"><b>Livres</b></a></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><a href="bibliotheque.php"><b>Bibliothèque</b></a></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><a href="universite.php"><b>L'université</b></a></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><a href="contact.php"><b>Contact</b></a></li>
</ul>
How to add active class in php pagination
check your currebt page with loop variable then add class according to it.
$pagLink = "<ul class='pagination'>";
$current_page = isset($_GET['page'])?$_GET['page'] : 1; //retrive your current page value here
for ($i=1; $i<=$total_pages; $i++) {
$active_class = "";
if($i==$current_page)
{
$active_class = "active";
}
$pagLink .= "<li class='$active_class'><a href='manage_claims.php?page=".$i."'>".$i."</a><li>";
}
echo $pagLink . "</ul>";
Related Topics
Differencebetween Null and Empty
How to Insert Array of Data into MySQL Using PHP
$Path Environment Variable for Apache2 on MAC
Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out
Get "Content-Type" Header of Request in PHP
Detecting File Upload Size on the Client Side
Make Curl Output Stderr to File (Or String)
How to Consume a Wcf Web Service That Uses Custom Username Validation with a PHP Page
Customize Addresses Fields on Woocommerce My Account and Checkout
Prevent PHP Script from Being Flooded
How to Use Curl and PHP Simple HTML Dom Parser with Object
How to Select Rows in MySQL Starting at a Given Row Number
How to Get MySQL 8 to Run with Laravel