Web Hosting Talk







View Full Version : About this Code


Einstein500
12-22-2005, 08:21 PM
Includes are the way to go. The three most common methods are PHP, ASP and SSI (though SSI is rapidly losing popularity, in my observation).

The whole idea of an include is easy. You put a single line of code into every page, like this:

<?php include "path/include_file.inc";?>

And then you create an entirely separate document with the information you want to insert, in your case, navigation. Now, when you want to make a site-wide change, you change that one file, and the whole site updates.

You can take this a step farther by putting includes inside included files. I'll personally create subsection navigation for a site, put each subsection in its own include file, and call it into a page for displayed navigation, DHTML menus, the domain's site map, etc., all controlled from a single source. It saves a TON of time, and you can put together a really slick system!

I have seen this posted alot, but where exactly would you put it?..does it matter or can it be in any place of the coding? Also, about that separate document you create, how do you set it up? Like how would it link?...Thanks for your time.

Einstein

Dan L
12-22-2005, 08:27 PM
The file needs to be a .php file, and you can put it anywhere. You usually place it where the content from the included file would go.

The separate document is just a normal file.

Say I had an about me page and wanted to include a header & footer.

index.php
<?php include 'header.html'; ?>
Hi! My name is Dan. I'm a programmer.
<?php include 'footer.html'; ?>

header.html
<html>
<body>

footer.html
</body>
</html>

When you're done, the resulting page will look like:

<html>
<body>
Hi! My name is Dan! I'm a programmer.
</body>
</html>

Does that make sense?

Einstein500
12-22-2005, 08:37 PM
Ya, sort of...I am still a little confused about where to put it.

DWS
12-22-2005, 10:10 PM
Ya, sort of...I am still a little confused about where to put it.
You'd put it inside a .php file.

Look at DanX's first example of code (index.php) - that is a small but complete file. You'd simply add more to yours.

To view the contents of that file, you'd go to http://yoursite.com/index.php assuming your server supports PHP (most do).

Dan L
12-22-2005, 10:17 PM
The code is like a substitution for other code. :)

In math, say you have something like

x = 1 + 2
y = 1 + x + 3

From that, you would make

y = 1 + 1 + 2 + 3

An include works the same way. x is the include and y is the initial page. You're simply taking content from one page and putting it in the other, exactly where the code is.