highlight currently menu selection php - php

I have PHP code which will check if the user is logged in and will return the menu if they are, however I was wondering if there was a way to make each of the current selected highlighted or would I have to go through and add them as a manual list to each page?
The code is:
<?php
if (!securePage($_SERVER['PHP_SELF'])){die();}
//Links for logged in user
if(isUserLoggedIn()) {
echo "<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>
<div id='button1'>
<a href='/Demos.php'>Demos</a></div>
<div id='button2'>
<a href='/Helpfiles.php'>Helpfiles</a></div>
<div id='greeting'>
Hello, $loggedInUser->displayname.</br>";
}
//Links for users not logged in
else{
echo "<div id='Default'>
<ul>
<li><a href='/login.php'>Login</a></li>
<li><a href='/register.php'>Register</a></li>
<li><a href='/forgot-password.php'>Forgot Password</a></li>";
echo "</ul></div>";
}
?>
Now I know that on a normal CSS one it would just be .current and you can do it that way, however I cannot make that work with this echo because they are all on the screen at the same time. What would be the best way? Manually add would see like the longer way.
p.s. this is used in conjunction with usercake

You don't need to put HTML into an echo in PHP. I would recommend something like this.
So you will end up with something like:
<?php
if(isUserLoggedIn()) {
?>
<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul>
</div>
<div id='greeting'>
Hello, <?php echo $loggedInUser->displayname; ?>
</br>
<?php } ?>
Then I would not recommend you to add the class with PHP because you will suffer from lisibility with a lots of if and else cases.
The best way to do this would be to use ID/Classes for your LIs and add the selected class to a specitic item with a simple JavaScript function.
Btw, if you really feel the needs to have this in PHP I recommend you to read this:
http://www.catswhocode.com/blog/snippets/highlight-current-menu-item-in-php
http://webdeveloperswall.com/php/how-to-highlight-the-current-page-in-menu-in-php
So you will have something like:
<?php if(isUserLoggedIn()) { ?>
<ul>
<?php
$url = $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$page_name = basename($parts['path']);
?>
<li><a class="<?php echo ($page_name=='acount.php')?'selected':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='user_settings.php')?'selected':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='logout.php')?'selected':'';?>" href="contact.php">CONTACT US</a></li>
</ul>
<?php } ?>
EDIT
Finally, you should end up with something like this: http://pastebin.com/V8jxwi7T

You could grab the page you are currently browsing and see if it matches. Something like this perhaps? (sorry for the crude example)
<?php
// will return 'home' is the filename is home.php
$filename = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
?>
<li <?php if ($filename == "home") { echo "class='active'"; } ?>>
Home
</li>
Some info on pathinfo and how it works here

Just add class to selected <li> element, this way:
echo "<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li class="selected"><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";
In your CSS, put appropriate style for .selected class.
You should have a variable telling in which page you are, let's say it's $currentpage.
echo "<div id='Default'>
<ul>
<li".($currentpage=='account' ? ' class="selected"' : '')."><a href='/account.php' >Account Home</a></li>
<li".($currentpage=='usersettings' ? ' class="selected"' : '')."><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";

Related

Bootstrap multi-level drop down not working

I am working on a PHP website. And for some reasons, I need to add multiple level dropdown in my navigation bar. I tried the bootstrap class="dropdown-submenu" but I don't know why it has not worked.
<ul class="dropdown-menu">
<?php
$alllinks = mysql_query("SELECT `cid`, `cname` FROM `services` WHERE `parentid`=0");
while($reslink = mysql_fetch_assoc($alllinks)){ ?>
<li class="dropdown-submenu">
<a tabindex="-1" href="<?php echo MYWEBSITE;?>services/<?php echo to_prety_url($reslink['cname']).'-'.$reslink['cid'];?>.html">
<?php echo $reslink['cname'];?>
</a>
<ul class="dropdown-menu">
<li>
<a href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<?php echo $rowsb['cname'];?>
</a>
</li><br>
</ul>
</li><br>
<?php } ?>
</li>
</ul>
Throughout your code You haven't enclosed MYWEBSITE in inverted comma's
you have written.
<?php echo MYWEBSITE;?>
whereas you should write
<?php echo "MYWEBSITE";?>
and You are using the old and Deprecated mysql connector it is higly recommended that you switch to PDO or mysqli.

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 do I apply a specific class to a navigation link on a page ?

I have a navigation menu in my includes file, but I am having trouble echoing out the menu across my site. If I try and place the code as so, the page renders out mark up where the nav menu should be.
The code works somewhat if I do not add the <?php function before the menu.
I'm wondering how do I echo this code out within a function. I've tried changing the single quotes to double, but I think I am getting confused by the echo that is within the li in the menu.
A solution would be great.
Thanks
<?php
function nav() {
echo
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
}?>
The simplest is not to use the initial echo. Notice the ?> after the function name and <?php added again at the bottom for the ending function curly brace.
<?php
function nav() {
?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
<?php
}?>
Replace with this:
<?php
function nav() { ?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
You dont need echo for the showing the html just close the php tag before the html
Hope this will help!!
<?php
function nav() {
?>
<nav class='container'>
<ul>
<?php if(strpos($_SERVER['PHP_SELF'],'current-journal' )) {echo "<li><a class='current' href='current-journal'>CURRENT JOURNAL<span class='sub-nav'>Our latest and greatest!</span></a></li>"; }?>
<?php if(strpos($_SERVER['PHP_SELF'],'submit' )) {echo "<li> <a class='current' href='submit'>SUBMIT<span class='sub-nav'>Your writing</span></a></li>"}?>
<?php if(strpos($_SERVER['PHP_SELF'],'submission-guidelines' )) {echo "<li> <a class='current' href="submission-guidelines'>SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'contributors' )) {echo "<li> <a class='current' href='contributors'>CONTRIBUTORS<span class='sub-nav'>See who\'s in our magazine\'s latest issue</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'past-journals' )) {echo "<li> <a class='current' href='past-journals'>PAST JOURNALS<span class='sub-nav'>Browse our issue archives</span></a></li> ";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'blog' )) {echo "<li> <a class='current' href='blog'>BLOG<span class='sub-nav'>Just a blog</span></a></li>";}?>
</ul>
</nav>
<?php
}
?>
This should do the work.

Highlight active menu link in php

I have made a navigation in php, which works perfect, well, almost perfect. I have 6 pages site and button of active page is highlighted with a different color. In 4 pages it works, but for "gold going green" and "About Ecuador" link remains not highlighted. Here is the code I used:
<?php
//initialize the page variables here so no errors occur in some server environments
$index="myButtons";
$about="myButtons";
$gold="myButtons";
$ecuador="myButtons";
$contact="myButtons";
$documents="myButtons";
//this line gets the file name without the dot and extension
$menuLinkid=basename($_SERVER['PHP_SELF'], ".php");
if($menuLinkid=="index"){
$index='myActiveButton';
} else if($menuLinkid=="about"){
$about='myActiveButton';
} else if($menuLinkid=="gold"){
$gold='myActiveButton';
} else if($menuLinkid=="ecuador"){
$ecuador='myActiveLink';
} else if($menuLinkid=="contact"){
$contact='myActiveButton';
} else if($menuLinkid=="documents"){
$documents='myActiveButton';
}
?>
<div id="header">
<div id="innerheader">
<h1 id="logo"><img src="img/logo.gif" /></h1>
<nav id="navigation">
<ul class="menu">
<li><a class="<?php echo $index; ?>" href="../index.php">Home</a></li>
<li>/</li>
<li><a class="<?php echo $about; ?>" href="../about.php">About</a></li>
<li>/</li>
<li><a class="<?php echo $gold; ?>" href="../gold-going-green.php">Gold going green</a> </li>
<li>/</li>
<li><a class="<?php echo $ecuador; ?>" href="../about-ecuador.php">About Ecuador</a></li>
<li>/</li>
<li><a class="<?php echo $contact; ?>" href="../contact.php">Contact</a></li>
<li>/</li>
<li><a class="<?php echo $documents; ?>" href="../documents.php">Documents</a></li>
</ul>
</nav>
</div><!-- /innerheader -->
</div><!-- /header -->
You set everthing to 'myActiveButton', and use myActiveLink for ecuador:
$ecuador='myActiveLink';
Try this:
$ecuador='myActiveButton';
Change,
else if($menuLinkid=="gold"){
$gold='myActiveButton';
to
else if($menuLinkid=="gold-going-green"){
$gold='myActiveButton';
& Change
$ecuador='myActiveLink';
to
$ecuador='myActiveButton';
and recheck.

add active class to menu links using php

<div class="menu clearfix">
<ul>
<li>start</li>
<li>rating</li>
<li>upload</li>
</ul>
Was a while since i used php. Is there any smart way to do a foreach in php and render this menu + an "active" class to the clicked link. So if the active page is "rating", the html would render:
<div class="menu clearfix">
<ul>
<li>start</li>
<li>rating</li>
<li>upload</li>
</ul>
Thanks
Assuming the $_GET value of p would be rating (or any other link in the menu for that matter), one could do something like this:
<?php
echo "<div class=\"menu clearfix\">";
echo "<ul>";
$links = array('rating', 'upload', 'about');
foreach ($links as $link) {
$active = "";
if (!empty($_GET['p']) && $link == $_GET['p']){
$active = 'class="active"';
}
echo "<li><a href=\"./?p=$link\" $active>$link</a></li>";
}
echo "</ul></div>"
?>
As far as I understand you want to know which li is active after request.
If it is - you have to get $_GET parameter smth like $_GET['p'].
And do rendering, smth like:
foreach($ul as $li)
{
if ($_GET['p'] == $li->code)
echo 'class="active"';
}
For example:
<div class="menu clearfix">
<ul>
<?php foreach($ul as $li): ?>
<li><a href="<?php echo $li->url;?>" <?php echo $_GET['p']==$li->get ? class="active" : ''?>><?php echo $li->name;?></a></li>
<?php endforeach; ?>
</ul>
<ul class="sub-nav" >
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<li><a class="<?php echo ($page_name=='where-to-buy.php')?'active':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='about.php')?'active':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='contact.php')?'active':'';?>" href="contact.php">CONTACT US</a></li>
Please follow below URL to live demo...
https://webdesignerhut.com/active-class-navigation-menu/

Categories