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

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

- Mitko Kostov

Hello.

You can do it that way:

index.php

<?php  include("functions.php");
     $title = checkPage($_GET['page']);
          
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
    <?php $page.=".php";
              include($page);
    ?>
  </body>
</html>

functions.php :

<?php
    function checkPage($arg) {
      switch ($arg) {
        case "home":
          $title = "Home";
          break;
        case "downloads":
          $title = "Downloads";
          break;
      }
 return $title;

}
?>

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

this is a solution for have a title in each page?

<html><head>
</head>
<body>
<title>My site- article name</title>
</body><html>

or must write a title in head?

<?php
switch($_GET['page']) {
default:
include('src/home.php');
break;
case "Home":
include('src/home.php');
break;
case "Register":
include('src/register.php');
break;
}
?>

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

Member Avatar for diafol

//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:

$title = '...';
$keywords = '...';
$description = '...';
(other head variables)
$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.

$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':

...(do php include based on $_GET variable)...
<html>
...dtd...
<head>
<title><?php echo $title;?></title>
...more of the same for other head elements...
</head>
<body>
...get common php header...
...get common navbar...
...get common sidebar(s)...
<?php echo $content;?>
...get common php footer...
</body>
</html>

Sorry if I'm stating the obvious and telling granny how to suck eggs.

Sorry to bring up a thread, but my teacher showed me a way to show a default page!
CODE:
Index.php

<?php  include("functions.php");
     $title = checkPage($_GET['<strong class="highlight">page</strong>']);

?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
     <title><?php echo $title; ?></title>
  </head>
  <body>
    <?if($page=='') {$page="home.php";} else { $page.=".php"; }
              include($page);
    ?>
  </body>
</html>

That area right before the </body > tag is all we edited! (the php area)

Member Avatar for diafol

The code doesn't make much sense to me (top bit):

$_GET['<strong class="highlight">page</strong>']

This is very odd. Your teacher's code did you say? Hmmm.

Yes, I ONLY changed the area between the body tags, no other part I changed...

Member Avatar for diafol
<?if($page=='') {$page="home.php";} else { $page.=".php"; }
              include($page);
    ?>

Seems this will always give the homepage as $page isn't declared anywhere.

Well, it worked for me! Check out:
http://ss.kcmartz.com (my school site, I own whole site tho) and add "/test" to the end of the url after the homepage. If I didn't do this, it gave me PHP warnings!

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.