kwek-kwek
kwek-kwek

Reputation: 1343

PHP if else Request URI help

I tried doing this but then it is not working

 <?php

if ($_SERVER['SERVER_NAME']=='http://www.testground.idghosting.com/idi' && $_SERVER['REQUEST_URI'] == 'our-production/') {

         echo '<div id="services">
<h1>Our services</h1>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_productions" title="Our Productions"><span>Our Productions</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_services" title="Production Services"><span>Production Services</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_equipment" title="Equipment &amp; Facilities"><span>Equipment &amp; Facilities</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_pr" title="PR &amp; Media"><span>PR &amp; Media</span></a>

</div>';
     } else {
         echo '<div> do not show</div>';
     } ;
 ?>

to see the sample click here see the block where it says Our services in the bottom I don't want it to be shown on ths page but visible to all other pages....

Upvotes: 0

Views: 9861

Answers (9)

user2164884
user2164884

Reputation:

try this and it is working for me ...

$data['test']=basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);

if($data['test'] == 'our-production')
{
 echo '<div id="services">
<h1>Our services</h1>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_productions" title="Our Productions"><span>Our Productions</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_services" title="Production Services"><span>Production Services</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_equipment" title="Equipment &amp; Facilities"><span>Equipment &amp; Facilities</span></a>
<a href="<?php bloginfo(\'url\'); ?>" id="serv_pr" title="PR &amp; Media"><span>PR &amp; Media</span></a>

</div>';
 } else {
 echo '<div> do not show</div>';
 } ;

}

Upvotes: 0

Petruza
Petruza

Reputation: 12276

The problem here is that in the echo string, you open a php tag, but that is already php code, so you don't have to open that tag. You'd have to one one if you were in HTML code.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655319

You’re using the wrong values.

// REQUEST_URI is the requested URI path plus the requested URI query, so let’s strip the latter
$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
// HTTP_HOST may not be set if the request didn’t contain the Host header field (just HTTP/1.0)
if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']=='www.testground.idghosting.com' && $_SERVER['REQUEST_URI_PATH'] == '/idi/our-production/') {
    // host is "www.testground.idghosting.com" and requested URI path is "/idi/our-production/"
}

See the manual for what values the $_SERVER contains.

Upvotes: 2

PTBNL
PTBNL

Reputation: 6122

At a higher level, have you considered changing your approach for getting this onto the pages where you want it? You could put the section within the then part of your code into a separate file which you include into those files in which you want it to appear.

Upvotes: 0

Kuroki Kaze
Kuroki Kaze

Reputation: 8481

Always indent your code — it's simplier to see errors

 <?php
     if ($url == "http://www.sample.com/test.php") {
         echo "<div>whatever</div>";
     } else {
         echo "<div> do not show</div>";
     };
 ?>

Note the placement of curly brackets.

Upvotes: 10

Sampson
Sampson

Reputation: 268364

Or you could use the ternary operator. All on one line if you like - I broke it up to avoid the evil scrollbars.

echo ($url == "http://www.sample.com/test.php") 
       ? "<div>Whatever</div>" 
       : "";

Upvotes: 3

Synetech
Synetech

Reputation: 9925

There does not currently seem to be a direct way. What you’ll have to do is to reconstruct the URL by combining entries from the $_SERVER array. You can check the results of the phpinfo() to see what entries you need.

Upvotes: 0

Edward Dale
Edward Dale

Reputation: 30133

<?php

if ($_SERVER['SERVER_NAME']=='www.sample.com' && $_SERVER['REQUEST_URI'] == '/test.php') {
  echo 'blah';
} else {
  echo 'asdf';
}

?>

Upvotes: 2

user140125
user140125

Reputation: 143

<?php
    if($url == "http://www.sample.com/test.php")
    {
        echo "<div>whatever</div>";
    }
    else
    {
        echo "<div> do not show</div>";
    }
?>

Your syntax was just a little wrong.

Upvotes: 0

Related Questions