The Pokémon Peninsula
Today is -:- -:- Release Dates: soon (JP) -:- soon (US) -:- soon (EU) -:- soon (AU)
.rxncommunity: RyuX Network Forums | RXN Top 50 | Pocket Power Network | RXN Chat

Layout: You are currently using Winter Stars | Switch to Castform Sunrise

-:- -:-The Pokémon Peninsula: Winter Stars -:- -:-

The Pokémon Peninsula - Webmaster Tutorials - PHP Includes: Including Content

The Pokémon Peninsula > Webmaster Tutorials > PHP Includes: Including Content

Using PHP Codes for Website Layouts

by Jedi_Amara

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]<?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;
}

?>
[/php]

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]<?php[/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 "?>".

[php]$news = "news.php";[/php]
The $ at the beginning of "$news" tells the server that $news is a string variable, meaning it can include letters and numbers. 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.

[php]$error = "404.htm";[/php]
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.

[php]$ext = ".htm";[/php]
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.

[php]if(!isset($_GET['id'])){[/php]
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.

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

[php]}[/php]
So that part of the function is finished.

[php]elseif($_GET['id'] == "main") {[/php]
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.

[php]include $news;
}
[/php]
Again, same as before.

[php]elseif(isset($_GET['id']) && file_exists($_GET['id'].$ext)){[/php]
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:

[php]include $_GET['id'].$ext;
}
[/php]
It includes the file named pagename.ext into the page, and closes off the function with the curly bracket.

[php]else{
include $error;
}
[/php]
If none of the previous parts of the if/elseif/else statement return true, then it includes the file not found error page.

[php]?>[/php]
Close off with the PHP closing tag.

4. Now, you need to make your content pages [b]without the layout[/b]. 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 "index.php?id=hello" and it will include "hello.ext".
- If it's in a subfolder, link to "index.php?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

 


Content © 2001-2005 to The Pokémon Peninsula. All rights reserved.
TPP does not own or claim to own Pokémon or any associated names or insignia; this is a fan site only.
Recommended resolution: 800x600. Colour: 16-bit+. CSS, images on. Tested in IE, Firefox, Mozilla and Netscape. Report Broken Link
♥ I won the layout from PSB
Topsites
Pokemon Top 500

TPP Main
Splash Screen
News and Updates
PocketNews
About TPP
Link to Us
Link Exchange
Official Links
Affiliate with Us
Contact Us
Awards We've Won
Win an Award
Support TPP
FAQs
Legal Stuff
Special Thanks
RXN Forums
STAFF
Site Staff
Job Openings
Staff Login

-:- click to expand a heading -:-

Features

General

Pokédex

Pokémon TV
Indigo League
Orange League
Johto Journeys
Johto League Champions
Master Quest
Advanced (Generation)
Advanced Challenge

Movies
Mewtwo Strikes Back
Mewtwo Returns
The Power of One
Spell of the Unown
Celebi: The Voice of the Forest
Pokémon Heroes
Jirachi: Wish Maker
Destiny Deoxys
Lucario and the Mystery of Mew

Games
GameBoy/Color/Advance
Red/Blue/Green
Yellow
Gold/Silver
Crystal
Ruby/Sapphire
Fire Red/Leaf Green
Emerald
GB/C/A RPG GUIDES
Puzzle Challenge
TCG
TCG II
Pinball
Pinball RS
Nintendo DS
Diamond/Pearl
Dash
Nintendo 64
Stadium
Stadium II
Snap
Puzzle League
GameCube
Channel
Box
Colosseum
Pokémon mini
Console
Party
Pinball
Zany Cards
Puzzle Collection
Untranslated Games

TCG
Rules
Tournaments
Base Set
Base Set 2
Jungle
Fossil
Team Rocket
Gym Heroes
Gym Challenge
Neo Genesis
Neo Genesis 2
Expedition
Aquapolis
Sandstorm-ex
Caravan-ex
Strategy

Music

Merchandise
Videos
Mangas
Novelisations
Annuals
Pikachu Mug

Media
IL Gallery
OL Gallery
JJ Gallery
JLC Gallery
MQ Gallery
AG Gallery
AC Gallery
MIDIs
Video Clips
Audio

Fan Stuff
Fanfics
Fanart
Jokes
Sprite Edits
Screen Mods
Fake Cards
Fake Pokémon

Downloads
Programs
Fonts
Sprites
Wallpapers
Winamp Skins
ROMs/Emulators

Tutorials
Sprite Editing
Trainer's Eyes
Battle Screens
Pokémon Stats Screens
Pokémon Move Screens

Just For Fun

RM2K/3
Facesets
Charsets
Chipsets
Game Over Screens
Title Screens
System Graphics
Games

Webmasters

Saybox

Partners
Patamon's World
Pokémon RM2K
Animeopolis
gallery@ryux
PocketNews
RyuX Network Forums
Affiliates
Pokemon Palace Network
Scyther's Pokemon Palace
The Cave of Dragonflies
PokéDen
Pokémon Fever Network
Pokemon OnLine
Hosted By
JKaizer.Net
Hentai-Free
TPP is proudly hentai-free!
Networks
Pocket Power Network
Hentai-Free Network
Pokemon Cult
Top-Site Awards
Statistics


Support TPP

Bravenet Signup
Buy a Domain
Click to Support
Buy a Toy

Search the Web:


Webmasters make money!