Dynamic TITLE Text not being generated

Discussion in 'PHP' started by Omzy, Oct 4, 2008.

  1. #1
    I'm having this problem generating dynamic TITLE text on one of my search pages. Here are the relavant parts of the code:

    search.php:

    $location=$_GET['location'];
    $title="Results for $region[$location]";
    include("header.php");
    PHP:
    header.php:

    <?php
    $region['london']="London";
    $region['north-west']="North West";
    $region['north-east']="North East";
    $region['south-west']="South West";
    $region['south-east']="South East";
    print "<title>Welcome- $title</title>";
    ?>
    PHP:
    As you can see $location is being pulled from the URL and matched up against the array to print out it's "friendly" name. This works fine when printing out a H1 heading, however TITLE text doesn't seem to be generating, all I get is:

    "Welcome - Results for "

    Can anyone figure out where the problem lies?
     
    Omzy, Oct 4, 2008 IP
  2. SeanBlue

    SeanBlue Peon

    Messages:
    110
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I might be wrong but I think it's simply because you're calling header.php after establishing $title.

    Instead try:

    $location=$_GET['location'];
    include("header.php");
    $title="Results for $region[$location]";

    That way $region is declared BEFORE you construct $title, which uses it.
     
    SeanBlue, Oct 4, 2008 IP
  3. SeanBlue

    SeanBlue Peon

    Messages:
    110
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually, my mistake, you'll need to do:

    search.php
    header.php
     
    SeanBlue, Oct 4, 2008 IP
  4. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yep that's what I thought, but I have tried that and it doesn't work either. All I get then is:

    "Welcome - "

    Just to re-iterate I have got a H1 heading being printed out further down the script (after the include statement) with the same code and that prints out fine.
     
    Omzy, Oct 4, 2008 IP
  5. SeanBlue

    SeanBlue Peon

    Messages:
    110
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not too sure then, is this code inside a function or something like that? If so you'll need to declare $region as global.

    Alternatively you could pull the title straight from the $_GET using something like this

    echo '<title>Welcome - Results for '.ucwords(str_replace('-',' ',$_GET['location'])).'</title>';

    So south-east would become South East, etc.
     
    SeanBlue, Oct 4, 2008 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    If SeanBlue's last example isn't working, echo out the value of $_GET['location'] - it could be you aren't getting a value that's on your $region list for some reason.
     
    deathshadow, Oct 4, 2008 IP
  7. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    SeanBlue - the problem with your second example is that the TITLE tag would get written at the end of the HTML, which means it wont conform to the HTML STRICT standard...

    Additionally there are other pages that need the TITLE generated dynamically, so TITLE needs to be in the header.php page.
     
    Omzy, Oct 4, 2008 IP
  8. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    OK I fixed the issue - I put the array from header.php in another file "locations.php" and simply include this file before I declare $title and then include header.php after that.
     
    Omzy, Oct 4, 2008 IP
  9. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Your $title should be :

    
    $title="Results for {$region['$location']}";
    
    PHP:
    or


    
    $title="Results for ".$region['$location'];
    
    PHP:
     
    ads2help, Oct 4, 2008 IP
  10. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Cheers ads2help, I'll give that a try too.

    Another question - how do I specify different TITLE text for each $location in search.php?

    For example at the moment it will take $_GET['location'] and print out "Results for $region[$location]" (using the array $region)

    However I want to extend this to make it print for example "Results for North West, including Manchester, Liverpool and Bolton"

    But I wish to retain the existing print statement.
     
    Omzy, Oct 6, 2008 IP
  11. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Any thoughts on the above anyone? ;-)
     
    Omzy, Oct 6, 2008 IP
  12. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #12
    a database might be better? atleast for the last thought.
     
    NatalicWolf, Oct 6, 2008 IP
  13. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Database would probably be better but can it be done in an array?
     
    Omzy, Oct 7, 2008 IP
  14. bennjoe

    bennjoe Peon

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I tried this, don't know its meet your criteria or not, because im using condition for it, but its work.

    <?
    $location=$_GET['location'];
    
    if($location=='london')
    {
    $region['london']="London";
    }
    elseif($location=='north-west')
    {
    $region['north-west']="North West";
    }
    elseif($location=='north-east')
    {
    $region['north-east']="North East";
    }
    elseif($location=='south-west')
    {
    $region['south-west']="South West";
    }
    elseif($location=='south-east')
    {
    $region['south-east']="South East";
    }
    
    $title="Results for $region[$location]";
    
    print "<title>Welcome- $title</title>";
    ?>
    PHP:
    but i really think it SHOULD used condition, because if not..how it will know which locations to choose..
     
    bennjoe, Oct 7, 2008 IP
  15. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Figured this out - I rewrote the array as a multidimensional array and now I can reference additional info for each location :D
     
    Omzy, Oct 7, 2008 IP
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #16
    Mein Gott. Here's a tip, if you are constantly checking the same variable in a string of IF statements, use a switch instead. It's usually less code, and it's most always faster to execute too... much there is no reason to only fill in one value in the array since you would likely want the whole thing indexed.

    I think we can all assume that the form from which he's $_GETting probably has the locations to choose in it.

    Which is why instead of having the form pass north-east or north-west, I have to ask why isn't the form set up to pass "London" or "North East" in the first place? Then you'd not even have to bother with the array...
     
    deathshadow, Oct 7, 2008 IP