[b]Html_Table[/b]
[code=php]    /**
     * The Html_Table class can be used to build an HTML table using PHP's OOP principles. Each generated
     * table can be divided in three different sections: thead, tbody and tfoot. The class uses fluent
     * interfaces.
     *
     * @author Gerard Klomp <gerard.klomp@sitemasters.be>
     * @version 1.0
     * @license http://sitemasters.be/mit-license.txt MIT License
     */
    class Html_Table
    {
        private $_attributes;
        private $_head;
        private $_body;
        private $_foot;

        public function __construct()
        {
            $this->_attributes = array();
            $this->_head       = array();
            $this->_body       = array();
            $this->_foot       = array();
        }

        /**
         * This method allows to add a row to a section of the table. The first argument must be either a
         * string providing the name of the datasection (head, body, foot) or an Html_Table_Row object. If
         * supplied with an Html_Table object this method will return itself, presenting the user with the
         * option to make use of fluent interfaces. If this method is supplied with datasection string it
         * will return the generated Html_Table_Row object.
         *
         * @param string|object $dataSection
         * @param array $attributes
         * @return Html_Table_Row|Html_Table
         */
        public function addRow($dataSection, $attributes = array())
        {
            if (is_object($dataSection) && $dataSection instanceof Html_Table_Row)
            {
                $section = '_' . $dataSection->section;
                array_push($this->{$section}, $dataSection);
                return $this;
            }

            $section = '_' . $dataSection;
            $i       = count($this->{$section});

            array_push($this->{$section}, New Html_Table_Row($dataSection));

            foreach ($attributes as $identifier => $value)
            {
                $this->{$section}[$i]->addAttribute($identifier, $value);
            }

            return $this->{$section}[$i];
        }

        /**
         * This method allows to add an attribute to the table. Each attribute will be applied to the table
         * and not to rows/columns. Examples of such attributes are style, class, border, etc.
         *
         * @param string $identifier
         * @param string $value
         * @return Html_Table
         */
        public function addAttribute($identifier, $value)
        {
            $this->_attributes[$identifier] = $value;
            return $this;
        }

        /**
         * This method provides access to create or retrieve attributes based on the identifier. If a value
         * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
         * are supplied it adds the attribute to the array.
         *
         * @param string $identifier
         * @param string $value
         * @return string|Html_Table
         */
        public function attr($identifier, $value = null)
        {
            return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
        }

        /**
         * Retrieve the HTML for the complete table. If $tidy resolves to True the generated HTML will be
         * indented with the use of tabs. If $tabsToSpaces is set these tabs will be replaced with the number
         * of spaces provided.
         *
         * @param boolean $tidy
         * @param integer $tabsToSpaces
         * @return string
         */
        public function getHtml($tidy = true, $tabsToSpaces = null)
        {
            $attributes = '';
            $htmlOutput = '';

            foreach ($this->_attributes as $identifier => $value)
            {
                $attributes.= ' ' . $identifier . '="' . $value . '"';
            }

            $htmlOutput = '<table' . $attributes . '>' . "\n";

            if (count($this->_head) > 0)
            {
                $htmlOutput .= "\t" . '<thead>' . "\n";

                foreach($this->_head as $tableHeadRow)
                {
                    $htmlOutput .= $tableHeadRow->getHtml();
                }

                $htmlOutput .= "\t" . '</thead>' . "\n";
            }

            if (count($this->_body) > 0)
            {
                $htmlOutput .= "\t" . '<tbody>' . "\n";

                foreach($this->_body as $tableBodyRow)
                {
                    $htmlOutput .= $tableBodyRow->getHtml();
                }

                $htmlOutput .= "\t" . '</tbody>' . "\n";
            }

            if (count($this->_foot) > 0)
            {
                $htmlOutput .= "\t" . '<tfoot>' . "\n";

                foreach($this->_foot as $tableFootRow)
                {
                    $htmlOutput .= $tableFootRow->getHtml();
                }

                $htmlOutput .= "\t" . '</tfoot>' . "\n";
            }

            $htmlOutput .= '</table>';

            if (!$tidy)
            {
                $htmlOutput = str_replace(array("\t", "\n"), '', $htmlOutput);
            }
            else if (!is_null($tabsToSpaces) && is_numeric($tabsToSpaces))
            {
                $htmlOutput = str_replace("\t", str_repeat(' ', $tabsToSpaces), $htmlOutput);
            }

            return $htmlOutput;
        }

        /**
         * Magic method
         *
         * @param string $identifier
         * @param string $value
         */
        public function __set($identifier, $value)
        {
            $this->addAttribute($identifier, $value);
        }

        /**
         * Magic method. If the head, body or foot are requested it will return the array containing the
         * Html_Table_Row elements for the requested section. It is also possible to return the complete
         * array with attributes. If there is an attribute available with the identifier provided it will
         * return this. Else the method will return false.
         *
         * @param string $identifier
         * @return mixed
         */
        public function __get($identifier)
        {
            Switch ($identifier)
            {
                case 'head':
                case 'body':
                case 'foot':
                case 'attributes':
                    $identifier = '_' . $identifier;
                    return $this->{$identifier};
                    break;

                default:
                    if (isset($this->_attributes[$identifier]))
                    {
                        return $this->_attributes[$identifier];
                    }

                    return false;
            }
        }

        /**
         * Magic method
         *
         * @return string
         */
        public function __toString()
        {
            return $this->getHtml();
        }
    }

[b]Html_Table_Row[/b]
[code=php]    /**
     * The Html_Table_Row class can be used to instantiate objects representing rows in an HTML table.
     *
     * @author Gerard Klomp <gerard.klomp@sitemasters.be>
     * @version 1.0
     * @license http://sitemasters.be/mit-license.txt MIT License
     */
    class Html_Table_Row
    {
        private $_attributes;
        private $_columns;
        private $_section;

        /**
         * Each table row element must specify if it is part of the head, body of foot of the table.
         *
         * @param string $section
         */
        public function __construct($section = 'body')
        {
            $this->_attributes = array();
            $this->_columns    = array();
            $this->_section    = $section;
        }

        /**
         * This method allows to add a column to a row of the table. The first argument must be either a
         * string providing the content of the column or and Html_Table_Column object. This method will 
         * return itself, presenting the user with the option to make use of fluent interfaces. If the 
         * current row is part of the head the column will be automaticly converted to a th, else it will 
         * remain the default td.
         *
         * @param string|Html_Table_Column $data
         * @param array $attributes
         * @return Html_Table_Column|Html_Table_Row
         */
        public function addColumn($data, $attributes = array())
        {
            if (is_object($data) && $data instanceof Html_Table_Column)
            {
                $data->type       = $this->_section == 'head' ? 'th' : 'td';
                $this->_columns[] = $data;
                return $this;
            }

            $i                        = count($this->_columns);
            $this->_columns[$i]       = New Html_Table_Column($data);
            $this->_columns[$i]->type = $this->_section == 'head' ? 'th' : 'td';

            foreach ($attributes as $identifier => $value)
            {
                $this->_columns[$i]->addAttribute($identifier, $value);
            }

            return $this;
        }

        /**
         * Updates the type of all columns belonging to this row to the represented value. HTML valid options
         * are th and td. Can be used to force header columns as a normal column, else it will be automaticly
         * converted to a header column.
         *
         * @param string $type
         * @return Html_Table_Row
         */
        public function setColumnType($type)
        {
            foreach($this->columns as $column)
            {
                $column->type = $type;
            }

            return $this;
        }

        /**
         * This method allows to add an attribute to the table row. Each attribute will be applied to the
         * table row and not to columns. Examples of such attributes are style, class, border, etc.
         *
         * @param string $identifier
         * @param string $value
         * @return Html_Table_Row
         */
        public function addAttribute($identifier, $value)
        {
            $this->_attributes[$identifier] = $value;
            return $this;
        }

        /**
         * This method provides access to create or retrieve attributes based on the identifier. If a value
         * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
         * are supplied it adds the attribute to the array.
         *
         * @param string $identifier
         * @param string $value
         * @return string|Html_Table_Row
         */
        public function attr($identifier, $value = null)
        {
            return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
        }

        /**
         * Retrieve the HTML for the complete table row.
         *
         * @return string
         */
        public function getHtml()
        {
            $attributes = '';
            $htmlOutput = '';

            foreach ($this->_attributes as $identifier => $value)
            {
                $attributes.= ' ' . $identifier . '="' . $value . '"';
            }

            $htmlOutput = "\t\t" . '<tr' . $attributes . '>' . "\n";

            foreach($this->_columns as $column)
            {
                $htmlOutput .= $column->getHtml();
            }

            return $htmlOutput . "\t\t" . '</tr>' . "\n";
        }

        /**
         * Magic method. It offers the option to update the section to which the row belongs or to set a
         * single attribute.
         *
         * @param string $identifier
         * @param string $value
         */
        public function __set($identifier, $value)
        {
            Switch ($identifier)
            {
                case 'section':
                    $this->_section = $value;
                    $this->setColumnType($value == 'head' ? 'th' : 'td');
                    break;

                default:
                    $this->addAttribute($identifier, $value);
            }
        }

        /**
         * Magic method. It offers the option to retrieve all attributes, columns or the name of the current
         * section. If a string is provided it will also check if there is a single attribute available with
         * this identifier.
         *
         * @param string $identifier
         * @return mixed
         */
        public function __get($identifier)
        {
            Switch ($identifier)
            {
                case 'attributes':
                case 'columns':
                case 'section':
                    $identifier = '_' . $identifier;
                    return $this->{$identifier};
                    break;

                default:
                    if (isset($this->_attributes[$identifier]))
                    {
                        return $this->_attributes[$identifier];
                    }

                    return false;
            }
        }

        /**
         * Magic method
         *
         * @return string
         */
        public function __toString()
        {
            return $this->getHtml();
        }
    }

[b]Html_Table_Column[/b]
[code=php]    /**
     * The Html_Table_Column class can be used to instantiate objects representing columns in an HTML table.
     *
     * @author Gerard Klomp <gerard.klomp@sitemasters.be>
     * @version 1.0
     * @license http://sitemasters.be/mit-license.txt MIT License
     */
    class Html_Table_Column
    {
        private $_attributes;
        private $_value;
        private $_type;

        /**
         * Each table column element has to have data. It is allowed to supply an empty string. The default
         * type for a new column is td.
         *
         * @param string $section
         */
        public function __construct($value)
        {
            $this->_attributes = array();
            $this->_value      = $value;
            $this->_type       = 'td';
        }

        /**
         * This method allows to add an attribute to the table column. Examples of such attributes are style,
         * class, border, etc.
         *
         * @param string $identifier
         * @param string $value
         * @return Html_Table_Column
         */
        public function addAttribute($identifier, $value)
        {
            $this->_attributes[$identifier] = $value;
            return $this;
        }

        /**
         * This method provides access to create or retrieve attributes based on the identifier. If a value
         * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
         * are supplied it adds the attribute to the array.
         *
         * @param string $identifier
         * @param string $value
         * @return string|Html_Table_Column
         */
        public function attr($identifier, $value = null)
        {
            return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
        }

        /**
         * Retrieve the HTML for the table column.
         *
         * @return string
         */
        public function getHtml()
        {
            $attributes = '';

            foreach ($this->_attributes as $identifier => $value)
            {
                $attributes.= ' ' . $identifier . '="' . $value . '"';
            }

            return "\t\t\t" . '<' . $this->_type . $attributes . '>'
                 . $this->_value
                 . '</' . $this->_type . '>' . "\n";
        }

        /**
         * Magic method. It offers the option to update the type of the column and the content. It also
         * offers the option to add a single attribute to the column.
         *
         * @param string $identifier
         * @param string $value
         */
        public function __set($identifier, $value)
        {
            Switch($identifier)
            {
                case 'type':
                    $this->_type = $value;
                    break;

                case 'value':
                    $this->_value = $value;
                    break;

                default:
                    $this->addAttribute($identifier, $value);
            }
        }

        /**
         * Magic method. It offers the option to retrieve all attributes, typ or the data of the current
         * column. If a string is provided it will also check if there is a single attribute available with
         * this identifier.
         *
         * @param string $identifier
         * @return mixed
         */
        public function __get($identifier)
        {
            Switch ($identifier)
            {
                case 'attributes':
                case 'type':
                case 'value':
                    $identifier = '_' . $identifier;
                    return $this->{$identifier};
                    break;

                default:
                    if (isset($this->_attributes[$identifier]))
                    {
                        return $this->_attributes[$identifier];
                    }

                    return false;
            }
        }

        /**
         * Magic method
         *
         * @return string
         */
        public function __toString()
        {
            return $this->getHtml();
        }
    }

[b]Voorbeeld[/b]
[code=php]    // Inladen van de class
    require_once 'Table.class.php';

    // Opmaken van een eenvoudige tabel met enkele attributen voor de table en één kolom
    $table = New Html_Table;
    $table->attr('width', '100%')->attr('style', 'border: 1px solid red;')->attr('id', 'overview-table');
    $table->addRow('head')->addColumn('Title')->addColumn('Author')->addColumn('Views');
    $table->addRow('body')->addColumn('Leven in het wild')->addColumn('Gerard Klomp', array('style' => 'color: red;'))->addColumn('10000');
    $table->addRow('body')->addColumn('Leven in het licht')->addColumn('Koen van den Wijngaert')->addColumn('10000');
    $table->addRow('foot')->addColumn('')->addColumn('')->addColumn('20000');

    // Toevoegen van een body rij met enkele kolommen, maar nu met de objecten
    $darkRow      = New Html_Table_Row('body');

    $titleColumn  = New Html_Table_Column('Leven in het donker');
    $authorColumn = New Html_Table_Column('Ralph Thielen');
    $viewsColumn  = New Html_Table_Column('0');

    $darkRow->addColumn($titleColumn)->addColumn($authorColumn)->addColumn($viewsColumn);
    
    $table->addRow($darkRow);

    // Tonen van de tabel met tabs voor indenting.
    echo $table->getHtml();

    // Tonen van de tabel met 4 spaties voor inspringing
    echo $table->getHtml(true, 4);

    // Tonen van de tabel zonder inspringing
    echo $table->getHtml();

    // Tonen van de tabel met gebruik van de magic __toString() methode, is gelijk aan $table->getHtml()
    echo $table;