The current page someone is in on my website is highlighted on the navigation menu (the class "active"). I've done this through php, which compares the current page to a string. I'd like to simplify it even further because this string is actually the href value of the menu item.
Is there a way to get the href value of this item via php? That way I could just use that variable instead.
My php code:
<?php
function curPageName()
{
return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}
function activeMenuItem($href)
{
return (curPageName() == $href) ? "class=\"active\"" : "";
}
?>
and the html in the body:
<ul id="navilist">
<li><a href="index.php" <?php echo activeMenuItem('index.php'); ?>>Home</a></li>
<li><a href="resume.php" <?php echo activeMenuItem('resume.php'); ?>>Resume</a></li>
<li><a href="projects.php" <?php echo activeMenuItem('projects.php'); ?>>Projects</a></li>
<li><a href="contact.php" <?php echo activeMenuItem('contact.php'); ?>>Contact</a></li>
</ul>
Build your menu in a loop:
$menu_items = array(
'Home' => 'index.php',
'Resume' => 'resume.php'
);
<ul id="navilist">
<?php foreach ( $menu_items as $title => $href ): ?>
<li><a href="<?php echo $href; ?>" <?php echo activeMenuItem($href); ?>>
<?php echo $title; ?>
</a></li>
<?php endforeach; ?>
</ul>
You Can't get the href value using php code.
in my knowledge - one way to get the href value
You should declare the page name with one one variable like
$home="home.php";
$resume="resume.php";
.
.
.
<ul id="navilist">
<li><a href="<?php echo $home;?>" <?php echo activeMenuItem($home); ?>>Home</a></li>
<li><a href="<?php echo $resume;?>" <?php echo activeMenuItem($resume); ?>>Resume</a></li>
.
.
.
</ul>
I am not sure. Please check it.
Related
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>";
How do you hide specific content if $page = name
For example:
<?php
if ($page=='special'){
echo "<div>hello</div>";
}
?>
The above example will show the div if the $page = special. How do I do the opposite of this, hide a specific div if the $page = something?
Edit:
To be more specific I would like to hide my main navigation when on the $clients page.
Do I wrap the <nav> with PHP or is it possible to hide a specific div if I give it a name, for example. <nav id="clients"> the PHP would be: if $clients then hide the id named clients.
I should also mention that the content in question has <?php echo $url; ?> and the likes contained within it.
This is the exact content I would like to hide on $clients pages.
<nav>
<ul>
<li><a <?php if ($page=="work") echo "class=\"current\"" ?> href="<?php echo $url; ?>" title="Work">Work</a></li>
<li><a <?php if ($page=="profile") echo "class=\"current\"" ?> href="<?php echo $url; ?>profile/" title="Profile">Profile</a></li>
<li><a <?php if ($page=="approach") echo "class=\"current\"" ?> href="<?php echo $url; ?>approach/" title="Approach">Approach</a></li>
<li><a <?php if ($page=="contact") echo "class=\"current\"" ?> href="<?php echo $url; ?>contact/" title="Contact">Contact</a></li>
</ul>
</nav>
hide a specific div if the $page = something?
if ($page !='special'){
echo "<div>hello</div>";
}
You just echo it if $page is different from something.
If you want, you can also echo it anyway but as 'hidden' if that's what you're trying to achieve.
if ($page =='special'){
echo "<div>hello</div>";
} else {
echo "<div style='display:hidden;'>hello</div>";
}
This way, the div will be in the DOM anyway and you can show it later on without reloading the page using JavaScript.
If you want it on every other page that is not special
<?php
if ($page!='special'){
echo "<div>hello</div>";
}
?>
So I'm trying to use a query string to highlight a 'current' menu item.
Say the url is www.....something.php?tag=Music
And I'm looping through this code to check the $tag against a record in the database:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name);
{ echo 'current'; }?>">
<a href="?tag=<?php echo $record->name; ?>">
<?php echo $record->name; ?></a></li>
Why doe's it always come out 'true' and echo 'current'.
The html it outputs is this:
<li class="current">
<a href="?tag=Music">Music</a>
</li>
<li class="current">
<a href="?tag=Film">Film</a>
</li>
<li class="current">
<a href="?tag=biscuits">biscuits</a>
</li>
Surely it should only be 'true' for 'Music'?
You have a semi-colon after your if statement. Remove that and it should work:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name)
For shorter code, and if you have short tags enabled, try:
<li class="<?=isset($_GET['tag'])&&$_GET['tag']==$record->name?'current':''?>">
<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/
Im trying to validate my output data from a php site that Im calling with
<?php include_once('nav.inc.php');
?>
The thing is when Im using this code
<ul>
<li>GÄSTBOK</li>
<li>OM MIG</li>
<li>PORTFOLIO</li>
<li>CURRICULUM VITAE</li>
</ul>
I wont get any errors, But when Im trying with the other code I get alot of errors. Is there any way to write the code so It validates ?
This is the code thas bugging me
<?php
$index = 'menu';
$gb = 'menu';
$portfolio = 'menu';
$cv = 'menu';
$menuLinkid=basename($_SERVER['PHP_SELF'],'.php');
if($menuLinkid=='index'){
$index = 'myButtons';
}else if($menuLinkid=='gb'){
$gb ='myButtons';
}else if($menuLinkid=='portfolio'){
$portfolio ='myButtons';
}else if($menuLinkid=='cv'){
$cv ='myButtons';
}
?>
<div id="fronticon">
<a href="contact.php"><img src="images/em.png" alt="Email" title="Email"/>
</a>
</div>
<ul>
<li><a class="<?php echo $index; ?> "href="index.php">OM MIG</a></li>
<li><a class="<?php echo $gb; ?> "href="gb.php">GÄSTBOK</a></li>
<li><a class="<?php echo $portfolio;?> " href="portfolio.php">PORTFOLIO</a></li>
<li><a class="<?php echo $cv; ?> "href="cv.php">CURRICULUM VITAE</a></li>
</ul>
Thanks
You need spaces between the ?> " and the href. You can't have them next to each other with no space.
Should be:
<li><a class="<?php echo $index; ?> " href="index.php">OM MIG</a></li>