Site Stuff
Home
Links
Linkback
Forums

About Me
Profile
Contact
My Sites
LiveJournal
Wishlist
My CD Collection

My Pages
LiveJournal
Iconjournal
deviantART
FanFiction.Net
Egopedia
MySpace
Band MySpace

Creative Force
Writing
Art
Avatar Gallery
Signature Gallery

Shoutouts
Fanlistings
Bands

Webmaster Stuff
Free Layouts
Free Scripts
Tutorials

Affiliates

Networks/Topsites



Stats
226 hits

Using PHP Codes for Website Layouts

Part 1: Including Content

EXPECTED KNOWLEDGE: HTML. PHP knowledge not required. Knowledge of includes (eg SSI, PHP includes) would be nice, but not absolutely necessary.

People in WD have been requesting a tute. Here is your tute. :P This first part details how to use PHP codes to include the content of a page into your layout.

1. Create the layout for your website. Add the links for all the pages etc, but don't link them yet. You can link all the external stuff. Leave a placeholder where you'll put your content so you know where to put it later. I'll show you what to put in that in a bit.

2. Decide what you want your variable name in the pages to be. Your links to the page will be in the form:
index.php?variablename=pagename
where index.php is the main page of your site, variablename is the variable name you've chosen and pagename is part of the filename of that particular page.

3. Here's the basic code for content inclusion. This code looks a little more complicated than some of the others that other sites use, but it works with both PHP 4 and PHP 5, with globals turned on or off. PHP 5 was recently released and its security fixes break some of the include codes used by other sites.

BASE CODE:
<?php

$news = "news.php";
$error = "404.htm";
$ext = ".htm";

if(!isset($_GET['id'])){
include $news;
}
elseif($_GET['id'] == "main") {
include $news;
}
elseif(isset($_GET['id']) && file_exists($_GET['id'].$ext)){
include $_GET['id'].$ext;
}
else{
include $error;
}

?>


Wow, that looks complicated, you might be thinking. Sure, so we'll go through it bit by bit so you understand what each bit does and you can modify it to fit your own site.

<?php
This is just the PHP opening tag. You'll see it's a little different from HTML tags because it doesn't have a closing ">". PHP is enclosed in what's called PHP tags, where you open the code with "<?php", type all the code in and close the code with "?>".

$news = "news.php";
The $ at the beginning of "$news" tells the server that $news is a variable, meaning it can store letters and numbers. In this case, we're making $news a string, but we don't have to say so because PHP is "loosely typed" - it automatically picks up whether a variable is a string of text or a number. The single = sign sets the value of the $news variable to what comes after it, in this case what's enclosed in the quote marks: news.php. You need to change this to the path of the news file in your site, relative to the index.php page or whichever page has the include code in it. For example, "news/news.php", "news.php", "/news/news.php" - you should know how to do this. It's just the same as relative hyperlinks. Your newsfile's filename will depend on how you do it - if you use something like Coranto or CuteNews, it might create a file named "news.php" or "news.txt" in the folder you set it to, or you might have written your own news file. The semicolon at the end tells the server we've finished with this bit of the code.

$error = "404.htm";
Just the same as the line before, except that this is the address of your "file not found" error 404 page. It can be htm, php, txt, whatever. Standard include stuff.

$ext = ".htm";
You should give all your pages besides the news page and 404 page the same filename extension, eg. htm, php, html. Set it here, with the "." before it.

if(!isset($_GET['id'])){
This looks weird, doesn't it? Well, it's a simple "if" statement. It tests to see if the condition in the brackets immediately following the brackets is true, and if it does it carries out a function.
- $_GET['id'] retrieves the value of the "id" variable, where "id" is your variable name from step 2 in this case.
- !isset($_GET['id'] means that the id variable isn't set, ie. someone's just gone to "index.php" with no "?id=" after it.
- The whole if statement is testing to see if the id variable isn't set.
- If it's not set, then it carries out the function. This function is started by that curly bracket at the end of the line there.

include $news;
If there's no id set, it assumes you want the news page, and it includes that news page that you set in $news.

}
So that part of the function is finished.

elseif($_GET['id'] == "main") {
If the first "if" bit returns false, then it moves down to the first elseif, if there is one. This is like another if statement tied into the first one, and the double == is checking if the id variable's value is "main". If it is, then it performs the action defined after it, same as just before.

!!
This is just something I use on my site. If you don't need it, or your "main" page is different from your news page, then delete everything from the line starting "elseif" to the line with a single "}" after it. That's three lines.

include $news;
}

Again, same as before.

elseif(isset($_GET['id']) && file_exists($_GET['id'].$ext)){
This is the next "elseif" bit. It checks to see whether the id variable is set, and whether a file called "pagename.ext" exists relative to that place on the server - where pagename is the value of the id variable, and ext is that extension you set at the beginning of the code here. If this returns true, then it carries out the function:

include $_GET['id'].$ext;
}

It includes the file named pagename.ext into the page, and closes off the function with the curly bracket.

else{
include $error;
}

If none of the previous parts of the if/elseif/else statement return true, then it includes the file not found error page.

?>
Close off with the PHP closing tag.

4. Now, you need to make your content pages without the layout. In other words, just type the content into them. (Yeah, all the HTML formatting stuff too, but not the navigation etc.) Name each file with the extension you set in your PHP code.

5. Now we'll learn how to link to a page. Say the page with content is named hello.ext (where ext is that extension again)
- If the page with the content is in the same folder as the page to the layout (remember we're assuming the layout is index.php) then link to "?id=hello" and it will include "hello.ext".
- If it's in a subfolder, link to "?id=foldername/hello" and it will include "foldername/hello.ext".

Do the same if it's more folders down.

If you're linking from outside, link to "http://www.mysite.com/index.php?id=hello" etc.

Hope that helps, if you have any difficulties or there's something you don't understand just email or PM me and I'll try to clear it up.


Part 2 will be Including Navbars, which is the alternate (though not as good) way to do it. Watch for it whenever I get around to it :P