• Log in or sign up for an account here: https://www.sythe.org/login

{***++***Build your site with PHP***++***}

  • Thread starter Thread starter Jed
  • Start date Start date
J

Jed

Guest
I'll put the code in quotes so it'll be easer to read.

here is a small guide to writing your own simple website (not mysql based).

Key files:
index.php -> This is the key file, the layout itself is stored in here, generally consisting of a navigation bar and a 'main area' for the page content to go in.


urls.php -> First script called by index.php, it works out where you asked to be, based on the url (?Act=something), and sets pagetitle, (<title> tags) and A title for a bar on the page, if necessary. It also sets a third variable for use by later scripts.

navigation.php -> This page houses the code for the navigation bar, which is called by index.php at the appropriate point. It uses the third variable set in urls.php to work out where you have asked to be, and make the appropriate link (or image) no longer a link.

content.php -> The last script to be included by index.php, it performs a similar function to navigation.php, but instead of putting in code for navigation, it fetches the content. At this point instead of doing what I did, you could make it mysql based, and instead of just include()ing the files, you could use an sql query to fetch code.

So I take it you'd like some code too, huh?

I wont start with index.php, as it mainly requires knowledge of the other files first, lets begin with urls.php:

<?PHP
$etc = $_GET['Act']; //Transforms the ?Act=something into a variable
$etc = strtoupper($etc); //Makes that variable UPPERCASE
if($etc == ""){
$etc = "INDEX"; //Makes the index display on no etc
}
if ($etc == "INDEX") {
$content = "Index"; // Set the content for later parsing
$docutitle = "<title>Web page</title>"; //set pagetitle
$pagetitle = "Welcome to your site"; //Set in-page title
} elseif ($etc == "FORUM"){
header("Location: http://www.yoursite.com/forum");
} elseif ($etc == "About"){
$content = "aboutme";
$docutitle = "<title>About me</title>";
$pagetitle = "about me";
} elseif ($etc == "etc"){
$content = "etc";
$docutitle = "<title>etc</title>";
$pagetitle = "etc";
} elseif ($etc == "etc"){
$content = "etc";
$docutitle = "<title>etc</title>";
$pagetitle = "etc";
} elseif ($etc == "etc"){
$content = "etc";
$docutitle = "<title>etc</title>";
$pagetitle = "etc";
} else {
header("Location: ?Act=Index"); //If it doesn't recognise the page, send the user to the index
}
?>

A look at navigation.php:

<?PHP
if($content == "Index"){
echo "<td height='23'><img src='gfx/home.gif' border='0' alt='Home'></td>";
} else {
echo "<td height='23'><a href='?Act=Idx'><img src='gfx/home.gif' border='0' alt='Home'></a></td>";
}
if($content == "etc"){
echo "<td height='23'><img src='gfx/etc.gif' border='0' alt='etc'></td>";
} else {
echo "<td height='23'><a href='?Act=etc'><img src='gfx/etc.gif' border='0' alt='etc'></a></td>";
}

Pretty much continues the same, except for things like the forum button, which just echo()s regardless, as there isn't a page to display with them.

Content.php needs no explanation, but I will post it anyway:
<?PHP
if($content == "Index"){
require("main.htm");
} elseif ($content == "aboutme"){
include("about.htm");
} elseif ($content == "etc"){
include("etc.htm");
} elseif ($content == "etc"){
include("etc.htm");
} elseif ($content == "etc"){
include("etc.htm");
} else {
echo "An unknown error has occured<br />";
}
?>

I will post my entire index.php, to give you a better idea of what's going on:

<?PHP
require("urls.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<?PHP echo $docutitle ?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL=STYLESHEET HREF="style.css" TYPE="text/css">
</head>
<body>
<center>
<table class='Bodyholder' width='75%' cellpadding='0' cellspacing='0'>
<tr>
<td width='100%'>
<table width='100%' cellpadding='0' cellspacing='0'>
<tr><td class='topbar' height='25' width='650'><center><?PHP echo $pagetitle ?></center></td></tr>
<tr><td width='650' height='100' valign='top' class='logostrip'><img src='gfx/logo.jpg' alt=''></td></tr>
<tr><td class='topbar' width='650' height='25'>
<center>
<table cellpadding='0' cellspacing='0' class='navbar' align='center'>
<tr>
<?PHP require("navigation.php") ?>
</tr>
</table>
</center>
</td></tr>
<tr>
<td class='bodymain'>

<?PHP require("content.php") ?>

</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table><br />
</center>
</body>
</html>

This is just something I knocked forgive me if it's not too good.

---

Please don't let my post count fool you, I been here but never posted.
 
Your urls.php is just plain too bulky. When (if) the site gets big, the urls.php will be overcrowded, and simple errors such as parse errors will pop up here and there because someone forgot to escape a quote, or left out a bracket, etc. I'll show you how I do it.

url.php:
Code:
<?php
$extension="php";
$pdir="/pages/";
if(!isset($_GET['act'])) {
include($pdir.'indexbody'.$extension);
}
elif(file_exists($pdir.$_GET['act'].$extension)) {
include($pdir.$_GET['act'].$extension);
}
else {
echo('404!');
exit;
}
?>

and the 'layout' would be this
index.php:
Code:
<table>
<tr>
<td>
<a href="?act=about">about</a> | <a href="?act=credits"> Credits</a>
</td>
</tr>
<tr>
<td>
<?php
include('url.php');
?>
TEH FOOTER
</td>
</tr>
</table>

Certaibly not the best layout, but it gets the job done. Hope there are no errors, I just wrote this up quickly.
 
wouldn't it be better to leave the page itself to CSS and XHTML
 
wouldn't it be better to leave the page itself to CSS and XHTML

You obviously have never worked with sites with 100+ pages of content.

And this tutorial isn't about css + xhtml. This tutorial is about php. The css and xhtml is the design, the aesthetics of the website, which aren't meant as the main point of the tutorial.
 
Runescape Gamblingrunescape gambling
Back
Top