Jump to content


Photo

Including Your Navbar? Use Php To Set Class "active"


  • Please log in to reply
16 replies to this topic

#1 evhwanabe

evhwanabe

    New To The Neighborhood

  • Members
  • Pip
  • 22 posts

Posted 11 October 2005 - 02:30 PM

First off, let me start by saying I am not an expert in PHP, but I got this work and thought maybe other people could use it. I ran into a problem on my last project where I was using a php include for my navigation bar, No big deal right? But I also wanted to be able to change the class of my active page/link on each page so the button/link looked like it was pressed down. Look here to see what I mean. This is no sweat if you have your nav code on each page, you would just set the class to "active" or whatever and you would be good to go. I wrote this little script to basically check what page I am on, and then set the class active for that page. I now have a php include navbar that knows what page you are currently on and changes the link class accordingly. I am not going to get into the css side of this. I will save that for another time lol.

<?
if ($PHP_SELF != '/page1.php')
{ $page1 = 'none'; } else { $page1 = 'active'; }
if ($PHP_SELF != '/page2.php')
{ $page2 = 'none'; } else { $page2 = 'active'; }
if ($PHP_SELF != '/page3.php')
{ $page3 = 'none'; } else { $page3 = 'active'; }
if ($PHP_SELF != '/page4.php')
{ $page4 = 'none'; } else { $page4 = 'active'; }
?>
<ul id="nav">
<li><a href="/page1.php" class="<?echo $page1;?>">page1</a></li>
<li><a href="/page2.php" class="<?echo $page2;?>">page2</a></li>
<li><a href="/page3.php" class="<?echo $page3;?>">page3</a></li>
<li><a href="/page4.php" class="<?echo $page4;?>">page4</a></li>
</ul>

Like I said, I am not an expert, so if you know of a better way please share.....

:)

#2 abinidi

abinidi

    Immediate Family

  • Members
  • PipPipPipPip
  • 1,420 posts

Posted 11 October 2005 - 03:03 PM

Cool! That might come in useful for me! I appreciate it.
Paul Pehrson
Herriman, UT

Posted Image Posted ImagePosted ImagePosted ImagePosted Image



#3 owatagal

owatagal

    Immediate Family

  • Members
  • PipPipPipPip
  • 212 posts

Posted 12 October 2005 - 07:36 AM

evhwanabe, I do something very similar with one of my nav menus. The only thing I would suggest is that you could use the ternary operator to tighten up the code. For those who don't know, the ternary operator is a slick shorthand for if-then statements; see for example this tutorial

in your case, the whole code snippet could be shortened to:

<?php
echo '<ul>';

echo ($PHP_SELF == '/page1.php') ? 
'<li><a class="active" href="/page1.php">Page One</a></li>' :
'<li><a class="none" href="/page1.php">Page One</a></li>';

echo ($PHP_SELF == '/page2.php') ? 
'<li><a class="active" href="/page2.php">Page Two</a></li>' :
'<li><a class="none" href="/page2.php">Page Two</a></li>';

echo ($PHP_SELF == '/page3.php') ? 
'<li><a class="active" href="/page3.php">Page Three</a></li>' :
'<li><a class="none" href="/page3.php">Page Three</a></li>';

echo ($PHP_SELF == '/page4.php') ? 
'<li><a class="active" href="/page4.php">Page Four</a></li>' :
'<li><a class="none" href="/page4.php">Page Four</a></li>';

echo '</ul>';
?>

I've formatted it to make it easier to read, but that's really just six lines of code total. And it even includes the commands to print out the <ul> and each individual <li>, so that doesn't have to be a separate part of your code. For me, it's easier to maintain -- if I need to rename page2.php, all the instances of "page2.php" are right in one area of the code.

(Not that I'm a fan of the ternary operator or anything... ok, I am. Just a little bit. But it's such a neat little snippet.)

#4 evhwanabe

evhwanabe

    New To The Neighborhood

  • Members
  • Pip
  • 22 posts

Posted 12 October 2005 - 09:33 AM

evhwanabe, I do something very similar with one of my nav menus. The only thing I would suggest is that you could use the ternary operator to tighten up the code. For those who don't know, the ternary operator is a slick shorthand for if-then statements; see for example this tutorial

in your case, the whole code snippet could be shortened to:

<?php
echo '<ul>';

echo ($PHP_SELF == '/page1.php') ? 
'<li><a class="active" href="/page1.php">Page One</a></li>' :
'<li><a class="none" href="/page1.php">Page One</a></li>';

echo ($PHP_SELF == '/page2.php') ? 
'<li><a class="active" href="/page2.php">Page Two</a></li>' :
'<li><a class="none" href="/page2.php">Page Two</a></li>';

echo ($PHP_SELF == '/page3.php') ? 
'<li><a class="active" href="/page3.php">Page Three</a></li>' :
'<li><a class="none" href="/page3.php">Page Three</a></li>';

echo ($PHP_SELF == '/page4.php') ? 
'<li><a class="active" href="/page4.php">Page Four</a></li>' :
'<li><a class="none" href="/page4.php">Page Four</a></li>';

echo '</ul>';
?>

I've formatted it to make it easier to read, but that's really just six lines of code total. And it even includes the commands to print out the <ul> and each individual <li>, so that doesn't have to be a separate part of your code. For me, it's easier to maintain -- if I need to rename page2.php, all the instances of "page2.php" are right in one area of the code.

(Not that I'm a fan of the ternary operator or anything... ok, I am. Just a little bit. But it's such a neat little snippet.)



Cool owatagal,
Thanks for the tip!

#5 paul100

paul100
  • Members
  • 1 posts

Posted 09 January 2006 - 05:37 AM

Cool owatagal,
Thanks for the tip!



This is just what I've been looking for but I cant get it to work as it's my first go with PHP. Can anyone either tell me which bit goes where please?

Pa :unsure: ul

#6 TCH-Thomas

TCH-Thomas

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 15,181 posts

Posted 09 January 2006 - 05:39 AM

Welcome to the forum, paul100. :unsure:
Thomas Jikrantz
Forum Moderator
TotalChoice Hosting, Inc.

Any links or suggestions for third party software/sites should be used at your own risk. My opinions and recommendations are not necessary those of TCH and TCH is not responsible.

As a Forum Moderator I can assist in answering many of your hosting related questions. However, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.
Web Hosting by Total Choice Web Hosting - 24/7 Help Desk

#7 stevevan

stevevan

    Immediate Family

  • Members
  • PipPipPipPip
  • 3,522 posts

Posted 09 January 2006 - 06:11 AM

Welcome to the forums, paul100!
Steve, W4SJV

"When all else fails (and it will)...there's Amateur Radio!"
"It is better to let people THINK you're a fool than to open your mouth and remove all doubt."

#8 TCH-Bruce

TCH-Bruce

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 20,146 posts

Posted 09 January 2006 - 08:38 AM

Welcome to the forums Paul

Bruce Richards
Forum Moderator
TotalChoice Hosting, Inc.
Webhosting by Total Choice Web Hosting - General Support Forum

I am a Forum Moderator. While I can assist in answering most of your hosting related questions, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.


#9 TCH-Don

TCH-Don

    Immediate Family

  • Members
  • PipPipPipPip
  • 11,642 posts

Posted 09 January 2006 - 08:57 AM

Welcome to the forums Paul :unsure:

I would save it as menu.php
and then include it in place of your current menu.

<?php include $_SERVER['DOCUMENT_ROOT']."/menu.php"; ?>

of course the page has to be a php page for it to work

#10 surefire

surefire

    Immediate Family

  • Members
  • PipPipPipPip
  • 1,008 posts

Posted 09 January 2006 - 09:01 AM

<?php
echo '<ul>';
$navList = array('One','Two','Three','Four');
foreach($navList as $k=>$v)
{
$x = $k+1;
$class = ($PHP_SELF == '/page'.$x.'.php') ? 'active' : 'none';
echo '<li><a class="'.$class.'" href="/page'.$x.'.php">Page '.$v.'</a></li>';
}
echo '</ul>';
?>

It's a little shorter... but more importantly, it's easier to reuse on a new site.

Edited by surefire, 09 January 2006 - 09:03 AM.


#11 TCH-Rob

TCH-Rob

    Help Desk Manager

  • Members
  • PipPipPipPip
  • 7,797 posts

Posted 09 January 2006 - 10:31 AM

Welcome to the forums Paul

#12 TCH-Andy

TCH-Andy

    Immediate Family

  • Members
  • PipPipPipPip
  • 4,699 posts

Posted 09 January 2006 - 01:12 PM

welcome to the forums :)
Andy Beckett
-----------------
Part of the TCH family since the beginnings of time.

#13 theo

theo
  • Members
  • 3 posts

Posted 10 February 2009 - 08:50 AM

Does anyone know how you would do this with two active states? i.e. I'm using genres: so film/action is an example. What I think I need to be able to do is detect the (directory) level higher as well as the current level... So if you're on film/action(/index.php) both film and action would be the active states. Is this going to be possible?

Cheers,
Theo.

Edited by theo, 10 February 2009 - 08:53 AM.


#14 TCH-Bruce

TCH-Bruce

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 20,146 posts

Posted 10 February 2009 - 11:20 AM

Welcome to the forums theo ;)

Yes, it would be possible but I think you would want to use some content management system (CMS) like Joomla or WordPress to do it.

Bruce Richards
Forum Moderator
TotalChoice Hosting, Inc.
Webhosting by Total Choice Web Hosting - General Support Forum

I am a Forum Moderator. While I can assist in answering most of your hosting related questions, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.


#15 TCH-Thomas

TCH-Thomas

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 15,181 posts

Posted 10 February 2009 - 11:30 AM

Welcome to the forum, theo. ;)
Thomas Jikrantz
Forum Moderator
TotalChoice Hosting, Inc.

Any links or suggestions for third party software/sites should be used at your own risk. My opinions and recommendations are not necessary those of TCH and TCH is not responsible.

As a Forum Moderator I can assist in answering many of your hosting related questions. However, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.
Web Hosting by Total Choice Web Hosting - 24/7 Help Desk

#16 theo

theo
  • Members
  • 3 posts

Posted 10 February 2009 - 12:04 PM

Is there a way of extracting (separating out) the information from $PHP_SELF ?

Edited by theo, 10 February 2009 - 12:05 PM.


#17 theo

theo
  • Members
  • 3 posts

Posted 10 February 2009 - 01:51 PM

Is there a way of extracting (separating out) the information from $PHP_SELF ?


Well I've done it using this function:

<?php
function public_base_directory()
{
//get public directory structure eg "/top/second/third"
$public_directory = dirname($_SERVER['PHP_SELF']);
//place each directory into array
$directory_array = explode('/', $public_directory);

return $directory_array[1];
}
?>

It seems to work OK, but will have to see if I run into any problems with it later.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users