You are here: Home > Useful Guides > Step 3) Creating The Files - Part 3

Step 3) Creating The Files - Part 3

So far we've got our template files laid out. We've got a sparse CSS file and we've got the basic pages required, empty but ready to roll.

The first thing to do is fill in the static files: terms, privacy, disclaimer, contact, about.....

You can do this following this file structure:

<?php
$pagetitle="Page title goes here";
$pagekeywords="list, keywords, here";
$pagedescription="Write a page description here";
include "includes/head.php";
echo "<h1>".$pagetitle."</p>";
echo "<p>Page text goes here</p>";
include "includes/foot.php";
?>
$pagetitle is used to define the H1 tag on the page along with the title tag in the head section on the html. $pagekeywords lends itself nicely to the keywords tag and thus the description tag also works in the same manor.

The only PHP files that should now be empty are index.php, recipe.php, category.php and rss.php. Before we fill them, let's sort out htaccess.txt and robots.txt.
 
.htaccess

In htaccess.txt, paste:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteBase /
RewriteRule ^(.*).html$ $1.php [L]
Every server is different so your server may handle this differently. For me, it redirects all ".html" queries to a physical ".php" page on the server. The first line also says to redirect (ultimately to 404.php) if a 404 error occurs.

Upload to your server and rename to ".htaccess". If you get stuck, try this htaccess guide.

robots.txt

A robot checks for a robots.txt file before following a link. Robots.txt is designed to list all the negative links - i.e. all the links not to follow. As I don't have any links that point to secret locations (e.g. an admin panel), I could leave it empty.

I'm going to build a "go" script so my file will look like:
User-agent: *
Disallow: /go.html
Disallow: /go.php
I've put both variations of the page in there just in case. If you get stuck, check this robots.txt guide.

rss.php

Nice and easy file this so it makes sense to start here. As the site isn't large, I'm serving this direct from the database. If the site was larger it may make more sense to create a static XML file every hour or day using a cron job.

The code required is:
<?php
header("Content-type: text/xml");
include "includes/connect.php";
$sql1 = "SELECT id,name FROM recipe ORDER BY date DESC LIMIT 10";
$result1 = mysql_query($sql1) or die(mysql_error());
$rowcounter1 = mysql_num_rows($result1);
echo "<?xml version='1.0' encoding='iso-8859-1' ?>";
echo "<rss version='2.0'><channel>";
echo "<title>Latest Recipes From My Recipe Site</title><link>http://www.mydomain.com/</link><description>Get
baking with our 10 latest recipes</description>";
if ($rowcounter1 > "0")
{
while ($row1 = mysql_fetch_array($result1))
{
extract($row1);
echo "<item><title>".$name."</title><link>http://www.mydomain.com/recipe-".$id."-".strtolower(str_replace("
","-",$name)).".html</link><guid>http://www.mydomain.com/recipe-".$id."-".strtolower(str_replace("
","-",$name)).".html?q=".$id."</guid><description><![CDATA[<p>Discover how to cook a ".$name."</p>]]></description></item>";
}
}
echo "</channel></rss>";
?>
Here's the walthrough:
  • The header tag tells the browser to handle the page as an XML file.
  • Our connect file is called, connecting the page to the database.
  • The MySQL used selects the 10 latest recipes based upon a chronological order
  • The basic RSS header information is output, followed by a recursive script that outputs the specific RSS items.
The url format I've decided upon "url/recipe-id-name.html". I could have gone for "url/recipe-name.html" but that would have been more time consuming so for simplicity, I'm making life easier for myself :-)

index.php

Ultimately all we need from the database is the latest 5 or so recipes. This will be used to create the featured recipes section.

The MySQL required is identical to that used above, except for the limit.

Remember to include the connect file in index.php.

recipe.php

This is a little tricky as we need to get the id from the url. The title could be used to make extra sure we have the right article but so long as the ID is numeric and exists, that shouldn't be necessary in most builds.

Firstly, I've changed .htaccess to include the line:
RewriteRule ^recipe-(.*)-(.*).html$ recipe.php?recipe=$1 [L]
This enables the fancy URL structure discussed in rss.php.

Here's the code for this page:
<?php
include "includes/head.php";
include "includes/connect.php";

$recipereference=explode("-",$_GET['recipe']);
$recipeid=$recipereference[0];
if (!is_numeric($recipeid)) {$recipeid=0;}

$sql1 = "SELECT * FROM recipe WHERE id='".$recipeid."' ORDER BY date DESC
LIMIT 6";
$result1 = mysql_query($sql1) or die(mysql_error());
$rowcounter1 = mysql_num_rows($result1);
if ($rowcounter1 == "1")
{
extract(mysql_fetch_array($result1));
echo "<p>".$name."</p>";
} else {
echo "<p>Sorry, we cannot find that recipe.</p>";
}
include "includes/foot.php";
?>
Remember to add the template variables.

category.php

This will resemble recipe.php, except for the fact that you are searching for "category" rather than "id". You'll also need to think about your URL structure. Mine will be "url/category-categoryname.html" because of the lack of recipes. However, bear in mind that you may need to look at php pagination if you were looking at creating a larger site based on this script.

Where next?

I'm off to tidy up what I've got, pad it out a bit and tart up the CSS file. The next article looks at the snazzy side of the site, so it's important to have all the basic groundwork sorted before you carry on.

Bookmark Bookmark this article in Bumpzee, Del.icio.us, Digg and Stumble Upon
Technorati Technorati tags: , , ,

You may also be interested in reading:

CommentsCare to Comment?

Comments are manually approved and hence can a while to appear. Questions, informative posts, and feedback comments are gladly accepted. Spam is deleted. Spam-type comments have their links removed (Comment Policy)