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?
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.
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.
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.
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.
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.
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.
Your $title should be : $title="Results for {$region['$location']}"; PHP: or $title="Results for ".$region['$location']; PHP:
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.
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..
Figured this out - I rewrote the array as a multidimensional array and now I can reference additional info for each location
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...