Wednesday, 16 January 2013

Image Search OOPS

Today I was having a laugh with one of the guys at work and I wanted to send him an image of a donkey but I was having a brain dead moment and the only word I could think of for donkey was "Ass".  So I typed it into http://images.google.com and hit return.  Suddenly a lot of pictures of women in and out of underwear covered the screen.  LOL you've never seen anyone hit CTRL+F4 quicker in your life.

Monday, 14 January 2013

Tutorial - Write less code but write it better


One of the main problems with so many application developers is that they are happy to sit and write the same CRUD functions again and again and again.  To my fellow developers I say No More!!!


CRUD stands for CREATE, RETRIEVE, UPDATE & DELETE.  The basic code that we keep writing again and again.  About 10 years ago I wrote the CMS package for Libertas Solutions it was a massive undertaking taking me just under 4 man years work of work and all inside of a 2 year period.  I was working 16-20 hours a day solid for 2 years to produce the CMS.  It was and is completely modular allowing the creation of new modules and had three different experiences depending on the level of functionality that you wanted which was aimed at Small, Medium & Enterprise companies.  It was one application that could server all our clients content in one go it was WAI level 3 compliant and Passed Site Morses WAI level 1 completely first time.  The problem was that each module had massive functions to do the CRUD and I wrote 70 modules in the 2 years that I worked on the project.




One thing that I was sure of was that there had to be a better way of coding one that would produce better and more easily read code.  I think most developers when they hit that wall will come up with what is undoubtably the next step in the thinking of developers and that is to atomise the data you are wanting to save.  You normally end up 1 or 2 tables for your system one table will store each field of an object as a row in the database.  Brilliant you say I can now just dump information into this table and extract it at will.  and while you can do this there is one major flaw searching the data is a pain in the arse.  You can end up with 30+ inner joins on a database just to pull the information you need.  the real solution is actually more straight forward.



First you need to realise that the database model you were using originally is actually the correct one for the job what you weren't doing what managing the handling of the records correctly.


Lets say we have a table that stores the following fields about a person

TABLE Person
   id - Primary auto-incrementing key
   name - the name of a person

now lets create a class that represents that table.

Class Person extends DatabaseModel{
    private $primaryKey = "id";
    private $fields = Array("id"=>DB_INT,"name"=>"DB_STRING");

    public $id = null;
    public $name = null;
}

now lets say we build a function in our database class that looks something basically like this

define("
DB_INT", 0);
define("DB_STRING", 1);
define("
DB_DATETIME", 2);

class database{
....
function save($obj){

    if($obj->getPrimaryKeyValue()<0){
          $sql_insert = "........."
          foreach($obj->getFields as $field=>$type){
          }
          return $this->dbObj->execute($sql);
    } else {
         //  do an update using the fields.
    }
}
....
}

With this type of coding style you will write small simpler code ( no it doesn't work with joins its for when you wan to extract record X edit it and save it)

$person = new Person();
$person->name = "Bill Gates";
$db->save($person);

done


Sunday, 13 January 2013

PHP Tutorial - the wonderful world of hello

Ok the second tutorial will be simple its hello world.  Ok so I'll write some code I'll use two minus signs to represent all the code in the file.  The first double minus sign will be followed by the file name  so here is the first version of hello world

--hello.php
hello world
--

as you can see there is not even a php processor tag involved.  So what would it look like if we used php to write the code well one of three ways


--hello1.php
<?php
print "hello world";
?>


--hello2.php

<?php
echo "hello world";
?>

--hello3.php

<?="hello world";?>



OK lets go a step further and get it to say our name with it defaulting to world if no name supplied

--hello4.php
<?php
$name = isset($_GET["name"])?$_GET["name"]:"world";
// the following will write out the same text in different ways
// concatenate the two strings together
print "hello " . $name;
// with double quotes we dont need to concatenate two string together.
print "hello $name";
?>

You can test this by running this file via a web server and through your browser

hello4.php
hello4.php?name=test

Ok still with me  then how about an image that writes hello world to the screen

--hello5.php

<?php
header('Content-Type: image/png');

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 10, 5, 5,  "HELLO WORLD", $text_color);

// Output the image
imagepng($im);

// Free up memory
imagedestroy($im);
?>



--hello6.php

<?php
header('Content-Type: image/png');

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);

$name = isset($_GET["name"])?$_GET["name"]:"world";

imagestring($im, 10, 5, 5,  "HELLO ".$name, $text_color);

// Output the image
imagepng($im);

// Free up memory
imagedestroy($im);
?>





Friday, 11 January 2013

PHP Tutorial - Your first program is not hello world

Ok so i'm going to assume that you have already got php installed  if not then for linux use a package manager to install each of the sections your will need PHP, Apache and Mysql server.  For windows install a complete package like XAMPP for Mac OSX install MAMP.

Php is a scripting language so the .php files are all text so to run the script you must compile the script so that the computer can run the steps that you have in your code.  Most programming languages compile once and give you bytecode files which are normally then linked into an executable.  for example the java language is compiled into .class files (bytecode) which you then server to a JVM Java virtual machine which takes the bytecode and links it together so that it can run on what ever operating system you have a JVM for.  C compiles to .o files then is linked into exe files which you then server and  run.  With php you just upload your code to a server and it gets compiled & linked each time the page is called.

Ok the first program that we are going to write is to check out what things in the php.ini have been loaded correctly.  Don't assume that because you have put the correct line in the PHP.ini that its installed.  This program will also allow you to find out why a program that works on your dev box doesn't work on your live environment.

Ok php is a scripting language now with most scripting languages you just write your code in the text file and run it

-- filename : hello.bat
echo "hello world"
-- /end file

With php it was written to be embedded in html pages so you need to  wrap your scripts in processor tags that tell the php interpreter php.exe

<?php

?>

It is possible to configure php to accept short processore tags <? & ?> you save 3 bytes of information doing it this way.  "DON'T EVER DO THIS" because you will find that you'll move your application to a different provider who hasn't and won't configure php to allow it.  For compatibility with most servers use the long version.

So your first program is not an hello world program we are going to write a program to tell us which extensions that are in php.ini have been enabled.

<?php
phpinfo();
?>

Thats it! yeah that simple,  the PHP guys have written this function to report what has been configured correctly.  Have a look at the results of the file you will see things like the version of PHP that you are running.

In the future tutorials I hope will we will cover image creation, database access using PDO,  Fetching content from another server via CURL & SOAP.

The next tutorial is going to be how to do different hello world programs in php and not the following hello world program.  So we are going to need the basic image library "GD", the "PDO" library and "CURL" so scroll down the configuration page to check if those modules are available.  If they are not available then configure php.ini and restart your webserver to test if they become available.

<?php
 echo "hello world";
?>

Sunday, 6 January 2013

Had to use an Android phone the other day at work.

All I had to do was redial a number that we had just used (we were just testing the office phone network) so i picked up the phone and navigated to the last call screen very simple.  The one that lists the last numbers that you rang.  I pressed the last number and stuck the phone to my ear.  after waiting 10 seconds for the ring tone I looked back at the screen.  The phone was still sitting on the list of numbers that you previously dialed.  So I thought doh I mustn't have actually pressed correctly. So I pressed the screen again noticed an interaction and as I was putting it to my ear realised that it screen didn't look like it was making a call.

Looking again I realised that the default action was to list the history of making the calls "What?????  No" I thought to my self the default should be make the bleeding call I wanted to not list the history then have to click on one of those previous 5 calls to actually make the call.

I mean I had to choose one of 5 options all of which was the same.  Now I don't know if this is still the way that Android phones work but honestly just make the dam call.  This could be simply fixed by putting ring number icon on the right hand side of the cell.  Which you can  then easily click on to make a call or click on the data cell.

Every phone I've ever used would dial the frigging number when you select it from the call history.

Found out today that it was running version 2.2 of Android

Saturday, 5 January 2013

Why develop n-Tier Applications

New or junior php developers are always asking what they should be doing to improve their code which has led me to write the following post about n-Tier development. So what is n-Tier development,  N-Tier development is splitting your application into different sections to allow ease of future development.

For example if you were storing data in an array for your application then you would state that the php application is encapsulated in a single Tier.  Once you move to storing your data in another location separate from the php language like a database or data files.  That you process either through a database connection or through an import function.  You have moved to a 2 Tier application.  All good so far as this is where most developers will get to quite quickly.  Lets talk about 3-Tiers of application development,

So you have 

1) Database
2) Application 
But what is the third Tier???

The third Tier is presentation and by splitting your application into two distinct pieces you can get your third tier.Most applications have a business logic and a presentation logic and most young developers have these mixed through out their code.  Meaning that future changes to the system will either require a rewrite of a large complex function so that you can present two views or what normally happens is that the next developer takes a copy of function A and calls it B and then makes B do what they want.  So whats the problem with that ?  Simple if there is a fundamental flaw or security hole found in function A then you have to also know that you must fix B as wellTake for example the following small bit of code

hello("world"); 

function hello($name){ 
   $name = strtoupper($name);
   print "&gt;&gt;&gt;Hello [$name] &lt;&lt;&lt;";
}

As you can see this function renders the name in capitals and puts  angle brackets around the statement.

now lets say that we want to do the same thing but changing the presentation to be square brackets  would you create a new function our would it have been better to write it a different way to start with.

print ">>>" . hello("world") ."<<<";
print "[[[" . hello("world") ."]]]";

function hello($name){ 
   return strtoupper($name);
}


As you can now see the presentation is separate from the business logic.  the code is simpler as you have removed the presentation logic to a different location.

I was asked for example code so here you go

https://github.com/IrishAdo/examples-php