barrycorrigan Posted April 7, 2011 Share Posted April 7, 2011 Hi Everyone, I have this small bit of PHP that when you are on a certain page it will add a class called selected <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?> <ul id="subnav"> <li><a href="gallery.php?category=Wedding" id="wedding" <?php if ($currentPage == 'gallery.php?category=Wedding') {echo 'class="selected"';} ?>>Wedding Hair</a></li> <li><a href="gallery.php?category=Men" id="mens-styles" <?php if ($currentPage == 'gallery.php?category=Men') {echo 'class="selected"';} ?>>Mens Styles</a></li> <li><a href="gallery.php?category=Women" id="womens-styles" <?php if ($currentPage == 'gallery.php?category=Women') {echo 'class="selected"';} ?>>Women's Styles</a></li> <li><a href="gallery.php?category=Salon" id="salon" <?php if ($currentPage == 'gallery.php?category=Salon') {echo 'class="selected"';} ?>>Salon</a></li> </ul> This is not working I think it's because of the file extenstion 'gallery.php?category=Wedding' is there any way I can get this to work with these extensions Any help would be greatly appreciated Thanks Barry Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/ Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 php self would work, but that wil stop working at some point if the querystring changes, why not do a substring check for the filename , thus avoiding the querystring. <li><a href="gallery.php?category=Wedding" id="wedding" <?php if (substr($currentPage ,'gallery.php')) {echo 'class="selected"';} ?>>Wedding Hair</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198173 Share on other sites More sharing options...
KevinM1 Posted April 7, 2011 Share Posted April 7, 2011 Why not simply use $_GET['category']? Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198175 Share on other sites More sharing options...
barrycorrigan Posted April 7, 2011 Author Share Posted April 7, 2011 Hi spiderwell, I tried your way and it works but it adds the selected class on all the links how does it work where it only shows on the current page. Hi Nightslyr, I also had a go doing it your way. I done this: <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?> <ul id="subnav"> <li><a href="gallery.php?category=Wedding" id="wedding" <?php if ($currentPage == 'gallery.php$_GET['category']') {echo 'class="selected"';} ?>>Wedding Hair</a></li> <li><a href="gallery.php?category=Men" id="mens-styles" <?php if ($currentPage == 'gallery.php$_GET['category']') {echo 'class="selected"';} ?>>Mens Styles</a></li> <li><a href="gallery.php?category=Women" id="womens-styles" <?php if ($currentPage == 'gallery.php$_GET['category']') {echo 'class="selected"';} ?>>Women's Styles</a></li> <li><a href="gallery.php?category=Salon" id="salon" <?php if ($currentPage == 'gallery.php$_GET['category']') {echo 'class="selected"';} ?>>Salon</a></li> </ul> But this way give me an error. Maybe I done this wrong Thanks Barry Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198187 Share on other sites More sharing options...
KevinM1 Posted April 7, 2011 Share Posted April 7, 2011 Yeah, you did it wrong You simply need: $current = $_GET['category']; ?> <ul id="subnav"> <li><a href="gallery.php?category=Wedding" id="wedding" <?php if ($current == 'Wedding' || $current == 'wedding') {echo 'class="selected"';} ?>>Wedding Hair</a></li> <li><a href="gallery.php?category=Men" id="mens-styles" <?php if ($current == 'Men' || $current == 'men') {echo 'class="selected"';} ?>>Mens Styles</a></li> <li><a href="gallery.php?category=Women" id="womens-styles" <?php if ($current == 'Women' || $current == 'women') {echo 'class="selected"';} ?>>Women's Styles</a></li> <li><a href="gallery.php?category=Salon" id="salon" <?php if ($current == 'Salon' || $current == 'salon') {echo 'class="selected"';} ?>>Salon</a></li> </ul> You should really brush up on how to handle the superglobal arrays $_GET and $_POST, as you'll be dealing with them all the time if you stick with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198193 Share on other sites More sharing options...
barrycorrigan Posted April 7, 2011 Author Share Posted April 7, 2011 Thanks for the advice Nightslyr, That worked great! thanks for the help Barry Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198202 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 Hi spiderwell, I tried your way and it works but it adds the selected class on all the links how does it work where it only shows on the current page. yeah i didnt look closely enough and if the script isnt changing the category is which is why my code didnt work Quote Link to comment https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/#findComment-1198319 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.