views:

48

answers:

5

How would I make it possible to have dynamic(i think thats the word) titles with this setup? For example when I'm viewing a profile I want it to say Viewing profile of USERNAME' inside <title>. How could I make this possible without putting the html top on each indivudual page?

Thanks a lot.

and please dont start talking about mvc's, i dont need that

<html>
<head>
<title>KALASHNIKOV - $??</title>    
</head>
<body>

<?php

switch($area) {

// The default page
default:
include_once('pages/main.php');
break;  

// This is for registering an account
case 'register':
include_once('pages/register.php');
break;

// This is to log in!! (obviously)
case 'login':       
include_once('pages/login.php');
break;

// This is for logging out
case 'logout':
logout();
break;

}

?>

</body>
</html>
+2  A: 

Well, if you don't want to put the HTML at the top of every page, I assume you have it in a separate file, like header.php, which should look something like this:

<html>
<head>
<title><?= $page_title ?> - My Site</title>
</head>

And then on each of your pages, you just have to set $page_title before including it, like

$page_title = 'My Page';
include 'header.php'

That's one way of going about it, anyway.

Mark
+1  A: 

each sub-page should do something like:

<?php require('header.php') ?>

where header.php would have something like:

<title>KALASHNIKOV - <?php echo $GLOBALS['pageTitle'] ?></title>

or use zend framework which has support for templates and snippets

jspcal
A: 

I'd create an array like that, on the top of your php file (might be too complicated but as your site might grow bigger, it'll be easier to solve it like that):

<?php
error_reporting(E_ALL); // that's always good

$sites = array(
    'default' => array(
        'pages/main.php',
        'Main title'
    ),
    'register' => array(
        'pages/register.php',
        'Register'
    ),
    'login' => arary(
        'pages/login.php',
        'Login'
    }
);

$title = array_key_exists($area,$sites) ? $sites[$area][1] : $sites['default'][1];
?>
<html>
<head>
<title>KALASHNIKOV - <?=$title?></title>    
</head>
<body>
<?php
if (array_key_exists($area,$sites))
    require $sites[$area][0];
else
    require $sites['default'][1];
?>
watain
A: 

Put this anywhere on your page:

<script language="JavaScript">document.title ="KALASHNIKOV - blablabla";</script>
Benny
A: 

It looks like you could benefit from an extra layer. Rather than having your content page use a switch to include the content, you could have a separate script that uses a switch to set the content and the title (and anything else you might find necessary).

$title = "KALASHNIKOV - "
$include_page = "content.php"
$content_page = "pages/404.php";
switch($area) {

// The default page
default:
    $content_page = 'pages/main.php';
    break;  

// This is for registering an account
case 'register':
    $content_page = 'pages/register.php';
    break;

// This is to log in!! (obviously)
case 'login':       
    $content_page = 'pages/login.php';
    break;

// This is for logging out
case 'logout':
    $include_page = 'logout.php';
    break;

}

require_once $include_page;

content.php:

<html>
<head>
<title><?php echo $title; ?></title>    
</head>
<body>

<?php require_once $content_page; ?>

</body>
</html>

then logout.php would call logout() and do whatever there, I don't know how you're handling that.

This method also better supports the separation of logic from presentation, which is always a good idea.

Carson Myers