How do i create IF/Else statement to change title of site based on page url?

Discussion in 'PHP' started by amedia, Sep 15, 2008.

  1. #1
    I have a site that has a Header file that is included on every page. It has the page title and description, and menu for the page. I want to be able to have it change for a particular page/file.

    Like if i had tech-reviews.php, the title would be "Tech Reviews" and on the help.php file the title would be "Help"

    I am guessing the easiest way to do this would be with an If Else statement?
     
    amedia, Sep 15, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You can add this code in your header file.

    <?php
    
    //add all your file and their title in this array
     $pages = array('Index.php'=>'Home page',
                    'Help.php'=>'Help',
                    'Contact.php'=>'Contact Us'
                    );
    
     foreach($pages as $url => $label){
     
     
       if(strpos($_SERVER['PHP_SELF'],$url) > 0){
       
          $title = $label;
          break;
       
       }//end if
     
     }//end foreach
    
    ?>
    
    <html>
    <head>
    
    <title> <?php echo $title; ?> </title>
    </head>
    
    PHP:
     
    php-lover, Sep 15, 2008 IP
    Hade likes this.
  3. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    $filename = basename($_SERVER['PHP_SELF']);
    if ($filename == 'tech-reviews.php') {
      $title = 'Tech Reviews';
    }
    elseif ($filename == 'help.php') {
      $title = 'Help';
    }
    Etc...
    ?>
    
    PHP:
    And then just echo $title wherever you want the title to be.
     
    Shoro, Sep 15, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Try this code:
    $t= $_SERVER[SCRIPT_NAME];
    $t= str_replace('.php','',$t);
    $t= preg_replace('/[^A-Za-z0-9]/', ' ', $t);
    //$t has your title based on file name
    // you can also use $_SERVER[PHP_SELF]
     
    JEET, Sep 15, 2008 IP
  5. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In each of your file "tech-reviews.php" , "help.php "....

    like in "tech-reviews.php" file add this code:
    $title="Tech Reviews";

    and in "help.php" file add:
    $title="Help";

    after customizing your every page, lets come to HEADER FILE which is included in each of your page.
    customize your HEADER FILE as for example:


    <html>
    <head>
    <title><?php echo $title; ?>
    </head>
    </html>

    -----
    It will print text which is currently present in the $title Variable, for example during HELP.php file the $title variable will contain the text "HELP" and during the tech-reviews.php file, the $title variable should have "Tech Reviews" in its variable.


    Thanks.
     
    mehdi, Sep 16, 2008 IP
  6. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #6
    hmmmm thanks, i was also searching for a code like this. [:)]
     
    hassanahmad1, Sep 16, 2008 IP
  7. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #7
    Nice bit of code. I'll steal that for my site if I may :D
     
    Hade, Sep 16, 2008 IP