0

Here is my code...

if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level"><a href="/shop/category/handheld-thermal-imaging/">HANDHELD<br />THERMAL IMAGING</a></div>';
} 

else if($_SERVER['REQUEST_URI'] == '/shop/category/mobile-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level"><a href="/shop/category/mobile-imaging/">MOBILE IMAGING</a></div>';
}

Basically this code displays a different left side site navigation depending on which page you're on in the site. It detects what page you're on by the URL of the page by using the PHP REQUEST_URI feature.

My question is this, how can I make the code detect multiple URLs for the same navigation piece?

I tried this code...

if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/' , '/shop/iphone-thermal-imaging-adapter/')
{

But that code doesn't seem to work. Basically all I'm trying to figure out here is how I can use multiple URLs for the REQUEST_URI 'if' statement. Thanks in advance!

4 Answers 4

2

Put the URLs in an array and then... if(in_array($_SERVER['REQUEST_URI'],$array))

Sign up to request clarification or add additional context in comments.

Comments

1

You should... switch to a switch statement, which is both neater and offers this option:

switch($_SERVER['REQUEST_URI']) {
    case '/shop/category/handheld-thermal-imaging/':
        // THERE IS NO CODE AT ALL HERE!
        // The absence of a "break;" causes control to "fall through" to the next
    case '/shop/iphone-thermal-imaging-adapter/':
        echo "something common for the thermal imaging urls";
        break;
    case 'some other url':
        echo "something for the url that stands alone";
        break;
}

1 Comment

Thanks Jon, I went with this method that you suggested and it works out great!
0

You can use an "OR" statement in your conditional:

if($_SERVER['REQUEST_URI'] == "whatever" || $_SERVER['REQUEST_URI'] == 'something else")
{

}

1 Comment

Thanks for the advice. I'm still learning PHP so I wasn't even aware of the 'OR' statement!
0

I used the OR statement for multiple URL checking

<?php 

   if ((strpos($_SERVER['REQUEST_URI'], 'contact.php')  || strpos($_SERVER['REQUEST_URI'], 'register.php') || strpos($_SERVER['REQUEST_URI'], 'login.php')) === false) { 
     echo "show";  
   } else { 
     echo "hide";  
   } 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.