gc on November 24th, 2008

Trees are all around us… inside and outside our computers. As you may have already seen on your daily basis work, when you deal with tree-like structures binded to tables into your DB, you set something like


id | id_father | name | other datas
01 | 00 | root | ...
02 | 01 | sub node | ...

this kind of data structure is usually managed via recursion, and in web applications is useful to store site menu, product categories, file-system alike apps and so on.
The class i’m seeding here does all the dirty job giving you the possibility to

  • use custom callback to modify the default behaviour [<ul><li> sequence]
  • minimize/maximize function [for folder-like interfaces]
  • navigation path serialization

those are the methods usually needed for day-to-day work with trees.
here you find all the code, but I’d like to poin out two things that [IMHO] really teach something to the average PHP programmer

  • in the constructor 3 arrays are build as properties starting from the original array: arrayById, arrayByFather, arrayHasChild. I don’t know if it would be faster using array_find.. but once we’ve made the proper index association, ther’s no need for the original array to be parsed again.
  • use of $$ for the callback function: not so many people knows this sintax, but it’s worth a try

the rest is really simple, and it’s well commented as well.

of course it fits perfectly with the standard output from dbmanager class presented here. Set up your table in an appropriate way, make your assocQuery call and use your tree in no time.

Tags: ,

gc on November 19th, 2008

Yes, whe know, W3C specs garbaged all the nice IFRAMES that fitted so nicely in our pages. Ajax helped a lot but even if you used Prototye or some other ajax flavoured framework there was soo much work to do; each single link needed an event listener, some tweaking and so on.

After messing up with some application requiring a huge number of ajax calls, I ended up thinking about this little piece of code:

window.onload=function(){
Event.observe(document.body, 'click', function(event) {
    var element = Event.element(event);
    if ('A' == element.tagName){
        if ($(element.target) != undefined){
            new Ajax.Updater(element.target,element.href,
            {method:"post",evalscripts:true})
            Event.stop(event);
            }

        }
    });
}

basically, this 10-line worth trick sets an event listener on every click. If you click on a ‘a’ with a target attribute set up, it starts an Ajax.Updater call on it.
you can of course add GET parameters and feed .php, and of course javascript code is parsed as well..
the complete seed [just a .htm and .php example file added] can be downloaded here

Tags: , ,

gc on November 18th, 2008

here we go, a simple, very basic class to manage DB connections; I know there are so many around BUT

  1. it’s very lightweight because it:
  2. makes use of the “singleton” pattern, so you can start make a mess with patterns [if you've never do so]
  3. ther’s soo much room for improvement
  4. since I use it on an almost everyday basis anyother of my seeds will use it, so get used to it!

When you deal with mysql connections you should keep in mind that every single connection takes its load or system resources, and if you don’t pay attentio you could easily end up into a ‘too many connections‘ error and a faulty applications.
The very first solutions would be trying to use persistent connections, and this works very well on the mysql side: every page builds just one connection. The singleton pattern is chosen here because off it has a very low footprint into system resources: this means that you free more resources for the rest of your crappy code.

After creating your dbmanager Object, you have to dbmanager::connect giving host, user, pwd, db as always. from now on, you will have basically two methods:

dbmanager::assocQuery($sql) and dbmanager::sqlQuery($sql)

those two methods just query the database with the given query [there are also some nice counters if you wanna know how many querys ar made for every page you build].
The nice thing about dbmanager::assocQuery($sql) is how the result is returned: an associative array with fieldnames as keys: that’s really useful when you then have to cycle through your recordset and build tables, analyze data and so on.


$db=new dbconnection("mysql");
$db->connect($host,$login,$password,$database);
$recordset=$db->assocQuery("SELECT * FROM TABLENAME WHERE CONDITIONS...");
foreach($recordset as $record){
    // here goes your code
    }

very straightforward, isn’t it???

uh.. the code is here

Tags: , , , ,