I'm trying to set dynamic titles on my pages with a switch statement, but can't get it to work.

Tried ifelse using code gleaned from Google but it didn't work.

I'm trying to get it so that all my pages' titles are able to be dynamically used from a value across the site stored in my main file - content.php

This is my code:

<TITLE>My page title</TITLE>

<echo $mypagecontent ?>

Fairly basic, but the site is being developed on localhost anyway.

I would appreciate any help with this, as it's a new one for me!

Recommended Answers

All 11 Replies

try this and try to modify accordingly -

<?
//content is the array of titles which comes from the content.php page or the entire code can be put in the content.php
$content = array('title1 - This is the Homepage','title2 -Find new friends','title3 - Get your visa cleared now','title4 - bark about your wife here','title5 - Dont have kids yet','some other page');

$arr = explode('/',$_SERVER['PHP_SELF']);

$current_page = $arr[count($arr)-1];
//list your pages here
$pages  = array("1=>index.php","2=>friends.php","3=>nicks.php","4=>rent.php","5=>test.php");
for($i = 0; $i<count($pages);$i++)
{
	if($pages[$i] == trim($current_page))
		$pages_index = $i;
	else $pages_index = 6;
}
//This will echo the title
echo $content[pages_index];
?>

ask,if you stuck up somewhere.

Another way would be to have a content.php stored with the following code:

<?php

// Get pagename
$page = $_GET['page'];
if(!$page) $page = "home";

// Start pages

// Home page
$pagetitle[home] = "Welcome to our site!";
$pagecontent[home] = "We are glad you found our site!<br><font color='green'>Please have a browse through our pages.</font>";

// About us page
$pagetitle[aboutus] = "A little about us";
$pagecontent[aboutus] = "We are a small company located in the UK which specialise in SEO.";

// 404 Error page
$pagetitle[notfound] = "Error 404";
$pagecontent[notfound] = "The page you requested could not be found. (<b>".$page.")</b>";

// Set 404
if(!isset($pagetitle[$page])) $page = "notfound"; // Incase of 404

// Display page
//************
// Instead you could include a template file?
//************
echo "<h1>";
echo $pagetitle[$page];
echo "</h1>";
echo $pagecontent[$page];

?>

You could use page IDs if you want but names look better.

Hope this helps :)

Josh

Do arrays have to be numbered automatically, or can they be like array[pagetitlename.php]?

What I did there was an array but not a very good one. It's just vars for the element of the array.

They have to be strings though, no dots or special characters... correct me if i'm wrong.

This is the variable for the episode titles:

$phptitle = "<NAME OF PROGRAMME>";

However, I have to manually change it for every page and add it as:

<?php
$phptitle = "test";
?>

which is this further down the page:

<title>TV Episode Guide: PROGRAMME NAME: <?echo $phptitle ?></title>

I would like to know how to get it so that I can change it dynamically per page, whilst still using the echo variable.

Thanks

Explain in more detail, tell us about your project.

Explain in more detail, tell us about your project.

It is an episode guide for TV programmes, with the data stored in php variables.

Basicly...

<?php
$pagetitle = 'home'; // Set the default title...
$thisepisode = 'Home with the family'; // Your episode name?

// Overwrite the $pagetitle
if($thisepisode) $pagetitle = $thisepisode;

// Done

?>

That would work

Member Avatar for diafol

This is straightforward if your data is in DB (or file for that matter).
If you have just the one episode.php file and get the relevant programme via querystring and $_GET variable.

The structure of your data will be the key consideration.

If in a DB:

programmes table
id (PK)
page_title (varchar, 200?)
page_description (text)
page_keywords (text)
programme_title (varchar, 200?)
programme_contents (text)
programme_image (varchar, 100?)
programme_trailer_url (e.g. YouTube/Other id)
(other fields like main actors, director, production_company, year, etc)

The "page_" fields just get data to place into the HEAD area of the page. Perhaps "page_title" could be doubled up with "programme_title".

Using a DB may be useful as you could set up a WYSIWYG text editor (e.g. Spaw2/Yahoo!/FCKEditor/TinyMCE) to save this data.

Hand-coding hundreds of variables and placing them in a single file is probably asking for trouble.

The beauty of the DB is that you can search programmes by 'tag' (keywords) or category etc. You can also list programmes from the DB and create relevant links with the id in the querystring:

$output = "<ul>";
while(){
   $output .="\n\t<li><a href=\"episode.php?prog_id={$row['id']}\"></a>{$row['title']}</li>";
}
$output .="\n</ul>";

...more code...

echo $output;

The querystring will then pass the relevant 'id' via $_GET to the episode.php page. Then just query the DB to get the whole row - before the DTD. Then fill in your 'placeholder variables'.

This is straightforward if your data is in DB (or file for that matter).
If you have just the one episode.php file and get the relevant programme via querystring and $_GET variable.

Thank you for that advice - useful for if my site does have MySQL capacity, but my current webhost only offers PHP 5.28, with no databases available on the free hosting plan, so I've had to resort to PHP files only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.