When my sister was still responsible for creating SpacePhilippines.com she asked me to help her fix the errors on the site. I then noticed that we can make the editing simpler by using includes. I don’t know why I didn’t just use ASP and instead punished myself by converting the entire site into PHP. That was my first attempt in using and learning PHP. What I got stumped with is the titles of the pages. I was trying to figure out a code that will dynamically change the page title. I know how to do it in ASP and I was trying to apply the same logic in PHP but I didn’t have any luck. Anyway I did find out how to do it towards the end of the project.
Using includes, we need 3 pages to make this work: functions.php, header.php and anypage.php
(I’ll try andcreate a tutorial for this but until then, please google ‘php includes’ )
header.php
Type the line below inside your <title> tag:
<?php echo $title; ?>
Your title tag should look like this:
<title><?php echo $title; ?></title>
functions.php
Let’s say we have the pages AboutUs.php, ContactUs.php and the index.php page. We’ll use cases in functions.php to call the different titles when the user clicks on a different page. Type the following codes in your functions.php page.
<?php
function checkPage($arg) {
switch ($arg) {
// Main includes
case “”:
$title = “My Site”; //Replace this with the title of your site.
break;
case “index.php”:
$title = “My Site”;
break;
case “AboutUs.php”:
$title = “About Me”;
break;
case “ContactUs.php”:
$title = “Contact Me”;
break;
}
return $title;
}
?>
anypage.php
If you’re using includes, you only need to type the codes below in your index.php page
<?php include(“functions.php”);
$title = checkPage($_GET['contents']);
// Change ‘contents’ to the folder where
you placed the rest of your files
?>
<?php @ require_once (“includes/header.php”)?>
<!– end of main header table –>
That’s it! Run your site and you’ll find that the titles of each page will change as you click
on the different pages of your site. If you have a lot of pages, changing the titles using cases might not be a good idea.
If you have any comments/suggestions/violent reactions regarding the code, feel free to comment.

No related posts.



