Sunday, October 15, 2006

A Class act - object oriented programming

Programming with objects (OOP) is both easy and flexible. It is one of the main tools in a programmers Toolbox.

Objects are created with PHP and most other programming languages, using Classes.

Basically an object is Defined. For example:- an Advert.

This object (advert) is then constructed within the computers memory.

The object has properties, for our advert object those could be an identity, an owner, and other information about the advert. Properties are simply variables within the class.

You can do things with the object, again using the advert object as our example. You can view it, create it, update it etc etc. The things you can do with an object are called methods, essentially the methods are functions of the class


As this blog progreses, it will become clear how powerfull OOP is, whilst retaining simplicity.

So without further ado, lets discuss what objects we will need in our Classified advertising site. as well as what we will want to do with these objects.


The objects, starting with the obvious, but not in any particular order.
























































ObjectPropertiesMethods
Advert

Image

Thumbnail

Banner

User

Session

Authorisation

Signup

Smarty


Database


A Smart move - The Smarty template Engine

As was discussed in the previous blog, our classified advertising site is coded using PHP.

If you have looked at more than a few php scripts, then you will know that
the combination of the php code embedded thorough the HTML code all in the one script is
both messy and cumbersome,
quite honestly it looks a mess.
For larger projects, such as this one, the coding would put anyone off the subect
for life.
There is a much better way.

Using a template Class allows us to fully seperate the php code from the HTML.
Using such a class is almost essential for these larger projects.

Whilst Smarty is not the only templating class available, it is one of the
best. It has been around for some time, so it is mature and stable. Also it is public domain.


The Smarty class
allows you to code all of the php in one script and place all of the
HTML code in a seperate file. This HTML file is not quite free of
programming, but it is simplified to the extent that it is merely
referencing of variables , looping of arrays and using simple if
statements.

All of the complex stuff, like referencing of
databases, parsing of xml data figuring out sessions etc etc is done by
clean, clear, simple, pure php code.


The smarty HTML files are called templates(no surprise there) and use the extension .tpl
They have embedded within them the much simplified smarty programming code.


The PHP code gets the required information, puts it in variables or arrays and passes these to Smarty. via a statement like:-

$Smarty->assign('description',$description);

You
them tell Smarty which template(s) to use and the class will processes
these templates with the embedded code to produce the final HTML Page.

such as:-

$Smarty->display('header.tpl');
$Smarty->display('body.tpl');
$display->display('footer.tpl');


There is a lot more to Smarty than mentioned here. Suffice to say, that we will cover the subject more thoroughly as and when we need it throughout this blog.

Sunday, October 08, 2006

Introduction

There are now many classified advert sites online.
Some, mostly those owned by larger companies, charge a fee to place an advert on their site.
When you consider how cheap they actually are to own and operate, it makes you wonder what these guys do with all the money they are making. Look at autotrader.co.uk and try to estimate how much they are making. At £25.00 per fortnight per ad, it's a lot.


Whilst the internet is essentially public domain, it should be appreciated that we should indeed be able to make money from the sites we have created, especially if that site is of value to the community. After all there is some work and costs involved.

So I am going to discuss ways of keeping such a site free to the end user, whilst still making some money from it.

There are many commercial classified ad scripts available, but they are not flexible enough to completely customise. In addition, if you want more than just basic functionality like adding multiple pictures, creating thumb nails etc, then you will have to pay a fee, which can be considerable. Also, there's not much satisfaction to be gained from such a thing.

Classified Advertising sites can be as complex as you want, but they are not beyond the scope of anyone who can read, write and has a moderate amount of intelligence.

In a series of blogs over the next several months, I intend to discuss in detail, the methods I have used to create such sites.
In the process, we will create another new one, to prove how easy it really is.

The new site will be called YFORS, standing for Yachts for sale..
The detail will have enough information to allow you to develop your own Internet classified advertising site for any purpose you wish.

Once the site has been coded, tested and uploaded as a live site, there is still a lot of work to do to make the site popular. But when this is achieved you will be amazed, the site will become almost alive, as users upload and amend their own adverts.


Other sites I am responsible for are - aircraft for sale and motorcycles for sale www.afors.com and www.mfors.com respectively. So you can see, the code could be used for any classified items, Including houses.

The sites essentially use the same core code, which is written in PHP, and the database is MySql. The sites also take advantage of the Smarty Template engine, which allows complete separation of the PHP programming code, from the HTML template Code. This is a very big advantage as it simplifies things considerably, as you will discover in later posts.
The PHP code is written using modern programming techniques. i.e. utilising classes.

The HTML templates are not completely without some programming skills, but these are reduced to a very basic level. If you can write HTML, you can also do Smarty.


If you are new to Internet programming, then this will be a very good learning experience. Most of the code is beginner to intermediate level, but there are some advanced bits, like parsing XML data from AMAZON, and creating static pages from dynamic data, but I will explain these comprehensively and carefully.

It's going to take a while though, as there is a lot to cover.




Alex