I’m not too good with php, just the basics. So i’m using php to add the active states to my navigation links and using css after to style them, I was wondering if theres a way to use hashtags. I have two different navigation links that are both on one page. The first one works fine because no # is used but the second since the info for that page is on the same page as the default, I have the following code:
<?php
if (preg_match ("//", $_SERVER['REQUEST_URI'])){ $navID = "home";}
if (preg_match ("/service-area/", $_SERVER['REQUEST_URI'])){ $navID = "service";}
if (preg_match ("/about/", $_SERVER['REQUEST_URI'])){ $navID = "about";}
if (preg_match ("/heating-air/", $_SERVER['REQUEST_URI'])){ $navID = "heating";}
if (preg_match ("/plumbing-hvac/", $_SERVER['REQUEST_URI'])){ $navID = "plumbing";}
?>
Theres the pregmatch and here are the links:
<ul class="right-nav">
<li class="heating-air"><a href="/plumbing-hvac/#heating-air" <?= ($navID == 'heating') ? ' class="active"' : '' ?>>Plumbing</a></li>
<li class="plumbing"><a href="/plumbing-hvac/" <?= ($navID == 'plumbing') ? ' class="active"' : '' ?>>Heating & Air</a></li>
</ul>
I get an error whenever I add the anchor link to the preg_match() section. Is there a way around this?
The part starting with
#in an URL is never transfered to the server, so$_SERVER['REQUEST_URI']won’t include it.So regardless of the error you get, it won’t make any sense to look for it.