Formatting Database Results into Newspaper-Style Columns, Part 2By Claude Rubinson
In Part 1 we presented the code. In Part 2, we are going to discuss limitations and customizations.
The presented method of dimensionalizing the multidimensional array against the number of records and the number of columns creates an inordinate number of superfluous elements if the number of columns is greater than one. Specifically, the number of superfluous elements equals the product of the number of records and the number of columns minus the number of records. The solution is to dimensionalize the first element of the array against the quotient of the number of columns minus one and the number of records. This will cause a 'division by zero' error if the number of columns equals one, so a check for the number of columns is included.
There is one undesirable aspect of the script; depending upon the requirements of your particular project, this may or may not be serious. Under certain conditions, the number of rows per column may vary. Ideally, the only variance in column length would be with the last column, but the current script balances the number of records across all columns. The result is that sometimes the one column may be shorter than that which follows it. To date, I have been unable to develop an algorithm that corrects this flaw; if someone else is able to, I would appreciate your sharing it.
The script is rather flexible and permits a wide range of customization. Header and footer rows may easily be added to the table by passing the number of columns to the
COLSPAN attribute. Further customization is possible by adding additional dimensions to the multidimensional array. The next image presents a customized version of the script and the resulting output.
View the code
Finally, note that the script may easily be converted into a subroutine. By passing the number of desired columns to the subroutine (from, say, an HTML form), the custom formatting of results is available.