Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-06-2007, 12:05 AM   PM User | #1
phpnewbie99
New to the CF scene

 
Join Date: Sep 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
phpnewbie99 is an unknown quantity at this point
PHP Include w/ Custom Meta Tags?

Hi. I have a template that I use that calls a common header to all pages through a PHP include.

The problem is that I'd like to add custom meta tags individually to each page... but the template is calling the common header...

Each page will need unique meta tags so I need some sort of hack that lets me input the meta tags directly into the HTML while still pulling the common header.

Is this possible? Anyone have any tips?

thanks in advance.
phpnewbie99 is offline   Reply With Quote
Old 09-06-2007, 12:08 AM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
pull them out of a DB depending on which page it is?
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 09-06-2007, 12:15 AM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
If it's only a few pages use something like this:
PHP Code:
switch(basename($_SERVER['SCRIPT_NAME']))
{
    case 
'test.php':
        echo 
'<meta...>';
    break;
    
    case 
'contact.php':
        echo 
'<other meta..>';
    break;
    
    default:
        echo 
'<default meta...>';
    break;

Inigoesdr is offline   Reply With Quote
Old 09-06-2007, 12:37 AM   PM User | #4
phpnewbie99
New to the CF scene

 
Join Date: Sep 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
phpnewbie99 is an unknown quantity at this point
thanks for the responses... you know i figured it out though. It's not the cleanest way to do it i'm sure but it works...

If you're interested here's what I did.

In the header include file I put:

<meta name="description" content="<? if (isset($metadescription)) { echo $metadescription; } ?>">

Then on the individual page I put this immediately before I called the include:

<?php
$subtitle = "Page Title Here";
$metadescription = "Description Here";
$metakeywords = "Keywords Here";
require '../inc/header.inc.php'; ?>


When I load the page and view source it all comes out as clean HTML with the meta tags properly coded. So the search engines should be fine w/ it.

The subtitle is for the page title so each page can have a unique page title. The code in the include file is the same just w/ subtitle:

<title><? if (isset($subtitle)) { echo $subtitle; } ?></title>



As i said not the cleanest or easiest way i'm sure but it works. Hope someone else can use this.

By the way anyone have any thoughts on how to better this?

Last edited by phpnewbie99; 09-06-2007 at 12:39 AM..
phpnewbie99 is offline   Reply With Quote
Old 09-06-2007, 12:39 AM   PM User | #5
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
INIGoesDR (lol)
his idea is probably the better solution.
I'd probably go with that.
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 09-06-2007, 04:20 PM   PM User | #6
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
My solution is a little more complex, but in the end a lot cleaner and easier.

I have a static class called UI that I create per site (it has some resuable components, so it can inherit a base class if need be). This class has several methods to add things like scripts/css/etc. But these can be called from specific scripts to add to the common header. It could be changed to set/modify specific meta tags.

For example;

home.php
PHP Code:
    UI::addStylesheet('home'); 
header.php
PHP Code:
// ... some basic html here ...

foreach (UI::getStylesheets() as $css) {
    echo 
'<link href="'.$css.'.css" rel="stylesheet" type="text/css" />';
}

// ... more basic html ... 
This only works if your pages are executed before the header code. If you properly seperate model/views then you can add the stylesheet in the model, but that would be defeating the purpose of seperating business and presentation logic...
aedrin is offline   Reply With Quote
Old 09-09-2007, 01:19 PM   PM User | #7
syosoft
Regular Coder

 
Join Date: Sep 2006
Location: Vermont, USA
Posts: 154
Thanks: 0
Thanked 6 Times in 6 Posts
syosoft is an unknown quantity at this point
If you're trying to create a small, easy to template based site, look into buffers and modrewrite...it's amazing what 50 lines of code can do.
__________________
Active PHP/MySQL application developer available for immediate work.
syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready
syosoft is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:02 AM.


Advertisement
Log in to turn off these ads.