You are here: Home > Useful Guides > Step 5) Adding Functionality
Monday, 29 September 2008
Okay, back to the tutorial. Before we go any further, take a look at my chocolate recipe website (told you it was a pants domain name!) and have a good nosey at it. It's built using the tutorials as a basis although I've glossed it up only very slightly.
For prosperity, here's a screenshot:

So let's now take a look at what functionality I added.
Useful Affiliate Ads
Underneath each recipe you'll find a link to MySupermarket and Tesco:

Chocolate recipes is just one step away from chocolate gifts so you'll find a few banners for chocolate gift retailers.
Handy Printable Guide
Hit "print preview" on a recipe page and you'll see a handy cut down version that 'should' fit on one page containing just a picture, the ingredients and the recipe itself. This is created by calling a new stylesheet only for printers.

Categories
We can pull up recipes by category which means that visitors can instantly and easily find what they are after.
There's a few more directions the site could go but that is to be discussed in the final article in this mini-series.
You may also be interested in reading:
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)
@Chris - Apologies for skimming over this.
The page I'm using is very easy. First of all, we need two queries.
The first outputs all the categories using the SQL code:
SELECT category FROM recipe GROUP BY category ORDER BY category ASC
This selects all the categories from all the recipes. The GROUP BY command groups all the same one's together as we only need one of these to appear. The ORDER BY clause sorts the output into alphabetical order.
The next query looks for a variable - let's call it $selectedcategory.
$selectedcategory is the category selected. So when you click on cake, I've set $selectedcategory="cake";
The next query looks like this:
SELECT id,name,summary FROM recipe WHERE category='$selectedcategory' ORDER BY name ASC
You then output the result of this.
Remember that $selectedcategory (in this case) is a $_GET variable and so could be used by attackers to hijack or hack into your site. Any variable that is passed from page to page should be subjected to a series of validation clauses such as strip_tags - http://www.php.net/strip-tags
Let me know if you need any more help :-)
Written on Monday 29 September 2008 at 15:02:16 GMT (Permalink)
Ahh ok i'm understanding more now you've mentioned the selectedcategory when it's clicked.
I best lookup the GET variable as this is what's confusing me as to actually use it.
Just a thankyou for this series it's been great reading.
Written on Monday 29 September 2008 at 19:34:51 GMT (Permalink)
@Chris - Set up a blank php file. In it, paste this code:
<?php
echo $_GET['a'];
?>
Upload it to your server and run the file. It *should* be empty. Now run it but append ?a=hi to the URL:
e.g. file.php?a=hi
What you should see is the word "Hi". The GET variable allows you to pass bits of data from page to page or within a script. The above example shows it works with categories but it could work for all manor of this.
The POST variable (sending form data to another page) is considered more secure in that there's less of a chance of it being maliciously edited but it is possible so remember to validate the string (GET or POST) before letting it loose near a database.
Written on Tuesday 30 September 2008 at 10:15:30 GMT (Permalink)
Chris
Hello David,
How exactly do you create the category pages (like a page from the database) ?
I've been learning php/mysql from your tutorials and other places online and this is the one thing i don't quite get.
Written on Monday 29 September 2008 at 13:31:48 GMT (Permalink)