diode got some spring cleaning last night. I managed to free up enough space on the spare harddrives to backup the main drive for a total reinstall. Yeee. But that is not going to happen right now as I got a wild hair up my ass last night (1am this morning really) to install Gentoo on my laptop, not at all taking into consideration that this would force me to work on diode. No fun. So I am headed to Wierdstuff to pickup a new mobo for the raver box. It will get Gentoo and be my new dev server, diode will get Win2k, and the laptop will probably dualboot both. I am off to my next adventure.
Archive for April, 2007
Wierdstuff
Sunday, April 29th, 2007Using PHP to generate JavaScript classes
Saturday, April 28th, 2007In the next couple of days I will be releasing the first version of my (A)JAJSON DB Interface (yeah). The main concept of my DBI is an Object represents a tables, and Classes are rows from the database.
$arr = array('users' => array('id' => 'int', 'name' => 'char', 'pass' => 'char'),
'comments' => array('id' => 'int', 'message' => 'char', 'when' => 'date'));
This is the array that would be assembled from the database by PHP.
Table users;+-------------+--------------+------+-----+---------+----------------+ |Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | | |
| pass | varchar(255) | NO | | | | +-------------+--------------+------+-----+---------+----------------+
Table comments;
+-------------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | message | varchar(255) | NO | | | | | when | timedate() | NO | | | | +-------------+--------------+------+-----+---------+----------------+
Looping through this array, we can output JavaScript to prefab classes around tables in our database.
foreach($arr as $k => $v) {
echo "function {$k}() {}n";
foreach($v as $kk => $vv) {
echo "{$k}.prototype.{$kk} = {$vv};n";
}
}
This, put in a <script> tag, will give you a ready made object from your database. Further parsing would yield even better objects. Match types, like if type is int(anything) output Number() as $vv. Next is adding functions to do things with the content. An explanation of that will be coming up soon with the release of (A)JAJSON DBI.