PHP Class: DataDump
December 31st, 2007
Been working on this on and off for the past few weeks (mainly off if I’m honest). Not sure how useful this will be, but here goes! Basically, this is sort of a utility class designed to let you see the results of a search query in a number of formats: an array, un-ordered lists or a table. This can either be printed or returned as a variable for later use. The idea behind this was to let the developer see a big load of data in an understandable format without having to create their own function, and also be able to present data quickly to a client/co-worker for the same reason.
Take a look at an example:
include(‘path/to/datadump.class.php’);
$projects = new DataDump(‘SELECT * FROM projects’);
$projects->printTable(‘proj’); /* where ‘proj’ is the class of the table (this is optional) */
So the first thing you do when you want to call it write new DataDump(‘your query’). Note that because this cannot be used to add, edit or delete, since all the methods need a results set to work.
From there, you have a number of functions you can call:
- returnArray() : this does exactly what is says on the tin, and returns the results as an array.
- printArray() : uses print_r sandwiched between <pre> to print out the array is a staggered format.
- returnList($class) : this returns each row as an <ul> unordered list, with each list item corresponding to a field. The <ul> element has the id ‘item_[rownumber]‘ and each <li> element has a class corresponding to the field name. $class is an optional field where you can specify a class for the <ul> elements. This function returns the list to be used as a variable.
- printList($class) : as above, but this function will print out the list when called.
- returnTable($class) : returns a table of the records. The first row contains the field names inside <th> cells, with the row being inside a <thead> cell. Each cell has a class corresponding to the field name, in the format ‘col_[fieldname]‘. The records are all outputted as rows, where each <tr> element (each record) has the id ‘item_[rownumber]‘ and each cell has the class ‘[fieldname]‘. $class is an optional field where you can specify a class for the <table>. This function returns the table to be used as a variable.
- printTable($class) : as above, but this function will print out the table when called.
Also, if you want to use a really shorthand method, you can specify the output style when making the class, by adding an extra parameter with the name of the function in lowercase. E.g.:
$projects = new DataDump(‘SELECT * FROM projects’,'printtable’);
Of this course this means that you can’t specify a class for the output, but in most cases you wont have too.
Phew! Now thats out of the way, download and have fun! There are a lot more notes inside the class file itself to be fair, feel free to suggests improvements or bug fixes. Here’s the download link: