PHP question
- Started
- Last post
- 6 Responses
- virtu
Hello, let me see if i can make my question clear. I am a super novice PHP user, so bare with me.
I am wanting to write a navigation that will sense what page a user is on, and give a selected state to the nav button that corresponds with their page.
I am imagining that I can do this with an if else statement in PHP. Now, my question is...how would this be written. I poked around and found something to this extent:
if ($url="index.html"){do something}
else {do something else}would this be a correct approach? or does anyone know of a better way of doing the example listed above? If i need to clarify, please let me know.
thank you!
- heavyt0
2 ways to do it:
1- PHP
2- Javascriptpersonally, i'd go javascript just to take the work off of the server. Both methods follow the same logic; interpret the URL as a string, and create the active state based on teh results.
in PHP you use PHP_SELF to get the page name, in Javacript you use window.location.toString() and an indexOf();
TR1
- virtu0
what would be an example of the javascript way? what would be an example if else statement? I am even less knowledgable of javascript than i am php
- prophet0NE0
in php, you can use the switch statement: http://alphafilter.com/?inc=arti…
- fitsum0
heavyt is right, using javascript and avoiding using server resources will be faster
using "$_SERVER['REQUEST_URI']" will work as well as "$_SERVER['PHP_SELF']"
- heavyt0
i am using an else if because there arent many options, you can make it a switch if you so desire.
TR1
- asprin0
I'm currently doing this on a couple of sites and am using PHP...not much task for the server as this is one of the very few things that I'm doing with php.
here's where I picked it up:
http://www.alistapart.com/articl…basically you declare at the top of each page the name for the page...
$thispage='home' etc.then in the nav you write the statement after each anchor saying if $thispage=home echo 'class=on... etc.
good luck