How to change active menu item style in html/css/js/php - php

heres how my html is
<ul class="navigation">
<li>Home</li>
<li>About us</li>
</ul>
as you can see the content of "about us" page is loaded in to index.php.
now i want to change active page link on nav bar different style.
please do help me. i hope this question is clear.

to add a class to a particular item based on selection;
<ul class="navigation">
<li <?php if(!isset($_GET['content'])) { echo 'class="active"'; } ?>>Home</li>
<li <?php if(isset($_GET['content']) && $_GET['content']=='about') { echo 'class="active"'; } ?>>About us</li>
</ul>

Related

highlight current page in custome nav bar wordpress

i am trying to highlight the page i am in this is what i have so far
<ul class="navigation list-unstyled components">
<li class="active">
Home
</li>
<li>
Contacts
</li>
<li>
aboutus
</li>
<li>
aboutyou
</li>
</ul>
Here is the only jquery I have tried so far, but it doesn't seem to work:
$(".navigation a").on("click", function () {
$(".navigation ").find(".active").removeClass("active");
$(this).parent().addClass("active");
});
If you're building a standard WordPress website, then your pages will reload when you click a navigation link, so even if you set the class with jQuery it will be reset on page load.
What you should do is to set the class of the navigation elements in PHP.
This code is just a mockup, please adjust it to fit your needs:
$home = is_front_page();
# Checks if it's the site front page
$contacts = is_page('contacts');
# Checks if the current page is a page called 'contacts'
$aboutus = is_page('aboutus');
# Checks if the current page is a page called 'aboutus'
$aboutyou = is_page('aboutyou');
# Checks if the current page is a page called 'aboutyou'
<ul class="navigation list-unstyled components">
<li <?php if ($home) {echo 'class="active"';} ?>>
Home
</li>
<li <?php if ($contacts) {echo 'class="active"';} ?>>
Contacts
</li>
<li <?php if ($aboutus) {echo 'class="active"';} ?>>
aboutus
</li>
<li <?php if ($aboutyou) {echo 'class="active"';} ?>>
aboutyou
</li>
</ul>

Active Navigation Bar with PHP?

I'm trying to make an active navigation bar using PHP where the current page will be highlighted or colored in the navigation bar. The code I used:
<ul class="topmenu">
<li <?php if($_GET['page']="home") { ?> class="active" <?php } ?>>
<b>Bienvenue</b>
</li>
<li <?php if($_GET['page']="livres") { ?> class="active" <?php } ?>>
<b>Livres</b>
</li>
<li <?php if($_GET['page']="bibliotheque") { ?> class="active" <?php } ?>>
<b>Bibliothèque</b>
</li>
<li <?php if($_GET['page']="universite") { ?> class="active" <?php } ?>>
<b>L'université</b>
</li>
<li <?php if($_GET['page']="contact") { ?> class="active" <?php } ?>>
<b>Contact</b>
</li>
</ul>
The code will put the attribut in the class active if the page is the current page in the navigation bar. However, all the attributs will be given the class active in this case. How do I fix this?
P.S: I am not looking for any JS or jQuery alternatives, I'm trying to make this work with PHP only.
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><b>Bienvenue</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><b>Livres</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><b>Bibliothèque</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><b>L'université</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><b>Contact</b></li>
</ul>
I don't use PHP, but give the current page the active tag, and change it per file. Then a single class definition in the CSS handles changing the color for each page.
HTML:
<nav>
<a class="active" href="/home/">Home</a>
Calendar
Community
</nav>
CSS:
.active {
background-color: #4CAF50;
}
Declare page variables at the very top of each individual page.
Example: <? $page="home";?>
For each list item in your nav bar add if statment with corresponding variable and active class.
Example: <li class="<?if($page=="home"){?>active<?}?>"><b>Bienvenue</b></li>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I'm using bootstrap 4 class. I've achieved Active Class selection something like this.
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile1.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile1.php">Home
</a>
</li>
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile2.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile2.php">Home
</a>
</li>
Repeat this logic for further li tags. Hope this helps someone else.
The code work correctly if you use "==" instead of "=" in The if construct
Еxample:
if($_GET['page'] **==** "home")...
Instead:
if($_GET['page'] **=** "home")...
Hope this helps someone else...

How to activate "active" class to selected menu item in header.php file

I have few menu items in header.php file.
and calling "header.php" file in few pages like About us, Contact Us....
How can i highlight the selected menu item using php?
<ul>
<li><?php echo get_string('home'); ?></li>
<li><?php echo get_string('courses'); ?></li>
<li>About Us</li>
<li>Blog</li>
<li>Gallery</li>
<li>Contact Us</li></ul>
This is my header.php
Get the page URL from the URL as below -
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//get components of url
$parts = parse_url($url);
//echo $parts['path'];
?>
and then add the condition to your HTML element as
<li <?php if($parts['path']=="/course/index.php") { echo "class='active'";} ?>><?php echo get_string('courses'); ?></li>
<li <?php if($parts['path']=="/about.php") { echo "class='active'";} ?>>About Us</li>
<li <?php if($parts['path']=="/contact.php") { echo "class='active'";} ?>>Contact Us</li>
Change the page name and URL accordingly

PHP easier way to hide/show menu items to logged in / logged out users

Is there an easier more efficient way to hide/show menu items to logged in, logged out users? It seems like I should not have to copy the whole menu again with duplicate menu items. The menu items maybe in different order like below
You can see in my example below I have added to links in the <?php } else { ?> statement
<?php
if($_SESSION["loggedIn"] == "yes") {
?>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
</ul>
<?php } else { ?>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<li>My Account</li>//logged in item
<li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
<li>My Favorite Stores</li>//logged in item
</ul>
<?php
}
?>
Updated for new ordering of items:
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<?php
if($_SESSION["loggedIn"] == "yes") {
echo '<li>My Account</li>';
}
?><li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
<?php
if($_SESSION["loggedIn"] == "yes") {
echo '<li>My Favorite Stores</li>';
}
?>
</ul>
Rather than cluttering up the code, you can place the html into multiple PHP files that are loaded in dependant on the logged in user...
switch($_SESSION['userLevel']) {
case "guest": //Not logged in
require_once('guestnav.php');
break;
case "user": //regular user
require_once('usernav.php');
break;
case "admin": //admin nav
require_once('adminnav.php');
break;
//etc and default nav below
}
then in the nav php files just put the HTML with no PHP and it will be loaded in when the proper criteria is met. makes managing multiple navigation menu's easy.

Connect submenu to sql and pagination in php

I am trying to make my dynamic site in php by learning from tutorials and google search. I am stuck in one place.
I want sub-menu from the database i.e SQL. so when user add category in category section in sql then sub should come from the entered categories but it appears empty . Here is the code
<ul class="menu">
<li>Home</li>
<li>About Us</li>
<li>Products
<ul class="submenu pop">
<?php
$result = mysqli_query ($con,"SELECT CategoryId,CategoryName FROM category ORDER BY CategoryId ASC");
while ($row=mysqli_fetch_array($result))
?>
<li><a href='#'><?php echo $row['CategoryName'];?></a></li>
</ul>
</li>
<li>Directors</li>
<li>Contact Us</li>
</ul>
Also please tell me any easiest way for pagination.
try your code like this :-
<ul class="menu">
<li>Home</li>
<li>About Us</li>
<li>Products
<ul class="submenu pop">
<?php
$result = mysqli_query ($con,"SELECT CategoryId,CategoryName FROM category ORDER BY CategoryId ASC");
while ($row=mysqli_fetch_array($result)){
?>
<!-- check edit in line Below -->
<li><a href='<?php echo trim($row['CategoryName']).'php'; ?>'><?php echo $row['CategoryName'];?></a></li>
<?php } ?>
</ul>
</li>
<li>Directors</li>
<li>Contact Us</li>
</ul>
I think you do not have the open and close curly brace in your while loop { }
For available plugin refer to this http://www.jquery4u.com/plugins/10-jquery-pagination-plugins/

Categories