943,712 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 15825
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2007
0

Dynamic Title

Expand Post »
Every page in the system is grabbed into index.php.
For each page i'd like the title to change according to the page it's on, this can be set manually within each page we want to send.

My question is what would be the most effective method of updating the title each time a new page is viewed.

eg: $common->setTitle($title);

Is what i have in mind, but how would index.php know what the current title is so it can do '<title><?php echo getTitle(); ?>' or something along those lines.

Thanks in Advance.
Drag
Similar Threads
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 17th, 2007
0

Re: Dynamic Title

Sorry for the double post, please some mod to delete this.

- Mitko Kostov
Last edited by MitkOK; Jul 17th, 2007 at 3:56 pm.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 17th, 2007
0

Re: Dynamic Title

Hello.

You can do it that way:

index.php

php Syntax (Toggle Plain Text)
  1. <?php include("functions.php");
  2. $title = checkPage($_GET['page']);
  3.  
  4. ?>
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <!DOCTYPE html
  7. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  8. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  10. <head>
  11. <title><?php echo $title; ?></title>
  12. </head>
  13. <body>
  14. <?php $page.=".php";
  15. include($page);
  16. ?>
  17. </body>
  18. </html>

functions.php :

php Syntax (Toggle Plain Text)
  1. <?php
  2. function checkPage($arg) {
  3. switch ($arg) {
  4. case "home":
  5. $title = "Home";
  6. break;
  7. case "downloads":
  8. $title = "Downloads";
  9. break;
  10. }
  11. return $title;
  12.  
  13. }
  14. ?>

If you want to include page downloads.php for example - > index.php?page=downloads.

This is just a simple guess and you can find other way.

- Mitko Kostov
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
May 28th, 2009
0

Re: Dynamic Title

this is a solution for have a title in each page?
html Syntax (Toggle Plain Text)
  1. <html><head>
  2. </head>
  3. <body>
  4. <title>My site- article name</title>
  5. </body><html>
or must write a title in head?
Last edited by peter_budo; May 29th, 2009 at 6:52 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
constantin_ro is offline Offline
1 posts
since May 2009
Jul 18th, 2009
0

Re: Dynamic Title

php Syntax (Toggle Plain Text)
  1. <?php
  2. switch($_GET['page']) {
  3. default:
  4. include('src/home.php');
  5. break;
  6. case "Home":
  7. include('src/home.php');
  8. break;
  9. case "Register":
  10. include('src/register.php');
  11. break;
  12. }
  13. ?>

this is the code im using and it works for me change the include() stuff and if you save this as index go to
http://yourweb.com/name?page=case
like mine is 127.0.0.01/?page=home i recommend using it as index so u dont need to type in the extra index.php?page blah blah blah BUT dont send them to http://yourweb.com just like that you have to send them to http://yourweb.com/?page=home or theres gonna be a wierd glitch still workin on it
Last edited by peter_budo; Jul 19th, 2009 at 5:06 am. Reason: Keep It Spam Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamjoseph951 is offline Offline
1 posts
since Jul 2009
Jul 19th, 2009
0

Re: Dynamic Title

//EDIT

Apologies, I posted this and only then saw the original thread creation date, after it was resurrected.

//EDIT


Are you using include files or a db to store your pages?

If using a db, you could have something like:

PAGE_DATA
id
page_title
page_keywords
page_description
(other head fields)
page_content

If using files, just place the head data at the top of the include file:

PHP Syntax (Toggle Plain Text)
  1. $title = '...';
  2. $keywords = '...';
  3. $description = '...';
  4. (other head variables)
  5. $page_content = '...';

Obviously you'd need to include this file above your DTD and echo out the variables in the appropriate place.

If you're using a CMS / online text-editor to create your page content, the $page_content variable could be the contents of that file.

PHP Syntax (Toggle Plain Text)
  1. $page_content = file_get_contents('content/welshrugby_2009.html');

There are quite a few ways to do this.

BTW
If you're creating a system like WordPress, the db solution could be useful coz you could have an url like index.php?article=7 . Your $_GET['article'] variable would then be used to search the db for the page. Otherwise, you may be left using a string as a parameter in the querystring for a particular filename: index.php?article=welshrugby_2009 which would then include the welshrugby_2009.php file (and possibly get a welshrugby_2009.html through the file_get_contents as mentioned above, depending on how you're creating the content).

The beauty of your method is that you've just got 1 'template':

PHP Syntax (Toggle Plain Text)
  1. ...(do php include based on $_GET variable)...
  2. <html>
  3. ...dtd...
  4. <head>
  5. <title><?php echo $title;?></title>
  6. ...more of the same for other head elements...
  7. </head>
  8. <body>
  9. ...get common php header...
  10. ...get common navbar...
  11. ...get common sidebar(s)...
  12. <?php echo $content;?>
  13. ...get common php footer...
  14. </body>
  15. </html>

Sorry if I'm stating the obvious and telling granny how to suck eggs.
Last edited by ardav; Jul 19th, 2009 at 3:44 pm.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Feb 23rd, 2010
0
Re: Dynamic Title
Sorry to bring up a thread, but my teacher showed me a way to show a default page!
CODE:
Index.php
PHP Syntax (Toggle Plain Text)
  1. <?php include("functions.php");
  2. $title = checkPage($_GET['<strong class="highlight">page</strong>']);
  3.  
  4. ?>
  5. <!DOCTYPE html
  6. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  7. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  9. <head>
  10. <title><?php echo $title; ?></title>
  11. </head>
  12. <body>
  13. <?if($page=='') {$page="home.php";} else { $page.=".php"; }
  14. include($page);
  15. ?>
  16. </body>
  17. </html>

That area right before the </body > tag is all we edited! (the php area)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kcmartz is offline Offline
3 posts
since Feb 2010
Feb 23rd, 2010
0
Re: Dynamic Title
The code doesn't make much sense to me (top bit):

PHP Syntax (Toggle Plain Text)
  1. $_GET['<strong class="highlight">page</strong>']

This is very odd. Your teacher's code did you say? Hmmm.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Feb 23rd, 2010
0
Re: Dynamic Title
Yes, I ONLY changed the area between the body tags, no other part I changed...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kcmartz is offline Offline
3 posts
since Feb 2010
Feb 23rd, 2010
0
Re: Dynamic Title
PHP Syntax (Toggle Plain Text)
  1. <?if($page=='') {$page="home.php";} else { $page.=".php"; }
  2. include($page);
  3. ?>

Seems this will always give the homepage as $page isn't declared anywhere.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 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: How to post on blogger blog using PHP?
Next Thread in PHP Forum Timeline: Undefined Index Error





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


Follow us on Twitter


© 2011 DaniWeb® LLC