| | |
Dynamic title errors
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 54
Reputation:
Solved Threads: 0
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:
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!
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:
PHP Syntax (Toggle Plain Text)
<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!
•
•
Join Date: Sep 2009
Posts: 525
Reputation:
Solved Threads: 61
0
#2 30 Days Ago
try this and try to modify accordingly -
ask,if you stuck up somewhere.
PHP Syntax (Toggle Plain Text)
<? //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]; ?>
"The discipline of writing something down is the first step towards making it happen."
follow me on twitter
follow me on twitter
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 1
Another way would be to have a content.php stored with the following code:
You could use page IDs if you want but names look better.
Hope this helps
Josh
php Syntax (Toggle Plain Text)
<?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
•
•
Join Date: Nov 2008
Posts: 54
Reputation:
Solved Threads: 0
0
#6 19 Days Ago
This is the variable for the episode titles:
However, I have to manually change it for every page and add it as:
which is this further down the page:
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
PHP Syntax (Toggle Plain Text)
$phptitle = "<NAME OF PROGRAMME>";
However, I have to manually change it for every page and add it as:
PHP Syntax (Toggle Plain Text)
<?php $phptitle = "test"; ?>
which is this further down the page:
PHP Syntax (Toggle Plain Text)
<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
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 1
0
#9 19 Days Ago
Basicly...
That would work
php Syntax (Toggle Plain Text)
<?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
0
#10 19 Days Ago
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:
The querystring will then pass the relevant 'id' via $_GET['prog_id'] to the episode.php page. Then just query the DB to get the whole row - before the DTD. Then fill in your 'placeholder variables'.
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:
PHP Syntax (Toggle Plain Text)
$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['prog_id'] to the episode.php page. Then just query the DB to get the whole row - before the DTD. Then fill in your 'placeholder variables'.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
![]() |
Similar Threads
- mysql_numrows(): supplied argument is not a valid MySQL result resource (PHP)
- Dynamic Title (PHP)
- Dynamic Title Generation using PHP and MySql (PHP)
- Page title (ColdFusion)
Other Threads in the PHP Forum
- Previous Thread: Button php export table data
- Next Thread: Form input processing
| Thread Tools | Search this Thread |
# 5.2.10 access alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube





