Zymic Forums

Webmaster resources

Zymic IRC Server

Chat in real time at irc.zymic.com - Learn More

Welcome

Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.

In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.

left Zymic Webmaster ForumsZymic Free Web HostingTutorials right
left right
IamShipon1988
post Dec 4 2007, 11:50 PM
Post #1


Outrageously Uber Ninja
*******

Group: Moderators
Posts: 1,562
Joined: 19-September 07
From: Rochester, NY
Member No.: 86



Title: php Includes
Language: php, XHTML, CSS
Skill Level: Basic
Author: Sazzad Hossain
Demo: http://www.amuz.us

Authors Note: PHP is perhaps one of the simplest language you can earn that can grant you much freedom with your site. In addition, it can also make your life a lot easier. If you are a web-designer you will see why. First of all, php has many functions that allow you to substitute for a long list of codes. For example, in this tutorial I will talk about php include. What php include basically does is takes an entire page of coding from one location and brings it to the page where you requested it. So if I have a head section located in "/include/head.php" and I want it to show up in my index.php file; all I have to do is call for it using php include.

So whats an advantage of include?
If you want to modify your site, and you have tons of page, and do not want to modify each one of them separately; when you use php include, all you have to modify is the file your including. In this example, all you have to do is modify the header.php and footer.php (also the style.css) that will change the layout to whatever you wish. It sort of acts as an StyleChanger effect but it requires a bit more work.

Step 1: Create the file you want to call for. Example, this header file: "includes/header.php." So we have these codes for the header file:
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>

    </title>
    <link rel="stylesheet" type="text/css" href="includes/style.css" />
</head>
<body>
<div id="holder">
<!-- holds the title and the navagation bar -->
<div class="header">

    <!-- holds the logo -->
    <div id="logo">
        <img src="../images/logo.idontknow" alt="RIT Web Dev Logo" />
    
    </div>

    <div id="nav">
    <br />
        <a href="index.php" alt="home">Home</a>|
        <a href="forum.php" alt="forum">Forum</a>|
        <a href="members.php" alt="members">Members</a>|
        <a href="about.php" alt="about">About</a>|
        <a href="contact.php" alt="contact">Contact</a>
    </div>
    <hr />
    </div>

Step 2: Since we will not have the ability to edit this page for the pages that will have a different title, we will need to add a variable that will allow us to accomplish that. So in order for us to give custom title to our main pages, we will need to replace the title tags with the following:
CODE
<title>
<?php
    echo $title;
?>
</title>

Step 3: Now lets create our footer file (you can create a sidebar, but I'm skipping that for the sake of time). We don't have to make any changes in this file unless you want to include something else from another location. Lets call this file footer.php and put it also in the includes folder.
CODE
    <hr />
    <div class="copyright">
        © 2007 RIT Web Developers, All Rights Reserved <br />
            <a href="contact.php"> Contact Us </a>
    </div>
</div>
</body>
</html>

Step 4: Okay, since we are now done with all the files we want to include in our index.php file, lets create the most important file: index.php. A general HTML file without header and footer will look something like this:
CODE
<div id="content">
    <h1> Content Title </h1>
    <p>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce quis ante.
    Ut sit amet arcu eu lacus sodales egestas. Sed metus risus, placerat vitae,
    molestie sit amet, consequat eu, erat. Quisque tincidunt, urna sed
    pellentesque adipiscing, risus nulla congue urna, et pharetra tortor massa
    in leo. Cum sociis natoque penatibus et magnis dis parturient montes,
    nascetur ridiculus mus. Quisque tellus. Phasellus iaculis lacinia diam. In
    nonummy, tortor id rutrum fermentum, metus lacus ultrices turpis, sit amet
    hendrerit pede leo vitae neque. Aenean lacinia metus eu tellus. Quisque
    tempor dolor at velit. Etiam luctus fermentum justo. Pellentesque erat
    mauris, consectetuer a, commodo at, ultrices vel, lacus. Proin sit amet
    leo. Vivamus sit amet tellus.
    </p>
</div>

But that does not include our header.php and footer.php. So we need to call for it. We can accomplish this by using 3 lines of php codes. So above all the codes in our index.php code, we want to include our header. We can do this by pasting the following code:
CODE
<?php
    include("includes/header.php");
?>

Step 5: Remember when we added echo $tittle; in our header to allow us to give the page custom title? Well this is the additional work needed to accomplish that. Before the area where it states include("includes/header.php"); we will add the following line of title:
CODE
    $title = "Page 1";

Simply replace the Page 1 with your own page title.
Step 6: Now its time for us to call our footer. This can be accomplished the same way as in step 4. Simple paste the following code towards the bottom of the index.php file.
CODE
<?php
    include("includes/footer.php");
?>


Well your done! What you have to note is that you can place footer.php and header.php in any folder you want, just make sure you call it correctly. Many php programmers make that mistake, thereby end up spending hours finding the problem, although the problem is simple. Just as a word of warning/caution Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function.

If you put all these codes together, you should get a page similar to the demo page (unless I end up modifying the page). If you want to use the same css, then here it is:
CODE
.copyright {
}
.header {
    border: thick #000000;
    text-align:center;
}
#holder {
    margin: auto;
    width: 700px;
}
#logo {
}
#main {
}
#nav {
}
/***************
    Links
***************/
a:hover {
    text-decoration:underline;
}
a {
    text-decoration:none;
    color:#0066CC;
}


Cool Extras:
- PHP Date & Time function
Go to the top of the page 
 
  + Quote Post
 
Start new topic
Replies
Nexus
post Dec 23 2007, 03:00 PM
Post #2


Super Ninja
****

Group: Members
Posts: 384
Joined: 19-September 07
From: New Zealand
Member No.: 110



This is a nice tutorial. I found about the php include and require/requie_once scripts by trial and error myself lol. That's the only bit of PHP I know at the moment.
Go to the top of the page 
 
  + Quote Post

Posts in this topic
IamShipon1988   Php Include Explained   Dec 4 2007, 11:50 PM
Bread   Thanks for taking the time to write this IamShipon...   Dec 5 2007, 01:07 AM
MrTouz   basically include = iframes its installing an ext...   Dec 5 2007, 10:59 AM
IamShipon1988   Bread Thank you for your comment. Yes it is true t...   Dec 6 2007, 08:11 PM
MrTouz   Yes, an include has to be loaded on each page, but...   Dec 7 2007, 11:13 PM
IamShipon1988   Well I'm really glad to hear that. Includes is...   Dec 10 2007, 09:05 PM
Paranoid   Includes let me update my new layout/update large ...   Dec 12 2007, 02:30 AM
eJGDesigns   How do i do this what you have done? Thanks   Dec 13 2007, 08:45 PM
IamShipon1988   eJGDesigns, php includes does this automatically a...   Dec 14 2007, 10:15 PM
doughnut   I Love PHP includes="file.xxx" lol :)   Dec 15 2007, 02:44 AM
IamShipon1988   I Love PHP includes="file.xxx" Me tooo...   Dec 15 2007, 06:28 AM
doughnut   Me tooo!!! Before in c, you had to do ...   Dec 23 2007, 01:35 PM
Eibwen   You also can do require_once "file.xxx";...   Dec 18 2007, 11:26 AM
IamShipon1988   Well yes you can use require_once, but sometimes p...   Dec 19 2007, 06:37 AM
Nexus   This is a nice tutorial. I found about the php inc...   Dec 23 2007, 03:00 PM
doughnut   This is a nice tutorial. I found about the php inc...   Dec 23 2007, 03:08 PM
IamShipon1988   Nice advertisement doughnut. Don't worry I won...   Dec 24 2007, 07:09 AM
doughnut   Nice advertisement doughnut. Don't worry I won...   Dec 24 2007, 08:08 PM
IamShipon1988   Yes they are. I'm in the process of renovating...   Dec 24 2007, 11:14 PM
doughnut   I did not register for the site because you are us...   Dec 25 2007, 04:02 PM
IamShipon1988   Registered on your forum. I'm going on vacatio...   Dec 25 2007, 10:20 PM
doughnut   Registered on your forum. I'm going on vacatio...   Dec 26 2007, 08:24 PM
IamShipon1988   Haha I forgot I registered on your forum, anyhow t...   Dec 28 2007, 01:44 AM
smartalco   I'm not particularly new to php, however, unti...   Jan 20 2008, 08:37 PM
Alex   We need to see the whole file, likely the syntax e...   Jan 20 2008, 08:40 PM
smartalco   ya... just figured it out, left out a semicolon on...   Jan 20 2008, 08:53 PM
IamShipon1988   Hehe...good work on solving it. This is something ...   Jan 26 2008, 11:57 PM
Vanumi   Hello! I'm new here in ZYMIC. I wanted as...   Jan 31 2008, 11:06 AM
Alex   If it's in the same directory you don't in...   Jan 31 2008, 05:52 PM
Vanumi   Thanks, Alex! include 'file.php' didn...   Feb 1 2008, 08:12 AM
IamShipon1988   It's a bit awkward that 'file.php' did...   Mar 3 2008, 04:24 AM
omerts   Thank you for the information it was very helpful   Mar 4 2008, 10:15 AM
IamShipon1988   Thank you for the information it was very helpful ...   Mar 13 2008, 03:35 PM
Alex   Me tooo!!! Before in c, you had to do ...   Sep 30 2009, 09:33 PM
IamShipon1988   In the later version of C #include is usable. Howe...   Oct 1 2009, 03:21 AM
machiko99   complete and clear...thank you boss   Dec 30 2009, 01:17 PM
noetfd   I need to recode my site after reading this haha, ...   Sep 29 2010, 12:44 PM
shans2213   Nice shared.. Thanks.   Oct 13 2010, 10:33 AM
IamShipon1988   I'm happy to see that this tutorial has helped...   Oct 25 2010, 02:01 AM

 Reply to this topic Start new topic
left right
0 Members:
left right
 


Lo-Fi Version Time is now: 25th May 2013 - 11:45 PM