QikTable Class
© 2001, Dennis Shearin
Author: Dennis Shearin
Version: 1.1
Last Updated: October 11, 2001
Terms of Use: See license.txt
This class provides a simple, yet very flexible way to generate HTML tables. It contains methods that make it easy to import data from a number of different sources, including SQL tables, CSV files and tab-delimited databases.
This class is defined in the file qiktable.php. This file must be included in any PHP file that attempts to use this class. The require statement must appear before any instantiation of this class.
object QikTable([array content] [, array headings])
The QikTable constructor accepts two optional arguments and returns a QikTable object. The first argument is expected to be a two-dimensional array representing the table's rows and columns respectively. The second argument should be an array containing the text for the headings. Omitting either of these parameters results in a NULL value for that parameter. Methods are provided for setting these parameters after instantiation.
Nothing is output at the time the QikTable object is instatiated. Methods are provided to print the table once all parameters have been set.
Adding data:
This method adds one row of data to the end of the table. The parameter is expected to be an indexed array.
This method is ideal for building an HTML table from an SQL table. The output of each mysql_fetch_row statement can be used as the argurnent to the addRow method.
This method also takes an array as input and adds one row of data to the end of the table. Unlike addRow, this method will also process associative arrays, as well as non-sequential or non-zero-based indexed arrays. It can be used in conjunction with the mysql_fetch_assoc function.
Like the above methods, this adds one row of data to the end of the table, but it extracts that data from a string rather than an array. The second parameter is an optional separator character which is used to break the string into array elements. If the separator is not specified, then the default character (tab) is used.
This method is ideal for building an HTML table from CSV or tab-delimited flat file databases.
This method can be used to set the headings for the table. It accepts an array containing the headings. The previous contents of the headings array are overwritten. To remove headings from a table, use:
MyTable->setHeadings(NULL);
This is a qik and easy way to set up the headings from the keys in an associative array. The keys are extracted and the first letter of each key is capitalized. The result from the mysql_fetch_assoc function can be used as an argument to this function.
Setting up columns:
This method can be used to set the number of columns to be displayed in the table. If the number of columns is not set by calling one of these methods, then each row of the table will contain the number of entries in that row of the array. No checking is done to make sure that the number of cells is the same in each row of the table. The major browsers don't seem to have a problem formatting uneven rows, but it's not good form.
This method can also be used to limit the number of columns printed. For example, if your array contains five elements per row, but you are only interested in the first two, setting the Column Count to 2 will hide the last three columns.
This method sets the number of columns to be displayed based on the number of entries in the Headings array. To qikly set up a table from a MySQL table, first take the results of the mysql_fetch_assoc function and call setHeadingsFromKeys(), followed by setColCountFromHeadings().
This method sets the number of columns to be displayed based on the longest row in the Contents array. This is the least effective way to set the Column Count. If the Contents array is large, this could be time consuming. Also, if every row in the array contains at least one NULL value, then the results will not be accurate, as NULL values are not counted.
This method will only set the Column Count if the result it gets is greater than the current value of the Column Count. For example, if every row of the Contents array has three elements and the Column Count has been manually set to 4, a blank column will be output at the end of the table. When using this method, it's best to explicitly set the Column Count to zero first.
Setting up the table:
This method sets the border parameter of the table. An integer parameter is expected and behavior is undefined for any other argument type.
This method sets the cellpadding parameter of the table (the amount of whitespace surrounding the contents of a table cell). An integer parameter is expected and behaviour is undefined for any other argument type.
This method sets the cellspacing parameter of the table (the amount of whitespace between adjacent table cells). An integer parameter is expected and behaviour is undefined for any other argument type.
This determines how the table will be aligned with respect to the page (i.e., left, right, center).
This method can be used to set the background color for the table. Standard color names can be used or the colors can be entered in hex notation (i.e., #FFFFFF, #9900FF, etc.).When called with a NULL argument, the table will be the same color as the page.
This method can be used to specify the URL of an image file to be used as a background for the table. Typically, this takes precedence over the background color.
This determines how the headings will be aligned with respect to the columns of the table (i.e., left, right, center).
This method can be used to set the background color for the headings. Standard color names can be used or the colors can be entered in hex notation (i.e., #FFFFFF, #9900FF, etc.).When called with a NULL argument, the background color of the headings will be the same as the underlying table.
This method can be used to specify the URL of an image file to be used as a background for the headings. Typically, this takes precedence over the background color. Support for this varies between browsers.
This determines how the content in the body of the table will be aligned with respect to the table's columns (i.e., left, right, center).
This method can be used to set the background color for the body content. Standard color names can be used or the colors can be entered in hex notation (i.e., #FFFFFF, #9900FF, etc.).When called with a NULL argument, the background color of the content will be the same as the underlying table.
This method can be used to specify the URL of an image file to be used as a background for the table's content. Typically, this takes precedence over the background color. Support for this varies between browsers.
Outputting the table:
This method will print out the initial <table> tag with the parameters that have been set.
This method simply outputs the closing </table> tag.
This method outputs the headings for the table.
This method outputs the complete contents of the table as defined in the content array.
This method calls each of the above methods to print out the complete table. Using the individual methods allows the flexibility to add a title, comments or other info to the table.
Viewing a Database:
The following example shows how easy it is to display the contents of a MySQL database:
<?php // Create QikTable object require("qiktable.php"); // Use full path if not local $yourQikTable = new QikTable(); // Open database and send query // $user, $password, $database and $tablename can be: // -> Set in an include file // -> Passed in an HTML query string // -> Set in a form whose action is this PHP file // -> Replaced with hardcoded values $dbconn = @mysql_pconnect("localhost", "$user", "$password") or die("Can't open database"); mysql_select_db("$database") or die("Can't open database"); $result = mysql_query("SELECT * FROM $tablename ") or die("Error processing query"); // Extract headings and first line of table from the first row of the database $row = mysql_fetch_assoc($result); // NOT mysql_fetch_array($result)! $yourQikTable->setHeadingsFromKeys($row); $yourQikTable->addArray($row); // Get the rest of the table while ($row = mysql_fetch_row($result)) $yourQikTable->addRow($row); // Set desired parameters $yourQikTable->setHeadingAlign("left"); // And display it $yourQikTable->printTable(); mysql_free_result($result); ?>
Inheritance - Extending QikTable:
This example shows how to extend the generic QikTable class to create one that is more specialized. I use the LogTable class to display the change log for QikTable. This class inherits all the member variables and methods of the parent class.
As you can see, there are only two overridden methods: the constructor (because the constructor of the parent is not automatically called) and the method that outputs the contents (which allows the log to be formatted differently than a generic table). QikTable's addLine method is used to input the data from the tab-delimited log file.
Click here to see the results.
<?php require("qiktable.php"); // Use full path if not local class LogTable extends QikTable { var $fields = array("Date", "Hours", "Version"); // Constructor function LogTable($contents = NULL, $headings = NULL) { $this->contents = $contents; $this->headings = $headings; } function printContents() { for ($i = 0; $i < count($this->contents); $i++) { echo "<tr>\n"; for ($j = 0; $j < 3 ; $j++) { echo "<td align = \"$this->headingAlign\" "; echo "background = \"$this->headingBackground\" "; echo "bgcolor = \"$this->headingColor\" >"; echo "<b>".$this->fields[$j].": </b>"; echo $this->contents[$i][$j]; echo "</td>\n"; } echo "</tr><tr><td colspan=\"3\" "; echo "align = \"$this->contentAlign\" "; echo "background = \"$this->contentBackground\" "; echo "bgcolor = \"$this->contentColor\" >"; echo $this->contents[$i][3]; echo "</td>\n"; echo "</tr>\n"; echo "<tr><td colspan=\"3\"></td></tr>"; // Spacer } } } ?>
Interactive Table Builder:
This example lets you use QikTable to dynamically change the appearance of a table. Once you have a design you like, press the "Show Code" button to display a script that you can cut and paste into your own PHP file.
Click here to see this example.