Forum Moderators: coopster

Message Too Old, No Replies

minimizing includes - PHP

a newbie, revealed

         

mipapage

8:51 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, I'm designing my first three PHP sites. WW has been superhelpful with this for other issues already - I searched around here and on the web, but thought someone here may have a good link or some advice to help with the latest dilema...

Looking for any pointers, links etc...

Objective:
Fairly simple. I have a template, css/xhtml. I need to 'include' the content, possibly the menu, and definitely the Title tag (= 3 variables).

Problems (as I see them):
I only know how to include things this way:

<?php @ require_once ("$page.html");?>
So therefore I will need up to three variables for each page, and a (for example) menu.html, title.html, and content.html as includes for each page. Is this right, or is there a more efficient way to do this?

I am using mod_rewrite to clean up the urls. Having three variables also means that I have to represent them all in the 'clean url'. Seems kinda... 'cumbersomely long', no?

Hmm.. that's about it...

mipapage

9:31 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There I am showing off my newbie-ness, referring to includes in the title and require once in the message!

Birdman

9:34 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well it doesn't sound like you're using a database to store your info. I'll assume you are using a text file for each page of content.

If the menu stays the same on all pages, use a separate include for it. Then, put each page of content AND it's corresponding title in a separate file, named by the variable you will use to call the content.

That only makes one variable to pass in the url, which you can do with a regular querystring because the major engines have no problem with one or two vars in the url(eg. .com?page=1234). Or, you can still use mod_rewrite if you wish.

Name your content files with a .php extension and in the file do something like this:


<?php
function title()
{
?>
>>> Your <title> and other <head> info here <<<
<?php
}

function body()
{
?>
>>> Your main content here <<<
<?php
}
?>

Then, in your template:


<?php
include("1234.php");
include("menu.php");
?>
<html>
<head>
<?php
title();
?>
</head>
<body>
<?php
body();
?>

Something like that ;) I like to use a database to store my page information because the files don't keep piling up and I can develop easy searches for info via the db.

Birdman

mipapage

9:52 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey birdman, thanks for the quick response.

One question though:

In your example above, in the second box, the one that represents my 'template', you put...
<?php
include("1234.php");
include("menu.php");
?>

I take it that the '1234.php' should actually be the variable that is given in '.com?page=1234'?

What is mixing me up here is that it appears to me, in your example, I am lacking a variable. No?

Birdman

10:09 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, actually the first include would be the variable from the referring url.

It would look like this:

include("{$page}.php");

This is assuming that the template AND the content files are in root. If not, simply adjust the include.

include("/pages/{$page}.php");

So, if you call your template with a url like this:

www.site.com/template.php?page=1234

1234.php will be included..

Hope that helps some.

mipapage

10:17 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hope that helps some.

There's an understatement. It helps tonnes, though we are set up using 'require once', I'll give the includes a shot.

This all grew from a css-content-management article over at alistapart. It started simple, but, well, sans-database I suppose is still quite simple, just bigger!

daisho

10:29 pm on May 14, 2003 (gmt 0)

10+ Year Member



require_once and include_once are very similar. require gives a fatal error if it could not include the file and include only gives a supressable warning.

Might I suggest for your example to use require rather than require_once. require_once must keep track of the files it has already included to ensure no double inclusion. The difference is most likely not noticable but require and include do slightly less work then their _once counterparts.

daisho.

mipapage

10:39 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks daisho,

I think that if I get this working (haven't tried yet, about to) I will go with include.

I have a feeling that this is very basic PHP here, but it is supercool.

My resident PHP 'novice' is shaking her head going, 'of course, it's so clear now...'

mipapage

10:57 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



birdman,

worked like a charm. You have no idea how ecstatic I am right now. We have tonnes of content coming tomorrow and a Monday deadline. This was the last hitch.

Fwiw, I adjusted things so that they open and closed 'properly' in this part:


<?php
function title()
{
?>
Your <title> and other <head> info here
<?php
}
?>

<?php
function body()
{
?>
Your main content here
<?php
}
?>

Not sure if that is important at all, just did it that way though...Thanks again!

willybfriendly

10:57 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"What is mixing me up here is that it appears to me, in your example, I am lacking a variable. No?"

It depends on how you are set up. The variable would only be necessary if your script needs it to do its thing. There is no requirment for a variable. eg

<HTML>
<HEAD>
<TITLE XXXX </TITLE>
</HEAD>
<BODY>
<?
include("header.php");
include("menu.php");
?>
content
<?include("footer.php");
</BODY>
</HTML>

Should work fine for static pages.

If content is pulled from a db then it would become a variable that would need to be passed in your anchor.

I think that makes sense.

WBF

mipapage

11:15 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey WBF,

Good point. I need the variable though in this instance, in fact I was needing three, and now, only one!

Birdman

11:37 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick note:

You don't need to adjust the original code. What you did was go out of PHP mode and then right back into it. Since there is no HTML between the end of the first function and the start of the next, you can just stay in PHP mode.

Glad you are 'cooking with gas' now.

aspr1n

11:53 pm on May 14, 2003 (gmt 0)

10+ Year Member



Just to add my tuppence worth, I would do it a little differently:

default.php:

<?php require( 'myLib.php' );?>

myLib.php:

<?php 
//do some stuff that defines content and page title...
$pageTitle = 'xyz';

if( I-decide-something )
$header = 'a-header';
else
$header = 'a-different-header';

if( I-decide-something-else )
$body = 'a-body';
else
$body = 'a-different-body';

include( '$header. inc.php);
include( '$body. inc.php);
?>

a-header.inc.php:

<title><?php echo $pageTitle?>

asp

mipapage

12:14 am on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aspr1n
Thanks, maybe more sophisticated than I need right now, but certainly further down the road...

Birdman,

Something else learned this evening... Thanks!

Slade

12:16 am on May 15, 2003 (gmt 0)

10+ Year Member



I do a similar thing using ideas I stole from FuseBox.

index.php

switch($page)
{
case "": // if empty
case "main":
include('the_main_file.txt');
break;
default:
include('404.php');
// I don't know if a break is good form/required/optional or what, here.
}

Edit:
It's actually a lot more complicated now, but that's basically what it looks like.

You can also do neat pre/re-processing things with the output buffer functions ob_start, etc...

mipapage

11:18 am on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again to all..

One last comment/question, a bit off of the original topic..


Are there any different security problems WRT include Vs. Require Once, which is what I had looked at before?

We are using mod_rewrite to fix up our Urls, which will come out looking like normal .html files. We have taken a couple other steps in our .htaccess file in order to keep users in the main directory etc...


I have had a look at this [webmasterworld.com ]

[edited by: mipapage at 11:23 am (utc) on May 15, 2003]

Nick_W

11:22 am on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before you go much further I'd urge you to check this thread about Smarty Templates [smarty.php.net] out:
[webmasterworld.com...]

Nick

mipapage

11:55 am on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank Nick, great suggestion and I'm on it.

wrt to the above question, I am going to start a new thread [webmasterworld.com] as the topic is just too different....

[edited by: jatar_k at 4:19 pm (utc) on May 15, 2003]
[edit reason] fixed link [/edit]