Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Website Design Forum


You are currently viewing our Website Design Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-11-2007, 02:00 PM Simple PHP Includes
Junior Talker

Posts: 18
Trades: 0
Scenario 1: You own a large website, with many pages of a LOT of content. For instance: updating links is a drag because you have to update EVERY single link on EVERY single HTML file. But...there is a much easier way to do this, I give you...PHP Includes!

Basically, what this does, is it will save you a whole lot of time everytime you want to change your layout or update things. No previous PHP knowledge is required, too. All you need is some HTML basics and you're ready to go.

Now into the tutorial. Once you've got your layout ready, open it in a text editor like Notepad. Here's what a basic layout may look like:

Quote:
<html>
<head>
<title>My site</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="0" width="90%">
<tr>
<td width="20%">
<p>Navigation</p>
<p><a href="#">Link here</a><br>
<a href="#Link here</a><br>
<a href="#">Link here</a><br>
...</p>
</td>

<td width="80%">

<!-- Here's where your content starts -->

<p>Hello, welcome to my site, here's where I put all my content =D</p>

<!-- Here ends your content -->

</td>
</tr>
</table>

</body>
</html>
Step 1:
Take this piece of code (or everything above where your content starts) and cut and paste it into a separte document. You should have something like this:
Quote:
<html>
<head>
<title>My site</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="0" width="90%">
<tr>
<td width="20%">
<p>Navigation</p>
<p><a href="#">Link here</a><br>
<a href="#Link here</a><br>
<a href="#">Link here</a><br>
...</p>
</td>

<td width="80%">

<!-- Here's where your content starts -->
Still with me, here? Okay, click Save As and name the file as header.php. Now back to the rest of the layout: take the bottom part of your design and cut and paste it into a separate document. It should look like this:

Quote:
<!-- Here ends your content -->

</td>
</tr>
</table>

</body>
</html>
Save this as footer.php. Now, let's get to the magic part...take your content, it can have ANYTHING you want in it. In this case, it would be:
Quote:
<p>Hello, welcome to my site, here's where I put all my content =D</p>
And add the bold:

Quote:
<?php include('header.php'); ?>

<p>Hello, welcome to my site, here's where I put all my content =D</p>

<?php include('footer.php'); ?>
Save this separately as index.php. Upload header.php, footer.php, and index.php to your host and it's all there! So let's say you make a typo on one of the links (it happens!), all you have to do is edit header.php (or wherever your links are) and fix the error, instead of all 50+ pages. Have fun with it!
James` is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-12-2007, 08:59 AM
imported_oddcomments's Avatar
Junior Talker

Posts: 53
Trades: 0
I second that!

If you are not using a CMS system, this is the easiest way to manage a site. I’ve been managing my non-CMS sites this way for the past seven or eight years, and it saves a ton of time. Over time you will even develop more sophisticated ways to manage this, like creating a dynamic breadcrumb trail of alternating secondary navigation based on the directory structure, etc.

My setup looked more like this:

PHP Code:
<? // index.php
    
$location $_SERVER['PHP_SELF'];  // Tells PHP the location of the page.  I use this to dynamically creat the navigation and breadcumb trail.
$title "This is the title of the page";
$descrip "The description goes here";
$keywords "Add keywords if you'd like";
$name 'Home Page';
$modsRght = array ("mod_dnld-cta.php""mod_news.php""mod_events.php"); //I create modules for commonly used items that you don't necessarily want on every single page.

require('default.inc'); 
    
?>

<p>Hello, welcome to my site, here's where I put all my content =D</p>

<? require($web_foot); ?>
From the “default” file you would include all of your logic and links/variables for your other templates. This way you have a separate page so you can keep your header template clean. I also like to have a unique Meta title and description for each page, so I include that before I call any includes from the page. Here’s what my other two pages would look like.

PHP Code:
<? // default.inc

// Other site logic and variables can go here.  I have about 500 lines of code.
    
$web_head 'head.inc';
$web_foot 'foot.inc';

require(
$web_head);
?>
PHP Code:
<? // head.inc

<HTML>
<
HEAD>
    <
TITLE><? echo $title " - " $sitename?></TITLE>
    <META NAME="Title" CONTENT="<? echo $title " - " $sitename;  ?>">
    <META NAME="Keywords" CONTENT="<? echo $keywords ?>">
    <META NAME="Description" CONTENT="<? echo $descrip ?>">
    <LINK REL="stylesheet" HREF="/lib/css/style.css" TYPE="text/css" MEDIA="print">
    <LINK REL="stylesheet" HREF="/lib/css/style.css" TYPE="text/css" MEDIA="screen">

</HEAD>

<BODY>

<!-- Etc, etc. -->


?>
imported_oddcomments is offline
Reply With Quote
View Public Profile Visit imported_oddcomments's homepage!
 
Old 01-12-2007, 10:12 AM
Hobbit's Avatar
Super Spam Talker

Posts: 823
Trades: 3
I use this all the time, so I know it's VERY VERY useful.
Hobbit is offline
Reply With Quote
View Public Profile
 
Old 01-13-2007, 04:29 PM
imported_babyboy808's Avatar
Junior Talker

Posts: 116
Trades: 0
Yeah, I am using this method the last 2 years or so...
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
imported_babyboy808 is offline
Reply With Quote
View Public Profile
 
Old 03-06-2007, 03:34 PM
Skilled Talker

Posts: 52
Location: Kelowna, BC
Trades: 0
Is there any benefit of doing it this way over using a template file? The way mentioned in this thread is how I've always created my "templates" but have more recently gone to using one .tpl file for the div layout/content output in addition to a .css file. I'm assuming there is no difference other then less includes files having to be opened reducing the associated overhead?
__________________

Please login or register to view this content. Registration is FREE
jacob is offline
Reply With Quote
View Public Profile Visit jacob's homepage!
 
Old 03-08-2007, 12:27 PM
TM.
TM.'s Avatar
$1,000 - $4,999 Monthly

Posts: 24
Trades: 1
It's a lot easier than any other method I find, and therefore quicker to code.
__________________

Please login or register to view this content. Registration is FREE
&
Please login or register to view this content. Registration is FREE
- Ajax Lookup tools.


Please login or register to view this content. Registration is FREE
||
Please login or register to view this content. Registration is FREE
TM. is offline
Reply With Quote
View Public Profile
 
Old 03-08-2007, 04:48 PM
johniman7's Avatar
President, JLI Media

Posts: 965
Name: John Irving
Trades: 0
I definitely agree with this. I have been using this method for a long time and it is a great way to make changes to navigation and such over a large range of pages.
__________________
Cheers, John Irving: My Blog
JLI Media:
Please login or register to view this content. Registration is FREE
| Website Development (Link Coming Soon!)
johniman7 is offline
Reply With Quote
View Public Profile Visit johniman7's homepage!
 
Old 03-13-2007, 06:28 PM
jdrobinson's Avatar
Registered User

Posts: 12
Trades: 0
I use this method on all of the sites that I develop and it saves me an extreme amount of time. After I create the initial template for the page I divide it into the relevant includes and then I can easily produce pages as I need them and all I have to worry about is the content.

Great post!
jdrobinson is offline
Reply With Quote
View Public Profile
 
Old 03-14-2007, 04:13 PM
Banned

Posts: 19
Trades: 0
Try using require_once() and include_once() instead of include()
This will prevent errors from multiple includes.
require_once() should be used for important code.
and include_once() for non critical (css, hmtl) code.
imported_bmr is offline
Reply With Quote
View Public Profile
 
Old 03-14-2007, 10:44 PM
Yeah I Do That

Posts: 41
Trades: 0
That is an amazing method that I have been using for years but I have started to use switches
__________________

Please login or register to view this content. Registration is FREE
.insane is offline
Reply With Quote
View Public Profile Visit .insane's homepage!
 
Reply     « Reply to Simple PHP Includes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30527 seconds with 12 queries