Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to place page-generated title into <head>

We include a header.php file across all the pages on our site. So we could either place a single title in the header.php file which would be applied to the entire site, or have a custom header within each page to be more descriptive.

The problem is that in doing that, the title would be outside the head tags (which remain in the header.php file) and marked as invalid.

Is there some kind of function we can use to define a variable ($pageTitle) within the page, that will be displayed in head tag?

Thanks.

like image 922
Andelas Avatar asked Feb 27 '23 05:02

Andelas


2 Answers

Actually it should be this way

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

plain and simple

like image 108
Your Common Sense Avatar answered Feb 28 '23 19:02

Your Common Sense


Ummm.....

<?php 
$pageTitle = "Test";
include('header.php');
?>

EDIT

Then in your header.php

<head>
    <title><?php echo $pageTitle; ?> </title>
</head>
like image 45
Jim Avatar answered Feb 28 '23 18:02

Jim