tags:

views:

46

answers:

3

I have php condition basically static if the page is "biz-los-angeles.php" show specific html. I would like to add another condition for example "retail-biz-los-angeles.php". What would be the easiest way to do this. Basically i just want to script to pick up "retail" text at the beginning of the url and return specific html. Below is the code to do this but would be too cumbersome to go over each city.

<?php
$currentpage = basename($_SERVER['SCRIPTNAME']);

if ($currentpage == 'retail-biz-los-angeles.php')
    echo 'HTML ';
else if ($currentpage == 'biz-los-angeles.php')
    echo 'HTML';
?>
+1  A: 

You can check something like this:

if (strpos($currentpage, 'retail-') === 0))
    ...

Depending on your server / CMS / whatever you might also consider using mod_rewrite.

RewriteRule ^retail-(.*)$ $1?retail=1 [L,QSA]

This would give you $_GET['retail'] == 1 on the retail pages.

Greg
A: 

you could make it more robust by making a switch statement. I haven't tested the code, but something like the following would make it easier to maintain.

    $currentpage = basename($_SERVER['SCRIPTNAME']);
    $pageParts =  explode('-',$current_page);
    $section = $pageParts[0]; 

    switch($section){
     case 'biz':
      echo 'BIZ';
      break;
     case 'retail':
      echo 'Retail';
      break;
     default:
      echo 'catchall';
      break;
    }
easement
switch($section) -> switch(strtolower($section))
powtac
A: 

i would use mod rewrite ....

$currentpage = basename($_SERVER['SCRIPTNAME']);
$pagetags=explode('-',$currentpage);
if(array_search("retail",$pagetags)!==FALSE){
echo "HTML";
}
n00b32