Jump to content

[SOLVED] Changing titles per page with inc files.


tothemax

Recommended Posts

Ok I have a problem.

 

The way my script is set up now, I have an index.php which calls on inc files.  The problem is that each page (inc file) is using the title set in the php.index.

 

My goal is to have a title for each page.  I've looked and looked but can't find anything.  Any help would be appreciated.  Thanks.

Link to comment
Share on other sites

There's really no need to bump your post after half an hour.

 

Anywho, you could do something along the lines of:

 

<?php
$page = $_GET['page'];//im assuming that the file you are including is being passed in the URL somehow.
include('includes/'.$page.'.php');//again, no idea if this is how you are doing it - thats the trouble with no code being posted

//use a switch statement:
switch($page){
case 'page1':
$title = 'Some title for page 1';
break;
case 'page2':
$title = 'A different title for page 2';
break;
case 'page3':
$title = 'Guess what? Another title';
break;
default://if no page set - e.g. its just the index page
$title = 'Welcome to my website';
break;
}
//further down your script
echo '<title>'.$title.'</title>';
?>

 

Alternatively, you could define the $title variable within each page that you might include. You'd then set a title on the index page, which would be overridden if you do include a file. For example, index.php:

 

<?php
$title = 'Your default title';
$page = $_GET['page'];
include('includes/'.$page.'.php');
//further down your script
echo '<title>'.$title.'</title>';
?>

 

Then all of your files to be included:

<?php
$title = 'Your title for this page';
//the rest of your script
?>

Link to comment
Share on other sites

Sorry, yes its late. That wasn't ever really going to work.

 

Given that you're including a file each time, it should be a simple matter of defining the title in each of these pages.  You should only show the title for the index file if no page has been included:

 

<?php
if(!isset($_GET['page'])){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome to my website. This is the Index page</title>
</head>
<body>
Some content
</body>
</html>
<?php
}else{
include('includes/'.$_GET['page'].'.php');
}
?>

 

On a side note, if you are including files in this manner, you would be wise to validate the input from the $_GET array to make sure the page you are including is supposed to included.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.