Page 1 of 1

Dynamic Title

Posted: Fri Oct 22, 2010 9:48 am
by TravisT6983
I am posting this into the Newbie forum cause this seems nebieish to me...

Ok i have a few files that make up a set of pages

Topbar.php
index.php
info_birmingham.php ( i want the title to match the info page i have a few cities )
footer.php

OK so obviously the <title> is in top bar and i am trying to display the city name in the title which would be gotten from the info_city page

so in topbar i have...

Code: Select all

<title><?php echo ("$city"); ?> - <?php echo COMPANY; ?></title>


and then in the info_city page i have at the top of the page

Code: Select all

<?php
$city = "Birmingham";
?>
So i guess my question is why does the title not display the city name?

SOMEONE please help i have been pulling my hair out on this
you can see form the URL string that index.php controls all of the pages and the info page has the data for each city
http://www.skbk.com/OaklandCounty/index ... Birmingham

Thanks to anyone that might be able to shed some insight on this for me

T

Re: Dynamic Title

Posted: Fri Oct 22, 2010 10:11 am
by AbraCadaver
Most likely because you try and echo $city before it has been defined. Look at the order of your includes. I think it would be easier in your setup and less to keep track of if you do this:

Code: Select all

<title><?php echo ucfirst($_GET['section']); ?> - <?php echo COMPANY; ?></title>

Re: Dynamic Title

Posted: Fri Oct 22, 2010 10:28 am
by TravisT6983
You truly know your stuff with this that works great... and i 100% appreciate your response but can we take it a step further you might be able to help with this

Ok so some citys are 2 word for example Bloomfield Hills there for when i call them out with sections for bloomfield hill i get Bloomfield-Hills which is bad but i am going for perfections here ;)

so i am trying to use explode???

Code: Select all

$CityName = ucfirst($_GET['section']);
$aCityName = explode("-",$CityName);
and then

Code: Select all

<title><?php echo ("$aCityName"); ?> - <?php echo COMPANY; ?></title>
And it displays "array - company"

Thanks for your continued support

T

Re: Dynamic Title

Posted: Fri Oct 22, 2010 10:42 am
by TravisT6983
Hey AbraCadaver I actually figured it out on my own... i used

Code: Select all

<title><?php echo $aCityName[0]," ",$aCityName[1]; ?> - <?php echo COMPANY; ?></title>
I am not sure that this is the best coding and i am sure there is an easier way and if you have a suggestion on making my code better i would surely use it

Thanks again :drunk:

Re: Dynamic Title

Posted: Fri Oct 22, 2010 10:51 am
by twinedev
From looking at your site, it is looking like you already have it figured out.

One note though, sanatize the varaibel before you just directly put it in the output.

I was able to do

http://www.skbk.com/OaklandCounty/index ... 3Ctitle%3E

and get this when view source...

<title>Beverly Hilles</title><script src="http://domain.com/hacker.js" type="text/javascript"></script><title> - SKBK Sothebys International Realty</title>

-Greg

Re: Dynamic Title

Posted: Fri Oct 22, 2010 10:57 am
by AbraCadaver
Take twinedev's advice and make sure you sanitize all data before display. Try htmlentities().

The way you are doing it adding a dash - is fine and I would change it to this:

Code: Select all

$CityName = ucfirst(str_replace('-', ' ', $_GET['section']));
However, you can just encode the space for use in a URL and it will automatically be decoded on the next page instead of using a - if it is easier:

Code: Select all

echo 'http://www.skbk.com/OaklandCounty/index.php?section=' . urlencode('Beverly Hills');