Forum Moderators: coopster

Message Too Old, No Replies

Dynamic site creation

using includes for <title> and <meta name="description">

         

CNibbana

12:47 am on Apr 25, 2004 (gmt 0)

10+ Year Member



I just converted my site to generate dynamically via PHP and includes. I am trying to figure out how to change the <title> and <meta name="description"> when each page is generated. I'm a PHP newb, but I do know that I can simply declare a variable $title and $description and place them in the main page, however I'm not sure where I could store the variable values until they're needed?

Can I create another variable, say $content and then have all three of them identified within each page?:
<?php
$title = "homepage"
$description = "what page is about"
$content = {
<html line1>
<html line2>
...etc (html lines 3-100)
}
?>
Or is this even possible?

What I am currently using is an Include statement to input the entire page, so I'm not sure how to involve the $title and $description variables into it:
a href="index.php?page=home">home</a>
a href="index.php?page=about">about</a>

Credit goes to forum member "Distel" for the following code:
<?php
if(isset($_GET['page'])) {
include("{$page}/index.php");
print($page);
}
else {include('home/index.php');
}
?>

So what is the easiest way to dynamically call the $title and $description variables and where should I store them?

igotfivecents

2:43 am on Apr 25, 2004 (gmt 0)



create a **settings.php** file inside that create your strings. $mytitle="Welcome to my site";

call it at the top of each page you wish to change....
then where the title is you could do it this way

<title> <? echo $mytitle;?> </title>

bumpaw

8:50 pm on Apr 25, 2004 (gmt 0)

10+ Year Member



This works for me. I first create a function like:

<?php
function do_html_head($title,$keywords,$description)
{
// print an HTML head
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title><?php echo $title;?></title>
<META NAME="keywords" CONTENT="<?php echo $keywords?>">
<META NAME="description" CONTENT="<?php echo $description?>">
}

And then I apply it on the page where I need it like:

<?php
do_html_head('title text','keywords text','description text');
?>

mifi601

11:27 pm on Apr 25, 2004 (gmt 0)

10+ Year Member



Do you have a different title for each page? Are you using a database for the titles?
Or is it the same title that you just want to be able to change from time to time?

bumpaw

12:05 am on Apr 26, 2004 (gmt 0)

10+ Year Member



I use the same function on each page as you see it at the bottom of my first post with title, keywords, and description different each time. Example: Welcome page.

<?php
do_html_head('Welcome to Clyde's Used Camels','used camels, camel humps','we specialize in used camels both one and two hump');
?>

mifi601

12:57 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



bumpaw - that is a really neat solution!

I was always wondering how to get on site specifics into the include(d) header ....

I did not realize one could define a variable after it is being called ...

thanks!

mifi601

1:26 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



I tried it.

I do not see the advantage. you'd have to call the function first thing on the page, otherwise it would not be on top. Might as well write the headers in there right away on the page and make the included header shorter. The savings between the function, the execution time, etc. seem to be minimal.

or am i missing something?

currently I use the actual file name to create title and headers on the fly. I use php_self and it seems to work. albeit I would love to have better description tags ...

Mike12345

1:33 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



[webmasterworld.com...] for some ideas :)

CNibbana

2:56 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



I ended up creating a seperate file in each subdirectory called head.php and declaring the variables within it:

<? php
$title = 'page title here';
$desc = 'page description';
$key = 'keywords';
$robots = 'index, follow';
?>

and simply using an include to get the variables into the template page:

<?php if(isset($_GET['page'])) {include("{$page}/head.php");} else {include('home/head.php');}?>

and plugging them into the title, description, etc.:

<title><?php echo "$title";?></title>
<meta name="description" content="<?php echo "$desc";?>" />
<meta name="keywords" content="<?php echo "$key";?>" />
<meta name="robots" content="<?php echo "$robots";?>" />

My initial goal was to be able to declare these variables within the content document without having to create a seperate head.php document for each page, but I couldn't figure out a way to do it.

So what do you think? Is there a better or more efficient way that I should know about before I begin making all of these header documents?

kjs50

8:11 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



I would also suggest using Smarty Templates and you can have a pretty nice solution using that as well.

CNibbana

3:40 am on Apr 28, 2004 (gmt 0)

10+ Year Member



I came up with a pretty cool solution for those that are interested. To reiterate, my goal was to have one template page with each content page to include the page header info and the content.

This is in my template page before the <head>:

<?php $x=head; if(isset($_GET['page'])) {include("{$page}");} else {include('home/index.php');}?>

This is in the template page <head>:

<title><?php echo "$title";?></title>
<meta name="description" content="<?php echo "$desc";?>" />
<meta name="keywords" content="<?php echo "$key";?>" />
<meta name="robots" content="<?php echo "$robots";?>" />

And this is in the template page <body> where you want your content:

<?php $x=body; if(isset($_GET['page'])) {include("{$page}");} else {include('home/index.php');}?>

Notice the $x variable determining head versus body content. Then I simply made my includes pages as follows:

<?php
$title = 'my page';
$desc = 'page description';
$key = 'keywords;
$robots = 'index, follow';
if ($x==head) {return;}
?>
<!-- begin content below -->

This solution allows you to declare your page header info right within your content pages. Hopefully this may come in handy for someone.

[edited by: CNibbana at 3:45 am (utc) on April 28, 2004]

CNibbana

3:43 am on Apr 28, 2004 (gmt 0)

10+ Year Member



.fixed above