Posts Tagged ‘programming’

PDO database layer

Wednesday, January 18th, 2012 | Tech

If you’re not using PDO in your PHP projects yet, you should be.

I finally got round to taking a proper look at it recently, and I’m really impressed. I managed to convert an entire open source project to it in under two hours! Granted, I was helped a lot by the fact that the PDO naming conventions luckily match up with the naming conventions I used in my original data access layer, but still, it was smoother than I expected.

PDO has some great advantages too:

1. It’s cross platform, so you can use it on a number of different database platforms including MySQL, MSSQL, Oracle, Postgres, SQLite and many others! All you do is enter the protocol and login details to the connection and it handles everything else.

2. It has prepared queries so you never need to escape anything again! You simple pass it some SQL with a series of question marks in, and an array of values those question marks represent and all the SQL injection negation is taken care for you.

3. If something goes wrong, it throws an exception rather than producing an error message.

4. It’s easy to extend, to add your own functionality. Just use Class MyPDO extends PDO and you can add extra functionality on top of it.

All in all, it’s a great addition to PHP and one I now wish I had gotten to sooner.

Old is the new new

Monday, December 26th, 2011 | Life, Tech

Over the years, the Worfolk Online network has run a lot of technical websites. Dozens and dozens of them. Each one had a selection of interesting and useful content, which for one reason or anther has since been closed down.

So, we decided to rescue them.

The result is that we’re currently working on importing articles from a number of old websites from our network.

It’s stretching our remit a little further than we already do – most of the articles are about web development and programming, but we thought it would be better to put them on here then for them to be lost forever – though that’s probably a big claim given how old the content is in some of them – it’s things we haven’t been doing for a decade!

Never the less, you may notice that our archives now date back to before the site was even founded in 2005. This is as a result of the new content we have brought on board and with articles dating back to 2002, we are now pulling together resources from the past decade!

Testing multiple conditions in if statements

Friday, December 2nd, 2011 | Programming

This is one of the geeks among you. I’m currently working on a new open source project which uses a series of database models derived from a base model which includes some standard methods and I was having one problem in particular to do with updating data values in an object.

if (
    !$object->setName($d["name"]) ||
    !$object->setSlug($d["slug"]) ||
    !$object->setDateByArray($d["date"]) ||
    !$object->setContent($d["content"])
) {
    $this->setMessage($object->getMessage());
    return false;
}

As you can see, this does an if statement and then runs all the updates – the idea being that if any of them come back as false, it can set the error message and return false to the controller.

This worked as expected but with one problem – as soon as one of them appears false, PHP stops looking at the other conditions in the if statement. This is a problem because it means the user only gets one piece of feedback at a time. Whereas, it would be much better if we could give them all the fields that were wrong at once.

However, this doesn’t seem possible in the above implementation. Even if I switched it round to check that everything is true, and everything must be true, that suffers from the same problem that once something isn’t, PHP will stop checking the conditions.

I considered whether I could implement it using exceptions – putting all the checks inside a try block and then have the update operations throw an exception if they were invalid. I could then catch this exception and add the error to the list of errors. However, this suffers from the same problem – as soon as the first invalid value triggers an exception, the rest of the try block is ignored.

The solution I have eventually come to is this:

$writes = array (
    $object->setName($d["name"]),
    $object->setSlug($d["slug"]),
    $object->setDateByArray($d["date"]),
    $object->setContent($d["content"])
);

if (in_array(false, $writes)) {
    $this->setMessage($object->getMessage());
    return false;
}

All the operations are run, and their values are fed into an array. That way all the checks get run no matter what value they return. I then search the array for any values set to false and if any of them were, I know there has been a error and can flag it up with the user.

Poker Stats Library

Tuesday, August 16th, 2011 | Tech

A few weeks ago, I wrote some tools which would help me out in getting to grips with poker, which in general I fail at.

It annoyed me because it should be fairly simple for someone like myself to get my head around the poker maths (well, it is, pot odds are easy), so even despite the lack of social understanding the life of a computer scientist brings, I should at least be able to achieve a level of averageness in the game. I clearly have failed to do this, and so I decided a bit of work on my basic strategy was needed.

As a result, I built an interactive tool which would teach me what starting hands I should play, similar to the concept of Basic Strategy in blackjack. It presents you with two cards and you have to say what position you can play them from, if any. It will then tell you if you are correct or not, if not it will ask you to try again and if so, it will move on to the next hand.

I also wrote a tool which allows you to select the cards you have, and using the same formulas it will tell you what position that hand is worth playing from. I’ve thrown in a few other simple odds calculations in there as well.

Of course, these won’t make you a great poker play by themselves, but it should provide a good basis to learn from.

Given the tools would otherwise just disappear into the depths of my hard drive somewhere, I’ve decided to publish the code on Github. Should you have any interest, you can download the source from the Github repository. It’s all written in PHP and should run out of the box.

Btw, the images below are screenshots, but the way they have been scaled down looks rubbish. They make more sense when you open them…

Heads and brick walls

Sunday, August 17th, 2008 | Tech

Facebook’s developer platform is a joke.

I mean, it wasn’t great initially but they made an effort. They launched a wiki and tried to provide good documentation. They often didn’t succeed but a little more time and experience and they would probably get the hang of writing good quality documentation.

But it’s just gone down hill. As you probably know they have re-designed Facebook and as part of their changes they have altered much of the API and mark-up language. The only problem is, they didn’t bother to updatr any of the documentation. So none of it works anymore.

Nothing is coherant either. There are dead links and nothing matches – the new integration guide will say one thing and the documentation for the function it is talking about is completely different.

Worst of all, the final hope of salvation, asking users who have actually managed to get it working, isn’t a simple search of the forums at the moment as it’s returning no results for any search term I care to put in. Fantastic stuff.