943,992 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 775
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 29th, 2009
0

Dynamic title errors

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  1. <TITLE>My page title</TITLE>
  2.  
  3. <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!
Similar Threads
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Oct 30th, 2009
0
Re: Dynamic title errors
try this and try to modify accordingly -
PHP Syntax (Toggle Plain Text)
  1. <?
  2. //content is the array of titles which comes from the content.php page or the entire code can be put in the content.php
  3. $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');
  4.  
  5. $arr = explode('/',$_SERVER['PHP_SELF']);
  6.  
  7. $current_page = $arr[count($arr)-1];
  8. //list your pages here
  9. $pages = array("1=>index.php","2=>friends.php","3=>nicks.php","4=>rent.php","5=>test.php");
  10. for($i = 0; $i<count($pages);$i++)
  11. {
  12. if($pages[$i] == trim($current_page))
  13. $pages_index = $i;
  14. else $pages_index = 6;
  15. }
  16. //This will echo the title
  17. echo $content[pages_index];
  18. ?>
ask,if you stuck up somewhere.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 30th, 2009
0

Another way to do it

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

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // Get pagename
  4. $page = $_GET['page'];
  5. if(!$page) $page = "home";
  6.  
  7. // Start pages
  8.  
  9. // Home page
  10. $pagetitle[home] = "Welcome to our site!";
  11. $pagecontent[home] = "We are glad you found our site!<br><font color='green'>Please have a browse through our pages.</font>";
  12.  
  13. // About us page
  14. $pagetitle[aboutus] = "A little about us";
  15. $pagecontent[aboutus] = "We are a small company located in the UK which specialise in SEO.";
  16.  
  17. // 404 Error page
  18. $pagetitle[notfound] = "Error 404";
  19. $pagecontent[notfound] = "The page you requested could not be found. (<b>".$page.")</b>";
  20.  
  21. // Set 404
  22. if(!isset($pagetitle[$page])) $page = "notfound"; // Incase of 404
  23.  
  24. // Display page
  25. //************
  26. // Instead you could include a template file?
  27. //************
  28. echo "<h1>";
  29. echo $pagetitle[$page];
  30. echo "</h1>";
  31. echo $pagecontent[$page];
  32.  
  33. ?>

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

Hope this helps

Josh
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Nov 9th, 2009
0
Re: Dynamic title errors
Do arrays have to be numbered automatically, or can they be like array[pagetitlename.php]?
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 9th, 2009
0
Re: Dynamic title errors
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.
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Nov 9th, 2009
0
Re: Dynamic title errors
This is the variable for the episode titles:
PHP Syntax (Toggle Plain Text)
  1. $phptitle = "<NAME OF PROGRAMME>";

However, I have to manually change it for every page and add it as:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $phptitle = "test";
  3. ?>

which is this further down the page:
PHP Syntax (Toggle Plain Text)
  1. <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
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 9th, 2009
0
Re: Dynamic title errors
Explain in more detail, tell us about your project.
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Nov 9th, 2009
0
Re: Dynamic title errors
Click to Expand / Collapse  Quote originally posted by rokape ...
Explain in more detail, tell us about your project.
It is an episode guide for TV programmes, with the data stored in php variables.
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 9th, 2009
0
Re: Dynamic title errors
Basicly...

php Syntax (Toggle Plain Text)
  1. <?php
  2. $pagetitle = 'home'; // Set the default title...
  3. $thisepisode = 'Home with the family'; // Your episode name?
  4.  
  5. // Overwrite the $pagetitle
  6. if($thisepisode) $pagetitle = $thisepisode;
  7.  
  8. // Done
  9.  
  10. ?>

That would work
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Nov 9th, 2009
-1
Re: Dynamic title errors
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:

PHP Syntax (Toggle Plain Text)
  1. $output = "<ul>";
  2. while(){
  3. $output .="\n\t<li><a href=\"episode.php?prog_id={$row['id']}\"></a>{$row['title']}</li>";
  4. }
  5. $output .="\n</ul>";
  6.  
  7. ...more code...
  8.  
  9. 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'.
Sponsor
Featured Poster
Reputation Points: 1054
Solved Threads: 949
Sarcastic Poster
ardav is offline Offline
6,700 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Button php export table data
Next Thread in PHP Forum Timeline: Form input processing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC