Wednesday, October 25, 2006

5 Gems I have discovered

Apart from writing loads of code in PHP as you can see from my previous posts, there is nothing better than writing code while having a glass of good red wine to go with it. Hey, red wine goes with just about anything, reading a book, chilling beside the fireplace, having dinner or whatever.

If you are like me and have evolved to the point where you realise it is OK to have red wine with seafood, you would have noticed that lately it has become pricey to support a nice wine collection.

Over the past few months I had to be pretty careful with what wines I buy, I love more classical heavy red wines, and for some reasons more and more sweet, light fruity redwines are appearing on the shelf, and I cannot afford to purchase a bottle of Raka everytime I am looking for a good wine, so I started the experiment.

The five wines I have listed above are all available from Pick and Pay (Believe it or not) for R 35 or less, most of them signifigantly less.

I will give my brief opinion on all of these wines from left to right, in no particular order, and do yourself a favour, go get them and hang on to these.

1. Juno Shiraz.

Juno wines are based in the Robertson wine region, they make a basic selection of both whites and reds and are well worth a try, This is a nice drinkable Shiraz, with a very satisfying full flavour, not overly heavy, but still a classical shiraz that goes well with any occasion.

A plus is the very well designed logo, you will not only feel like you are drinking a classy artfull wine, but actually have a pretty picture to look at as well.

2. Angels' Tears

From the Franschhoek wine region, a simply divine Merlot/Cabernet Sauvignon blend, This has got to be my favourite wine at the moment, extremely well balanced. Buy a few cases of this one, as every sip is absolute heavenly indulgence with a wine that has such a full ripe body, which is yet deliciously simple and a genuine moving experience.

The logo is also a work of art, depending on your sensibilities. If you like a drinkable wine such as KWV Roodeberg you will enjoy this wine.

3. Oranjerivier Cellars - Ruby Cabernet

Oranjerivier Cellars are from the Northtern Cape, this is another one of those undiscovered classics, and one you can buy in bulk for everyday drinking, great to have around for social events such as dinner parties as it will not put you out of pocket, but no one will disagree that you chose your wine wisely.

4. Ashwood

The estate is also Franschhoek based, and as with Oranjeriviers' Ruby Cab, grab these by the case, for some time now Pick and Pay was letting these go at a buy 5 get one free setup, so pick up a few cases and just save it for your next dinner party.

The one in the Picture is their Merlot, but they also have a Shiraz and a Cabernet, all of these are nice, drinkable wines which cannot be beaten for value for money.

5. Lutzville Pinotage

The name says it all, you have got to include a good Pinotage in any wine list, Initially I wanted to include the Beyerskloof Pinotage, however, they do not quite fit in with my criteria on price anymore. I imagine Lutzville will follow the path of being discovered by some wine exporter and the prices will also be increase seriously, but get a few bottles and hang on to them.

Sunday, October 22, 2006

PHP 5, The Promised Tutorial Part 1

Ok, from a previous post I got some requests around how people can get started with PHP. I am going to attempt posting a series of tutorials here to get you started.

Disclaimer:

I do not claim to be a teacher at all, and if you benefit from this, please let me know. Also, there are no substitute for purchasing a good solid book on the topic to get started, leave some comments at the end of this post if you would like some suggestions on good books to get started.

Ok, getting started.

A quick explanation of what PHP is in order. I will not attempt to give a fancy explanation of PHP here, that is what Wikipedia is there for. Basically PHP is a Hypertext Preprocessor. I will attempt to explain what this means.

If you enter an address such as www.somesite.com/index.php the following happens:

  1. Your browser does a DNS lookup on the domain part to see where exactly www.somesite.com is located, this DNS lookup returns and IP Address, IP standing for Internet Protocol.
  2. Once it finds out where www.somesite.com is located, it will connect to the server and REQUEST the file index.php From the webserver.
  3. The webserver will locate the index.php file on disk. Because the file has a .PHP extension, instead of just sending the content back to your browser, it will First call a file called php.exe, or php.cgi etc, and tell this file to Process or Parse Index.php.
  4. Php will execute all the lines of PHP code (php is included in tags inside the file) to produce a result, and this result is plain HTML, which is then returned to your browser. This return is called a RESPONSE.
Ok, hope I did not lose anyone yet, if you feel slightly lost, hang in there, it will become just a little more clear soon enough.

Perhaps I should explain this by an example. For the first part of this tutorial I will write a classic, Hello World. By the end of this tutorial you should understand the following:
  • Embedding PHP inside HTML files
  • Handling PHP variables from Forms (Include Both GET and POST)
  • Process forms submitted to PHP.
First things first. You need a webserver and PHP installed. Personally I recommend WAMP, this installs Apache, MySQL and PHP which is all you need to get started with PHP. It is easy enough to install, but leave a comment is you cannot get this done.

Once you have WAMP up and running, create a new project, easiest way is to create a new folder in your document root on Apache. If you stuck to the default WAMP install, it should be c:\wamp\www.

Call this new folder hello_world. In here you can create a new file called hello.php.

Open hello.php in you favorite text editor, I use Cream/VIM or Notepad, both work just fine, but you would want to eventually look into something with a tad more features such as proper project/folder management etc, I use Macromedia Dreamweaver for this, but you are on your own here, find an Editor that works for you.

Ok, let us start our file. Let's do the form first...

Type the following into your file (Click for a large view):



Once you have done this, save the file and enter http://localhost/hello_world/hello.php into your browser - If it is not Firefox, get it here - and you should see the following:

Ok, now that's done, if you click the submit button nothing will happen, but you should see your location changing to http://localhost/hello_world/hello.php?action=1.

Now we want this thing to start doing something. Type the following PHP code into the top of the hello.php file.

If you view the page now, and enter your name, you should see the following:




In a nutshell, that was it. Time for a quick explanation...

In the HTML, you created a FORM Element and named it main, you specified the FORM's METHOD as POST, and the ACTION as hello.php?action=1.

The two important bits here is the Method and Action. Method refers to how the form should be returned to the server. This will be POST 90% of the time, but it can also be GET. In very rare conditions you might use mail as well, but this is highly irrelevant for our exercise.

POST means your browser will take all the elements inside the form such as Text boxes, dropdowns, checkboxes etc, and compile this into and HTTP POST request, one day when you are really bored you can read up about this in more detail, and then sends the POST to the URL specified in the forms Action part.

In the action part of the form, I included a querystring variable called action=1. Basically, the querystring is one of your biggest friends in PHP and other web languages for maintaining application state, we will get to that later on. The querystring is a set of values appended at the end of a URL, and starts with a ? After the .php extension. Additional variables can be included using & as a separator, but don't worry about this for now.

Once the POST reaches its destination, you can access a number of variables as outlined below. I am assuming that you are familiar with what a variable is.

These variables are as follows:

$_POST = Array containing all elements posted to your PHP script.
$_GET = Array containing all the variables passed in the querystring.

There are others such as Session and Server variables, but I will explain these later on.

So, we have now told the form where to go, and it is time to explain how our PHP script does its magic.

At the top of the page we start with a block of PHP script. PHP scripts are always included inside PHP tags, which is for closing tags. When the server passes our hello.php file through the PHP executable, PHP will parse everything between these tags.

First we check if our form was submitted. We do this by checking if action was included in the querystring, and if its value was set to 1, this is done with the simple if statement at the top of the page:

if($_GET['action'] == 1)

Please note the use of == which is the comparison operator, which should not be confused with the assignment operator =

If we have the action GET variable and its value is 1, then we generate our hello message and we store this in a variable. Else, we do not generate any message.

Note in this line of code

$hello_message = "Hello there " . $_POST['yourName'];

We use the assignment operator (=) here to assign a value to the $hello_message variable, and we also use the concatenation operator (.) to concatenate the contents of the $_POST['yourName'] value to our message.

Remember the textbox on the FORM was called yourName? That is exactly what you see here, it was included as a POST variable by your browser automatically when submitting to the webserver, and here we access it to generate our message.

Last but not least, we have another set of PHP tags inside our HTML, this line simply prints the hello message we generated and stored in our $hello_message variable to the browser.

This is where we are done with my first, very brief and basic tutorial. I trust that this did give you a small understanding of the very basic concepts of PHP, and exactly how interesting the journey could become going forward.

I encourage you to play around with writing code and sending it to servers and back and seeing what happens. Our next tutorial will be a lot more interesting, when we write our simple calculator class and create a basic online calculator. This tutorial will show you exactly what a class is, and try to explain the basics of inheritance.

Please leave some comments to this, so I can get an idea of how I should change the format of these articles, I hope you found if valuable.

My Current Projects



Being a geek, and in love with Movies and Music, as well as having some skill with programming, I have a fair collection of Movies (DVD) and Music (CD as well as Digital).

I have a Nice large Pulldown screen, as well as an Acer PD125 Projector. I also have a set of Logitech z5500 Speakers (kinda cool, enough sound to rattle both my windows and my neighbours).

To date, it has been an absolute mission trying to get a DVD to play, to give you an idea, because I am renteng my place, It is hard to find ways to cable it all so that it looks good, but is temporary enough not to absolutely destroy my deposit on the place. Also being a bit of a DIY junkie, I am doing everything myself as I get some spare cash.

Right now, I am once again cash strapped because we have the silly season right around the corner, so that means presents, buying plane tickets, etc...

However, I have this old PC, specs something like below:

  • Celeron 1.7 GHZ CPU
  • 256MB Ram
  • 40GB HDD
  • Winfast Geforce 4400 MX AGP Graphics (64MB)
As you can see this is hardly what would qualify as a PC these days, more like a calculator. Here is how this is going to turn into a new Media centre PC over the next 2-3 Months with very minimal investment in additional hardware.

I have a Portable DVD-RW which is quite good, I carried the monster around in my Laptop Bag and used it perhaps 2 times to write anything. I will use this for Creating Archives of my media (Photos, DVD's etc) as well as Making custom Compilation CD's for when I decide to take a drive somewhere.

I also have a 250GB Portable Drive inside an Enclosure. I will use this as my media store by removing the drive from the enclosure and installing as a secondary drive in the machine, which gives me a nice bit of space to start with.

That leaves only one thing in my mind being Surround Sound. My current onboard stereo card simply will not make the cut. For this I will make the only cash investmet needed in the short term which will take the sape of a Soundblaster Audigy Value PCI card, there I go with my audio able to scale up to 7.1 Suround.

Ok, that settled, but now what about software...

The picture at the top of this screen is a part of the conceptual design of the system. I plan to have two parts to the solution, a Media indexing system which I will build on PHP, MySQL and Apache, and a Media player setup which I will use to play any media indexed inside the box, and for this I will use Video Lan (Check it out, I must say this is a nifty little media player) which I intend to automate through PHP via Command line parameters.

Anycase, enough about that, I am currently working on the conceptual design and layout of the indexing site, and will most likely change my mind as I go along.

This project will hopefully teach me the following:

  • How to use Macromedia Fireworks to design a kickass site
  • PHP's extensions for handling MP3 information as well as using GD to build image libraries.
  • A crap load of new JavaScript + CSS + DHTML details, this design I have in mind will absolutely challenge every single boundary when it comes to Web design, I intend to see exactly how far I can push them
  • PHP Error handling classes and interfacing with the Operating System, I will more than Likely get a feel for Ruby/Pear, and most likely play with Some Python/Linux Shell scripting to make things work.
I hope to complete this project by the end of this year, and will post some updates, screenshots and other documents here.

Thursday, October 12, 2006

5 Reasons I Love PHP5


PHP. What more can I say, the script language on the web that I originally thought was, well, just ASP with a different syntax, designed for people who just wanted to complicate life by adding semi colons and curly braces to their language, you know, to show off because they can code in something like C.

Anycase, with the release of .NET I, as a faithfull Microsoft coder, immediately got a copy and started playing with it. I thought .NET was the solution to anything that could happen on the net, you had proper object support, no more writing ActiveX DLL's in Visual Basic 6 and then using them on a webserver where it might or might not work as you want it to, all was native and all was well, until the day I needed to scale. The user base of some of my systems jumped from 30 to 500 users virtually over night. .NET died. So did my love for Microsoft.

That was 3 years ago. I have since been converted to a FireFox user, running Linux at home, only working on Apache webservers and coding in PHP. It is my scripting language of choice for a large number of reasons. I am not going to go into commandline scripts, as I am still trying to make up my mind about a few others (Perl, Python and Shell Scripting) but on the web, I am sold. Projects large and small can be done with PHP, it scales, it is lightning fast, has pretty syntax can be optimized and compressed like hell, and has database and platform support .NET can only dream about.

Getting back to the topic at hand, 5 things I love about PHP.

1. SimpleXML

If you have ever parsed XML in any language, you will appreciate that it can be a huge pain to get through, it can be slow, and it certainly does not lack complexity, yeah, you get used to the pain and it becomes easier, but that is only for those poor souls that never tried SimpleXML.

One Quote I found online goes something like "XML is the solution to all your problems. SimpleXML ensures that it is not the root of all your problems"

For more info visit http://php.net/simplexml

2. CURL

Ever had a need to get some URL directly from a server? Compose a post in the background and get the result? Perform Server2Server XML operations? Look no further, CURL has it all, It is so dead simple to work with I cannot even start to comment as writing the comments alone will take longer than it will to implement a complete Server2Server XML based solution with the combination of CURL and SimpleXML.

More here http://www.php.net/manual/en/ref.curl.php

3. SQLite

Yeah, I know, who needs a small, lightweight, single filebased, cross platform database with a Native API when you have access? Anyone that might accidentally get a database of about 2 terrabytes, that's who. Yeah, it lacks a few things, but check it out, when you are working with the smaller masses (just about every site that will not become the next ebay or yahoo) this is for you, check it out here http://sqlite.org/

4. GD

Working with graphics, we don't really need that on the net do we? Not unless we are trying to get a few Graphs displayed and perhaps do some type of CAPTCHA implementation to prevent comment spam. All you .NET junkies out there, read it and weap when you see how easy and fast GD is as opposed to your System.Drawing collections. Oh, you can actually do a complete image gallery using GD to create thumbnails and resize pictures for viewing in less than 200 lines of code (That is PHP and HTML combined, oh, I forgot, .NET coders do not concern themselves with HTML, who needs a site that is standards compliant in anycase)

http://www.php.net/manual/en/function.imagegd.php

5. The command line interface

Using PHP scripts to automate just about anything, and boy is it fast and small, I have yet to see .NET produce a complete application that actually does anything like interfacing with both an FTP and Database server that is under 100kb.

To conclude, if you have not yet tried out PHP, give it a shot, have an open mind and get creative, remember PHP does not force you to do everything one way as .NET does, it allows you to get seriously creative, and that is what you have to do to get the job done, ultimately, after 6 months of PHP'ing, if you want to go back to Bill, go for it, I promise you will be a better programmer for it.