View Full Version : Basic PHP
Brother
02-18-2006, 08:27 PM
I've just started learning basic PHP so bear with me.
Say for example I have four pages on my website: Home, Staff, About Us and Apply and these are arranged together in my navigation on the left. If I want to add a new page (e.g. Links) with HTML I'd need to add the new link to every page right?
How does PHP make this whole process easier? How do I set up my navigation so that I only have to edit at the source and it will appear updated on every page, instantly? I appreciate that this may be a really simple thing, but I haven't been able to purchase a decent book yet, and I hate reading online tutorials. If anyone does have any links though it wouldn't be such a bad thing.
There are two effective ways of creating the effect you wish to accomplish.
The method I prefer to use is to have only one page with the layout that loads in the content from other files. I'd suggest putting the content pages inside of a separate, specific folder to prevent people from trying to load in stuff they aren't supposed to. ;)
For instance, set up your links like this:
<a href="http://yourdomain.com/page.php?content=home">Home</a><br />
<a href="http://yourdomain.com/page.php?content=staff">Staff</a><br />
<a href="http://yourdomain.com/page.php?content=aboutus">About Us</a><br />
<a href="http://yourdomain.com/page.php?content=apply">Apply</a><br />
Then, in place of your content, include this code:
<?
if(isset($_GET['content'])) //make sure they specified a page
{
include("http://yourdomain.com/content/" . $_GET['content'] . ".php");
}
else //if not, load a default page
{
include("http://yourdomain.com/content/home.php");
}
?>
Then save the content for each page in a .php file with the correct name and upload it to the content folder.
It's a little work to setup, but then you can easily add new content pages by adding an extra .php file to your content folder and adding in the link on your one layout content page.
---
The second method (which I believe the FFI main site uses) is to have each file include a common functions.php file that has functions that display parts of the layout.
For instance:
<?
require("functions.php");
display_header();
?>
<h1>Content</h1>
Content, content, content, blah, blah, blah...
<?
display_footer();
?>
...and, in functions.php, setup display_header() and display_footer() (or whatever you want to call them) to display the respective parts of your layout.
Hope that helped. :D
This is a bit the same as Frag posted, but maybe you'll find this easier to understand. In your index.php you write
<a href="?page=home">Home</a>
<a href="?page=staff">Staff</a>
<a href="?page=aboutus">About Us</a>
<a href="?page=apply">Apply</a>
and were you want this text to come (in a table, div, ...) you write:
<?php
switch($_GET['page'])
{
case "home": include("home.php"); break;
case "staff": include("staff.php"); break;
case "aboutus": include("aboutus.php"); break;
case "apply": include("apply.php"); break;
default: include("home.php");
}
?>
That's all.
Ariel
02-19-2006, 10:52 AM
You generally don't need to use the .php extension for your content files unless you plan on having .php code within the content (though if the file has been called in through an include(); some servers will completely refuse to parse ANY php within the included file. I do believe you could probably get away with .html or .txt
Anyway to consolidate everything into a rather basic non-formatted simple tabular HTML page:
<html>
<head>
<title>Your page title</title>
<!-- All your META data and crap here -->
</title>
<body>
<table>
<tr>
<td>
<a href="http://yourdomain.com/page.php?content=home">Home</a><br />
<a href="http://yourdomain.com/page.php?content=staff">Staff</a><br />
<a href="http://yourdomain.com/page.php?content=aboutus">About Us</a><br />
<a href="http://yourdomain.com/page.php?content=apply">Apply</a><br />
</td>
<td>
<?
if(isset($_GET['content'])) //make sure they specified a page
{
include("http://yourdomain.com/content/" . $_GET['content'] . ".php");
} else //if not, load a default page
{
include("http://yourdomain.com/content/home.php"); }
?>
</td>
</tr>
</table>
</body>
</html>
Also note that "content" can be changed to whatever you want, so if you want it to be page or whatever you can have it as that.
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.