1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple PHP Question.

Discussion in 'PHP' started by INV, Sep 15, 2005.

  1. #1
    INV, Sep 15, 2005 IP
    SEMrush
  2. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure why you need this ;), but here goes, easy as pie:

    
    switch ($_SERVER['HTTP_HOST']) {
      case 'website.com':
        $title = ($_SERVER['REQUEST_URI'] == '/index.php' ? 'URL is website.com/index.php' : 'URL is website.com');
        break;
      case 'www.website.com':
        $title = ($_SERVER['REQUEST_URI'] == '/index.php' ? 'URL is www.website.com/index.php' : 'URL is www.website.com');
        break;
      default:
        $title = 'Nothing else matched';
    }
    
    echo '<title>' . $title . '</title>';
    
    PHP:
     
    Willy, Sep 15, 2005 IP
  3. INV

    INV Peon

    Messages:
    1,686
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not quite working as planned :( When the url is www.website.com/folder/ it still displays
    the case 'www.website.com': stuff. Rather then the variable I assigned to 'default'
     
    INV, Sep 16, 2005 IP
  4. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, the code was designed to only handle the four cases you specified in your original post ;)

    ...but it's simple to change around:

    
    switch ($_SERVER['REQUEST_URI']) {
      case '/index.php':
        $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com/index.php' : 'URL is www.website.com/index.php');
        break;
      case '/':
        $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com' : 'URL is www.website.com');
        break;
      default: // anything else
        $title = 'Nothing else matched';
    }
    
    echo '<title>' . $title . '</title>';
    
    PHP:
    (This also has a built-in assumption/limitation, in that this only handles two host names, website.com & www.website.com. If you need more, you need to rewrite the ternary expressions into switch/case statements, or better yet, explain why you really need this so it can be rewritten explicitly for that purpose.)
     
    Willy, Sep 16, 2005 IP
    INV likes this.
  5. INV

    INV Peon

    Messages:
    1,686
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, I have a content system that I am trying to tweak. Basicly the title for all the pages was in the following format <title> <php websitename; ?> - <php pagename; ?> </title>

    See I dont want to have on everypage the name of the website name apear. So what I am trying to do is when i am on the 'index' file meaning the Home of the website I want it to show <php websitename; ?>. WHen it's on a page I want it to have <php pagename; ?> in the title
     
    INV, Sep 16, 2005 IP
  6. Shoemoney

    Shoemoney $

    Messages:
    4,475
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #6
    hmmm what cms?

    I think the issue here is your not being specific... you cited 4 exampes and got specific answers for those questions.

    Now you throw in the cms and well... that could get difficult.

    I will take a crack at it based on your post of the varible pagename in the title...

     if(isset($_GET['pagename']) && !empty($_GET['pagename'])) {
    
    #pass pagename to the title
    
    echo "<title>".$_GET['pagename']." </title>\n";
    
    }else {
    
    #if no pagename
    
    echo "<title>Default </title>"; 
    }
    
    PHP:
     
    Shoemoney, Sep 16, 2005 IP
    INV likes this.
  7. INV

    INV Peon

    Messages:
    1,686
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The cms isn't part of the problem. I am just explaining what it's for.

    Okay Willy got it right with the second code. However I have one more problem.

    
    switch ($_SERVER['REQUEST_URI']) {
      case '/index.php':
        $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com/index.php' : 'URL is www.website.com/index.php');
        break;
      case '/':
        $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com' : 'URL is www.website.com');
        break;
      default: // anything else
        $title = 'Nothing else matched';
    }
    
    echo '<title>' . $title . '</title>';
    
    PHP:

    For this part
    default: // anything else
    $title = 'Nothing else matched';


    I place websitename; in the $title.

    websitename; is the function which gets the title of the page I assign. However when I do that it just makes my title "websitename;" How would I make it act as a php function like it's suppose to.
     
    INV, Sep 16, 2005 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you mean like
    $title = myGetTitleFunction ();
    PHP:
     
    exam, Sep 16, 2005 IP
  9. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you have a CMS does it have a database? If so does the DB have a field where you can store title?

    I use my own cms which has title key and desc fields and simply have php call at the beginning of the page (before html) and a default title if fails.

    If not you may want to get familiar with sql as it will become messy if you try to put in fixed vars in the headers.

    Expat
     
    expat, Sep 16, 2005 IP
  10. INV

    INV Peon

    Messages:
    1,686
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #10

    This wont work as the function echos in that making the code look like

    <head>
    TITLE HERE<title></title>
     
    INV, Sep 16, 2005 IP
  11. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #11
    All right, now that it's clearer what you'd like to do, how about simply this :)

    
    $titles = array(
      '/index.php' => 'Home page title',
      '/' => 'Home page title',
      '/someotherpage.html' => 'Explicit title for some other page',
    );
    
    echo '<title>';
    if (array_key_exists($_SERVER['REQUEST_URI'], $titles)) {
      // for any page we've explicitly specified a title for, use that:
      echo $titles[$_SERVER['REQUEST_URI']];
    }
    else {
      // for any other page, call the CMS function which outputs the title
      websitename();
    }
    echo '</title>';
    
    PHP:
     
    Willy, Sep 16, 2005 IP
  12. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #12
    or...
    ob_start ();
    myGetTitleFunction (); 
    $title = ob_get_contents ();
    PHP:
    :D
     
    exam, Sep 16, 2005 IP
    INV likes this.
  13. INV

    INV Peon

    Messages:
    1,686
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #13


    Whoa :) Thank you so much!! Works exactly like needed
     
    INV, Sep 16, 2005 IP
  14. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #14
    My pleasure ;)

    (Exam's ob_start tip, above, is also good to remember for any similar situation where a function insists on directly outputting something instead of "nicely" returning it.)
     
    Willy, Sep 16, 2005 IP
    exam and Shoemoney like this.
  15. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #15
    It's definitely not the cleanest way of doing things, but it'll work in a pinch. :)
     
    exam, Sep 16, 2005 IP