Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/
Share on other sites

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>   

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.