Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URI Dependent Content

Status
Not open for further replies.

Geronantimo

Technical User
Apr 3, 2006
79
SE
I am having difficulty getting this to work...

I have some code that I would like not to display on particular webpages. I have a line of PHP like this:

Code:
<?php if ( $_SERVER['REQUEST_URI'] !== "/" ) { ?>

html content

<?php } ?>

This works inasmuch as the HTML content is not displayed on the home page. I would like to add several other pages for which the HTML content is not displayed but I have not worked out how to combine them.

For example, I would like to add
Code:
<?php if ( $_SERVER['REQUEST_URI'] !== "/checkout/cart/" ) { ?>
Code:
<?php if ( $_SERVER['REQUEST_URI'] !== "/myaccount/" ) { ?>

but I have not succeeded. Can somebody show me what I am missing?
 
Hi

According my experience, your approach is not easy to use and enhance.

I would change the condition : instead of excluding if condition is false, I would include if condition is true. And I would put the separate pieces of HTML in separate files.

Anyway I would store the conditions in an array ( here I implemented your exclusion logic ) :
Code:
[navy]$exclude[/navy][teal]=[/teal][b]array[/b][teal]([/teal]
  [green][i]'content.htm'[/i][/green][teal]=>[/teal][b]array[/b][teal]([/teal][green][i]'/'[/i][/green][teal]),[/teal]
  [green][i]'cart.htm'[/i][/green]   [teal]=>[/teal][b]array[/b][teal]([/teal][green][i]'/checkout/cart/'[/i][/green][teal]),[/teal]
  [green][i]'account.htm'[/i][/green][teal]=>[/teal][b]array[/b][teal]([/teal][green][i]'/myaccount/'[/i][/green][teal])[/teal]
[teal]);[/teal]

[b]foreach[/b] [teal]([/teal][navy]$exclude[/navy] [b]as[/b] [navy]$file[/navy][teal]=>[/teal][navy]$condition[/navy][teal])[/teal]
  [b]if[/b] [teal](![/teal][COLOR=darkgoldenrod]in_array[/color][teal]([/teal][navy]$_SERVER[/navy][teal][[/teal][green][i]'REQUERT_URI'[/i][/green][teal]],[/teal][navy]$condition[/navy][teal]))[/teal]
    [red]include[/red][teal]([/teal][navy]$file[/navy][teal]);[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]

Feherke.
 
Continuing with your approach you need to use a logical OR to combine the conditions


you could try
Code:
<?php
$uri=$_SERVER['REQUEST_URI']
if ($uri!="/" or $uri!=""/Checkout/cart/" ) {
?>
html
<?php } ?>

this approach soon becomes unwieldly

I would suggest you instead check each item & set a flag to dsignify if the info should be shown
if the code to do this is placed at the top of the page the flag can be checked wherever necessary
 
I guess feherke's codes should probably have an exit() statement after the include.

the alternative route (no particular advantage over feherke's) is to use switch statements

Code:
switch (strtolower($_SERVER['REQUEST_URI'])){
    case '/':
    case '/anothergoodpage.php':
    case '/folder/yetanothergoodpage.php'
       include 'somehtmlcontent.html';
       //maybe exit();
       break;
} //end switch
 
Thanks to feherke, IPGuru and jpadie,

I shall test all three approaches and see which works best for my purposes.
 
to give some more code to my suggestion of setting a flag
Code:
<?php
$uri=$_SERVER('REQUEST_URI');
$nodisp=FALSE;

$nodisp=($uri=="/")?TRUE:$nodip;
$nodisp=($uri=="/checkout/cart/")?TRUE:$nodip;
$nodisp=($uri=="/myaccount/")?TRUE:$nodip;
$nodisp=($uri=="<another webpage")?TRUE:$nodip;
?>
main html
<?php
if (!$nodisp) {
?>
optional html
<?php
}
?>

untested but i think the idea is clear


 
of course that should be $nodisp not $nodip in the 4 ternary operators (spot the copy & paste typeo)

Not sure who on here first drew my attention to the ternary operator but it is great for streamlining simple "if then else" constructs for setting variables.
 
@feherke
fair enough.

This whole approach feels wrong, imo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top