chimpanzee Posted May 15, 2007 Share Posted May 15, 2007 I don't even know if this is possible or how complicated it would be but I would like to use php to define the class of a div depending on the filename... I have a consistent navigation bar in my website which is inside an include and therefore rendered in the same way on every page. Each button in the nav bar is a div: <div class="navItem"><a href="index.php">Introduction</a></div> Is it possible to add some PHP to say that if the current file is "index.php" then class="navItemActive" else class="navItem"? (or something along those lines ) Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/ Share on other sites More sharing options...
Silverado_NL Posted May 15, 2007 Share Posted May 15, 2007 hi. i think you could do it like this. if(strrpos($_SERVER["PHP_SELF"], "index.php")) { //css for index }else{ //other css } Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253426 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 hi. i think you could do it like this. if(strrpos($_SERVER["PHP_SELF"], "index.php")) { //css for index }else{ //other css } Thanks. Would that be used within the div tag like so: <div if(strrpos($_SERVER["PHP_SELF"], "index.php")) { class="navItem" }else{ class="navItemActive" }><a href="index.php">Introduction</a></div> Sorry, I'm a newb at PHP ??? Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253432 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 <div class=" <?php if(strrpos($_SERVER["PHP_SELF"], "index.php")) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="index.php">Introduction</a></div> Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253445 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Hi. Thanks for your help. I tried that and it returned some strange results. The page is at http://www.bareonline.co.uk/site2/index.php . Could you take a look. I've done this for each div (and obviously changed the bits surrounded by !'s accordingly(and obviously not included the !'s in the actual code )): <div class=" <?php if(strrpos($_SERVER["PHP_SELF"], "!index.php!")) { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="!index.php!">!Introduction!</a></div> Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253471 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 <div class=" <?php if($_SERVER['PHP_SELF'] == '/index.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="index.php">Introduction</a></div> try that.. note the forward slash / before the file name. I believe that would be needed Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253477 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 nope, still no luck. Now all of them display as navItemActive (apart from Forum because i haven't used it on that one) Any other ideas? Thanks. EDIT: Sorry, just noticed i made a mistake. I'll try it again..... Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253481 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Oh dear: http://www.bareonline.co.uk/site2/index.php ! Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253485 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 could you post the code you're using for the entire div section please? Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253486 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Sure: <table style="width: 100%; padding: 2px; margin:0px"> <tr> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/index.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="index.php">Introduction</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/about_bare.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="about_bare.php">About BARE</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/features.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="features.php">Features</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/news.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="news.php">News</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/who_are_we.php')) { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="who_are_we.php">Who Are We?</a></div></td> <td><div class="navItem"><a href="forum.htm">Forum</a></div></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253490 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 ah, my fault.. I left an extra ) at the end of the if().. try this: <table style="width: 100%; padding: 2px; margin:0px"> <tr> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/index.php') { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="index.php">Introduction</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/about_bare.php') { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="about_bare.php">About BARE</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/features.php') { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="features.php">Features</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/news.php') { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="news.php">News</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/who_are_we.php') { echo 'navItem'; }else{ echo 'navItemActive'; } ?> "><a href="who_are_we.php">Who Are We?</a></div></td> <td><div class="navItem"><a href="forum.htm">Forum</a></div></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253492 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 also as a side note, you should have error reporting turned on. Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253494 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Well, the page is being displayed now but in the same way it was before (all display as navItemActive)? Thanks for your patience! Any more ideas? Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253496 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 oops.. just realised that the echo statements were the wrong way round.. <table style="width: 100%; padding: 2px; margin:0px"> <tr> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/index.php') { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="index.php">Introduction</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/about_bare.php') { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="about_bare.php">About BARE</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/features.php') { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="features.php">Features</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/news.php') { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="news.php">News</a></div></td> <td><div class=" <?php if($_SERVER['PHP_SELF'] == '/who_are_we.php') { echo 'navItemActive'; }else{ echo 'navItem'; } ?> "><a href="who_are_we.php">Who Are We?</a></div></td> <td><div class="navItem"><a href="forum.htm">Forum</a></div></td> </tr> </table> if that doesn't work could you echo $_SERVER['PHP_SELF'] and see what it outputs Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253498 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Swapping the echos didn't work. They all display as navItem now. Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253503 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Ah ha. it returns /site2/features.php so i assume i need to include the directory in the if statement aswell. Is there a way of making it relative? Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253506 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 Okay, I was looking at your site and realised that you'd tried echoing the $_SERVER['PHP_SELF'] and it actually echoed the text $_SERVER['PHP_SELF']. This is because you were trying to echo a variable and I presume your code was something like this: echo "$_SERVER['PHP_SELF']"; when echoing variable you don't use ", so it would be: echo $_SERVER['PHP_SELF']; Also I think I've realised what the problem is. The code is fine, but you are using it on a sub folder in your directory, so you need to use if($_SERVER['PHP_SELF'] == '/site2/index.php') for the testing purposes. I presume you're just testing the files in this directory whilst working on it, then once finished they'll be moved to the root directory replacing the other files? If so, you'll need to then go back to that part after testing and remove all the /site2 parts. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253507 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 I don't think you can make it relative using $_SERVER['PHP_SELF'] Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253525 Share on other sites More sharing options...
chimpanzee Posted May 15, 2007 Author Share Posted May 15, 2007 Sorted! Thanks very much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253535 Share on other sites More sharing options...
Dragen Posted May 15, 2007 Share Posted May 15, 2007 glad it's working Quote Link to comment https://forums.phpfreaks.com/topic/51468-solved-conditional-css-using-php/#findComment-253537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.