your php script is already doing it… or do you like to translate that php code into blocks? in case of yes, then a good starting point is this guide
Taifun