chrismarsden Posted August 31, 2010 Share Posted August 31, 2010 Hi peeps, was wondering if anyone could help, i have decided I want to put page titles in to my website at the moment i have a static title which is the business name and slogan, what I want is business name - slogan - page title (E.G home) my title is set in a header.php file which is then included in all pages. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/ Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 What's an example of a URL to a page on your site? Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105641 Share on other sites More sharing options...
chrismarsden Posted August 31, 2010 Author Share Posted August 31, 2010 http://www.somesite.com/conquick.php Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105643 Share on other sites More sharing options...
chrismarsden Posted August 31, 2010 Author Share Posted August 31, 2010 the only thing i can think of is setting variables for each page i.e: conquick.php would have a variable set to "quick guide to connections" i would do this for each page then have the script track what page was opened then out put the title which relates to that page, only problem is i have no idea how i would do this... Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105646 Share on other sites More sharing options...
AbraCadaver Posted August 31, 2010 Share Posted August 31, 2010 Maybe something as simple as this in your header file or a config file that you include: $title['home'] = 'business name - slogan - home page'; $title['conquick.php'] = 'business name - slogan - page title'; $title['another.php'] = 'business name - slogan - another page title'; if(isset($title[basename($_SERVER["SCRIPT_NAME"]))) { $page_title = $title[basename($_SERVER["SCRIPT_NAME"]); } else { $page_title = $title['home']; } The just echo $page_title. Or something more elaborate along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105668 Share on other sites More sharing options...
Adam Posted August 31, 2010 Share Posted August 31, 2010 How do you display your header, is it just a simple include for each page? If so there's no point adding unnecessary complexity to it. You may as well just set a variable before the include then check for that in the template: $title = 'Page Name'; require 'path/to/header.php'; In your header: <title>Company Name - Slogan<?php if (!empty($title) echo '- ' . $title; ?></title> Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105670 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 For something so simple, there are many, many ways to skin a cat. One not necessarily being the most correct, though in sticking with Occam's Razor you may want to choose the simplest. If you want the convenience of only having to keep track of custom code in the header, or knowing that the header is before all other custom code, and your titles are all in one, more easily maintainable place, you could easily use an associative array for this. $page = basename($_SERVER["SCRIPT_NAME"]); $titles = array( 'conquick.php' => 'Ttitle One', 'thispage.php' => 'Title Foo', 'barpage.php' => 'Title Bar'); <title>Company Name :: Slogan :: <?php echo $titles[$page]; ?></title> Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105675 Share on other sites More sharing options...
chrismarsden Posted September 1, 2010 Author Share Posted September 1, 2010 For something so simple, there are many, many ways to skin a cat. One not necessarily being the most correct, though in sticking with Occam's Razor you may want to choose the simplest. If you want the convenience of only having to keep track of custom code in the header, or knowing that the header is before all other custom code, and your titles are all in one, more easily maintainable place, you could easily use an associative array for this. $page = basename($_SERVER["SCRIPT_NAME"]); $titles = array( 'conquick.php' => 'Ttitle One', 'thispage.php' => 'Title Foo', 'barpage.php' => 'Title Bar'); <title>Company Name :: Slogan :: <?php echo $titles[$page]; ?></title> Hi peeps, this script worked great... cheers for your help people!!! much appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1105989 Share on other sites More sharing options...
Adam Posted September 1, 2010 Share Posted September 1, 2010 The main problems I see with this are that the titles have to be static, you couldn't have dynamic content (i.e. from a database) and vary the title depending on what the user's viewing. You'll also have to keep modifying it should you ever rename a file, make structural changes, etc. If you had two files with the same name, but one within a sub-directory, you're only going to get whichever's first in the array. Plus there'll be a lot of repetition if you want some pages with the same name. Of course if your website is only small - you never mentioned the size - this won't be such a problem. Quote Link to comment https://forums.phpfreaks.com/topic/212183-page-titles-help-needed/#findComment-1106000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.