adaniel Thu Mar 22 18:14:05 2001 EDT Modified files: /php4/pear/HTML Table.php Log: moved the display method to HTML_Common and updated some comments
Index: php4/pear/HTML/Table.php diff -u php4/pear/HTML/Table.php:1.3 php4/pear/HTML/Table.php:1.4 --- php4/pear/HTML/Table.php:1.3 Thu Mar 22 06:32:51 2001 +++ php4/pear/HTML/Table.php Thu Mar 22 18:14:05 2001 @@ -17,184 +17,184 @@ // | Bertrand Mansion <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: Table.php,v 1.3 2001/03/22 14:32:51 mj Exp $ +// $Id: Table.php,v 1.4 2001/03/23 02:14:05 adaniel Exp $ require_once "PEAR.php"; require_once "HTML/Common.php"; /** -* Builds an HTML table -* -* @author Adam Daniel <[EMAIL PROTECTED]> -* @author Bertrand Mansion <[EMAIL PROTECTED]> -* @version 1.5 -* @since PHP 4.0.3pl1 -* -* Example: -* $table = new HTML_Table; -* ... -*/ + * Builds an HTML table + * + * @author Adam Daniel <[EMAIL PROTECTED]> + * @author Bertrand Mansion <[EMAIL PROTECTED]> + * @version 1.5 + * @since PHP 4.0.3pl1 + * + * Example: + * $table = new HTML_Table; + * ... + */ class HTML_Table extends HTML_Common { - /** - * Automatically adds a new row or column if a given row or column index does not exist - * @var bool - * @access private - */ - var $_autoGrow = true; - - /** - * Value to insert into empty cells - * @var string - * @access private - */ - var $_autoFill = " "; - - /** - * Array containing the table structure - * @var array - * @access private - */ - var $_structure = array(); - - /** - * Number of rows composing in the table - * @var int - * @access private - */ - var $_rows = 0; - - /** - * Number of column composing the table - * @var int - * @access private - */ - var $_cols = 0; - - /** - * Class constructor - * @param array $attributes Associative array of table tag attributes - * @param int $tabOffset - * @access public - */ - function HTML_Table($attributes=null, $tabOffset=0) - { + /** + * Automatically adds a new row or column if a given row or column index does not +exist + * @var bool + * @access private + */ + var $_autoGrow = true; + + /** + * Value to insert into empty cells + * @var string + * @access private + */ + var $_autoFill = " "; + + /** + * Array containing the table structure + * @var array + * @access private + */ + var $_structure = array(); + + /** + * Number of rows composing in the table + * @var int + * @access private + */ + var $_rows = 0; + + /** + * Number of column composing the table + * @var int + * @access private + */ + var $_cols = 0; + + /** + * Class constructor + * @param array $attributes Associative array of table tag attributes + * @param int $tabOffset + * @access public + */ + function HTML_Table($attributes=null, $tabOffset=0) + { $commonVersion = 1.3; if (HTML_Common::apiVersion() < $commonVersion) { return new PEAR_Error("HTML_Table version " . $this->apiVersion() . " requires " . "HTML_Common version $commonVersion or greater.", 0, PEAR_ERROR_TRIGGER); } - HTML_Common::HTML_Common($attributes, $tabOffset); - } // end constructor + HTML_Common::HTML_Common($attributes, $tabOffset); + } // end constructor - /** - * Returns the API version - * @access public - * @returns double - */ - function apiVersion() - { - return 1.5; - } // end func apiVersion - - /** - * Sets the table caption - * @param string $caption - * @param mixed $attributes Associative array or string of table row attributes - * @access public - */ - function setCaption($caption, $attributes=null) - { - $attributes = $this->_parseAttributes($attributes); - $this->_structure["caption"] = array("attr"=>$attributes, "contents"=>$caption); - } // end func setCaption - - /** - * Sets the autoFill value - * @param mixed $fill - * @access public - */ + /** + * Returns the API version + * @access public + * @returns double + */ + function apiVersion() + { + return 1.5; + } // end func apiVersion + + /** + * Sets the table caption + * @param string $caption + * @param mixed $attributes Associative array or string of table row +attributes + * @access public + */ + function setCaption($caption, $attributes=null) + { + $attributes = $this->_parseAttributes($attributes); + $this->_structure["caption"] = array("attr"=>$attributes, +"contents"=>$caption); + } // end func setCaption + + /** + * Sets the autoFill value + * @param mixed $fill + * @access public + */ function setAutoFill($fill) { $this->_autoFill = $fill; } // end func setAutoFill /** - * Returns the autoFill value - * @access public - * @returns mixed - */ + * Returns the autoFill value + * @access public + * @returns mixed + */ function getAutoFill() { return $this->_autoFill; } // end func getAutoFill /** - * Sets the autoGrow value - * @param bool $fill - * @access public - */ + * Sets the autoGrow value + * @param bool $fill + * @access public + */ function setAutoGrow($grow) { $this->_autoGrow = $grow; } // end func setAutoGrow /** - * Returns the autoGrow value - * @access public - * @returns mixed - */ + * Returns the autoGrow value + * @access public + * @returns mixed + */ function getAutoGrow() { return $this->_autoGrow; } // end func getAutoGrow /** - * Sets the number of rows in the table - * @param int $rows - * @access public - */ + * Sets the number of rows in the table + * @param int $rows + * @access public + */ function setRowCount($rows) { $this->_rows = $rows; } // end func setRowCount /** - * Sets the number of columns in the table - * @param int $cols - * @access public - */ + * Sets the number of columns in the table + * @param int $cols + * @access public + */ function setColCount($cols) { $this->_cols = $cols; } // end func setColCount /** - * Returns the number of rows in the table - * @access public - * @returns int - */ + * Returns the number of rows in the table + * @access public + * @returns int + */ function getRowCount() { return $this->_rows; } // end func getRowCount /** - * Sets the number of columns in the table - * @access public - * @returns int - */ + * Sets the number of columns in the table + * @access public + * @returns int + */ function getColCount() { return $this->_cols; } // end func getColCount /** - * Sets a rows type 'TH' or 'TD' - * @param int $row Row index - * @param string $type 'TH' or 'TD' - * @access public - */ + * Sets a rows type 'TH' or 'TD' + * @param int $row Row index + * @param string $type 'TH' or 'TD' + * @access public + */ function setRowType($row, $type) { @@ -204,11 +204,11 @@ } // end func setRowType /** - * Sets a columns type 'TH' or 'TD' - * @param int $col Column index - * @param string $type 'TH' or 'TD' - * @access public - */ + * Sets a columns type 'TH' or 'TD' + * @param int $col Column index + * @param string $type 'TH' or 'TD' + * @access public + */ function setColType($col, $type) { for ($counter=0; $counter < $this->_rows; $counter++) { @@ -216,124 +216,124 @@ } } // end func setColType - /** - * Sets the cell attributes for an existing cell. - * - * If the given indices do not exist and autoGrow is true then the given - * row and/or col is automatically added. If autoGrow is false then an - * error is returned. - * @param int $row Row index - * @param int $col Column index - * @param mixed $attributes Associative array or string of table row attributes - * @access public - * @throws PEAR_Error - */ - function setCellAttributes($row, $col, $attributes) - { + /** + * Sets the cell attributes for an existing cell. + * + * If the given indices do not exist and autoGrow is true then the given + * row and/or col is automatically added. If autoGrow is false then an + * error is returned. + * @param int $row Row index + * @param int $col Column index + * @param mixed $attributes Associative array or string of table row +attributes + * @access public + * @throws PEAR_Error + */ + function setCellAttributes($row, $col, $attributes) + { if ($this->_structure[$row][$col] == "SPANNED") return; - if ($row >= $this->_rows) { - if ($this->_autoGrow) { - $this->_rows = $row+1; - } else { - return new PEAR_Error("Invalid table row reference[$row] in HTML_Table::setCellAttributes"); - } - } - if ($col >= $this->_cols) { - if ($this->_autoGrow) { - $this->_cols = $col+1; - } else { - return new PEAR_Error("Invalid table column reference[$col] in HTML_Table::setCellAttributes"); - } - } - $attributes = $this->_parseAttributes($attributes); - $this->_structure[$row][$col]["attr"] = $attributes; - $this->_updateSpanGrid($row, $col); - } // end func setCellAttributes - - /** - * Updates the cell attributes passed but leaves other existing attributes in tact - * @param int $row Row index - * @param int $col Column index - * @param mixed $attributes Associative array or string of table row attributes - * @access public - */ + if ($row >= $this->_rows) { + if ($this->_autoGrow) { + $this->_rows = $row+1; + } else { + return new PEAR_Error("Invalid table row reference[$row] in +HTML_Table::setCellAttributes"); + } + } + if ($col >= $this->_cols) { + if ($this->_autoGrow) { + $this->_cols = $col+1; + } else { + return new PEAR_Error("Invalid table column reference[$col] in +HTML_Table::setCellAttributes"); + } + } + $attributes = $this->_parseAttributes($attributes); + $this->_structure[$row][$col]["attr"] = $attributes; + $this->_updateSpanGrid($row, $col); + } // end func setCellAttributes + + /** + * Updates the cell attributes passed but leaves other existing attributes in tact + * @param int $row Row index + * @param int $col Column index + * @param mixed $attributes Associative array or string of table row +attributes + * @access public + */ function updateCellAttributes($row, $col, $attributes) { if ($this->_structure[$row][$col] == "SPANNED") return; $attributes = $this->_parseAttributes($attributes); $this->_updateAttrArray($this->_structure[$row][$col]["attr"], $attributes); - $this->_updateSpanGrid($row, $col); + $this->_updateSpanGrid($row, $col); } // end func updateCellAttributes - /** - * Sets the cell contents for an existing cell - * - * If the given indices do not exist and autoGrow is true then the given - * row and/or col is automatically added. If autoGrow is false then an - * error is returned. - * @param int $row Row index - * @param int $col Column index - * @param mixed $contents May contain html or any object with a toHTML method - * @param string $type (optional) Cell type either 'TH' or 'TD' - * @access public - * @throws PEAR_Error - */ - function setCellContents($row, $col, $contents, $type='TD') - { + /** + * Sets the cell contents for an existing cell + * + * If the given indices do not exist and autoGrow is true then the given + * row and/or col is automatically added. If autoGrow is false then an + * error is returned. + * @param int $row Row index + * @param int $col Column index + * @param mixed $contents May contain html or any object with a toHTML +method + * @param string $type (optional) Cell type either 'TH' or 'TD' + * @access public + * @throws PEAR_Error + */ + function setCellContents($row, $col, $contents, $type='TD') + { if ($this->_structure[$row][$col] == "SPANNED") return; - if ($row >= $this->_rows) { - if ($this->_autoGrow) { - $this->_rows = $row+1; - } else { - return new PEAR_Error("Invalid table row reference[$row] in HTML_Table::setCellContents"); - } - } - if ($col >= $this->_cols) { - if ($this->_autoGrow) { - $this->_cols = $col+1; - } else { - return new PEAR_Error("Invalid table column reference[$col] in HTML_Table::setCellContents"); - } - } - $this->_structure[$row][$col]["contents"] = $contents; - $this->_structure[$row][$col]["type"] = $type; - } // end func setCellContents - - /** - * Returns the cell contents for an existing cell - * @param int $row Row index - * @param int $col Column index - * @access public - * @return mixed - */ - function getCellContents($row, $col) - { + if ($row >= $this->_rows) { + if ($this->_autoGrow) { + $this->_rows = $row+1; + } else { + return new PEAR_Error("Invalid table row reference[$row] in +HTML_Table::setCellContents"); + } + } + if ($col >= $this->_cols) { + if ($this->_autoGrow) { + $this->_cols = $col+1; + } else { + return new PEAR_Error("Invalid table column reference[$col] in +HTML_Table::setCellContents"); + } + } + $this->_structure[$row][$col]["contents"] = $contents; + $this->_structure[$row][$col]["type"] = $type; + } // end func setCellContents + + /** + * Returns the cell contents for an existing cell + * @param int $row Row index + * @param int $col Column index + * @access public + * @return mixed + */ + function getCellContents($row, $col) + { if ($this->_structure[$row][$col] == "SPANNED") return; - return $this->_structure[$row][$col]["contents"]; - } // end func getCellContents + return $this->_structure[$row][$col]["contents"]; + } // end func getCellContents /** - * Sets the contents of a header cell - * @param int $row - * @param int $col - * @param mixed $contents - * @access public - */ + * Sets the contents of a header cell + * @param int $row + * @param int $col + * @param mixed $contents + * @access public + */ function setHeaderContents($row, $col, $contents) { $this->setCellContents($row, $col, $contents, 'TH'); } // end func setHeaderContents - /** - * Adds a table row and returns the row identifier - * @param array $contents (optional) Must be a indexed array of valid cell contents - * @param mixed $attributes (optional) Associative array or string of table row attributes - * @param string $type (optional) Cell type either 'TH' or 'TD' - * @returns int - * @access public - */ - function addRow($contents=null, $attributes=null, $type='TD') - { + /** + * Adds a table row and returns the row identifier + * @param array $contents (optional) Must be a indexed array of valid +cell contents + * @param mixed $attributes (optional) Associative array or string of table +row attributes + * @param string $type (optional) Cell type either 'TH' or 'TD' + * @returns int + * @access public + */ + function addRow($contents=null, $attributes=null, $type='TD') + { if (isset($contents) && !is_array($contents)) { return new PEAR_Error("First parameter to HTML_Table::addRow must be an array"); } @@ -349,57 +349,57 @@ return $row; } // end func addRow - /** - * Sets the row attributes for an existing row - * @param int $row Row index - * @param mixed $attributes Associative array or string of table row attributes - * @access public - */ - function setRowAttributes($row, $attributes) - { - for ($i = 0; $i < $this->_cols; $i++) { - $this->setCellAttributes($row,$i,$attributes); - } - } // end func setRowAttributes - - /** - * Updates the row attributes for an existing row - * @param int $row Row index - * @param mixed $attributes Associative array or string of table row attributes - * @access public - */ - function updateRowAttributes($row, $attributes=null) - { - for ($i = 0; $i < $this->_cols; $i++) { - $this->updateCellAttributes($row,$i,$attributes); - } - } // end func updateRowAttributes - - /** - * Alternates the row attributes starting at $start - * @param int $start Row index of row in which alternatign begins - * @param mixed $attributes1 Associative array or string of table row attributes - * @param mixed $attribute2 Associative array or string of table row attributes - * @access public - */ - function altRowAttributes($start, $attributes1, $attributes2) - { - for ($row = $start ; $row < $this->_rows ; $row++) { - $attributes = (($row+1+$start)%2==0) ? $attributes1 : $attributes2; - $this->updateRowAttributes($row, $attributes); - } - } // end func altRowAttributes - - /** - * Adds a table column and returns the column identifier - * @param array $contents (optional) Must be a indexed array of valid cell contents - * @param mixed $attributes (optional) Associative array or string of table row attributes - * @param string $type (optional) Cell type either 'TH' or 'TD' - * @returns int - * @access public - */ - function addCol($contents=null, $attributes=null, $type='TD') - { + /** + * Sets the row attributes for an existing row + * @param int $row Row index + * @param mixed $attributes Associative array or string of table row +attributes + * @access public + */ + function setRowAttributes($row, $attributes) + { + for ($i = 0; $i < $this->_cols; $i++) { + $this->setCellAttributes($row,$i,$attributes); + } + } // end func setRowAttributes + + /** + * Updates the row attributes for an existing row + * @param int $row Row index + * @param mixed $attributes Associative array or string of table row +attributes + * @access public + */ + function updateRowAttributes($row, $attributes=null) + { + for ($i = 0; $i < $this->_cols; $i++) { + $this->updateCellAttributes($row,$i,$attributes); + } + } // end func updateRowAttributes + + /** + * Alternates the row attributes starting at $start + * @param int $start Row index of row in which alternatign +begins + * @param mixed $attributes1 Associative array or string of table row +attributes + * @param mixed $attribute2 Associative array or string of table row +attributes + * @access public + */ + function altRowAttributes($start, $attributes1, $attributes2) + { + for ($row = $start ; $row < $this->_rows ; $row++) { + $attributes = (($row+1+$start)%2==0) ? $attributes1 : $attributes2; + $this->updateRowAttributes($row, $attributes); + } + } // end func altRowAttributes + + /** + * Adds a table column and returns the column identifier + * @param array $contents (optional) Must be a indexed array of valid +cell contents + * @param mixed $attributes (optional) Associative array or string of table +row attributes + * @param string $type (optional) Cell type either 'TH' or 'TD' + * @returns int + * @access public + */ + function addCol($contents=null, $attributes=null, $type='TD') + { if (isset($contents) && !is_array($contents)) { return new PEAR_Error("First parameter to HTML_Table::addCol must be an array"); } @@ -409,61 +409,62 @@ } $this->setColAttributes($col, $attributes); return $col; - } // end func addCol + } // end func addCol - /** - * Sets the column attributes for an existing column - * @param int $col Column index - * @param mixed $attributes (optional) Associative array or string of table row attributes - * @access public - */ - function setColAttributes($col, $attributes=null) - { - for ($i = 0; $i < $this->_rows; $i++) { - $this->setCellAttributes($i,$col,$attributes); - } - } // end func setColAttributes - - /** - * Updates the column attributes for an existing column - * @param int $col Column index - * @param mixed $attributes (optional) Associative array or string of table row attributes - * @access public - */ - function updateColAttributes($col, $attributes=null) - { - for ($i = 0; $i < $this->_rows; $i++) { - $this->updateCellAttributes($i,$col,$attributes); - } - } // end func updateColAttributes - - /** - * Returns the table structure as HTML - * @access public - */ - function toHtml() - { - $tabs = $this->_getTabs(); - $strHtml .= "\n" . $tabs . "<TABLE" . $this->_getAttrString($this->_attributes) . ">\n"; - if ($this->_structure["caption"]) { - $attr = $this->_structure["caption"]["attr"]; - $contents = $this->_structure["caption"]["contents"]; - $strHtml .= $tabs . "\t<CAPTION" . $this->_getAttrString($attr) . ">"; - if (is_array($contents)) $contents = implode(", ",$contents); - $strHtml .= $contents; - $strHtml .= "</CAPTION>\n"; - } - for ($i = 0 ; $i < $this->_rows ; $i++) { - $strHtml .= $tabs ."\t<TR>\n"; - for ($j = 0 ; $j < $this->_cols ; $j++) { - if ($this->_structure[$i][$j] == "SPANNED") { - $strHtml .= $tabs ."\t\t<!-- span -->\n"; - continue; - } - $type = ($this->_structure[$i][$j]["type"] == "TH" ? "TH" : "TD"); - $attr = $this->_structure[$i][$j]["attr"]; - $contents = $this->_structure[$i][$j]["contents"]; - $strHtml .= $tabs . "\t\t<$type" . $this->_getAttrString($attr) . ">"; + /** + * Sets the column attributes for an existing column + * @param int $col Column index + * @param mixed $attributes (optional) Associative array or string +of table row attributes + * @access public + */ + function setColAttributes($col, $attributes=null) + { + for ($i = 0; $i < $this->_rows; $i++) { + $this->setCellAttributes($i,$col,$attributes); + } + } // end func setColAttributes + + /** + * Updates the column attributes for an existing column + * @param int $col Column index + * @param mixed $attributes (optional) Associative array or string +of table row attributes + * @access public + */ + function updateColAttributes($col, $attributes=null) + { + for ($i = 0; $i < $this->_rows; $i++) { + $this->updateCellAttributes($i,$col,$attributes); + } + } // end func updateColAttributes + + /** + * Returns the table structure as HTML + * @access public + * @return string + */ + function toHtml() + { + $tabs = $this->_getTabs(); + $strHtml .= "\n" . $tabs . "<TABLE" . +$this->_getAttrString($this->_attributes) . ">\n"; + if ($this->_structure["caption"]) { + $attr = $this->_structure["caption"]["attr"]; + $contents = $this->_structure["caption"]["contents"]; + $strHtml .= $tabs . "\t<CAPTION" . $this->_getAttrString($attr) . ">"; + if (is_array($contents)) $contents = implode(", ",$contents); + $strHtml .= $contents; + $strHtml .= "</CAPTION>\n"; + } + for ($i = 0 ; $i < $this->_rows ; $i++) { + $strHtml .= $tabs ."\t<TR>\n"; + for ($j = 0 ; $j < $this->_cols ; $j++) { + if ($this->_structure[$i][$j] == "SPANNED") { + $strHtml .= $tabs ."\t\t<!-- span -->\n"; + continue; + } + $type = ($this->_structure[$i][$j]["type"] == "TH" ? "TH" : "TD"); + $attr = $this->_structure[$i][$j]["attr"]; + $contents = $this->_structure[$i][$j]["contents"]; + $strHtml .= $tabs . "\t\t<$type" . $this->_getAttrString($attr) . ">"; if (is_object($contents)) { if (method_exists($contents, "toHtml")) { $contents = $contents->toHtml(); @@ -472,35 +473,26 @@ } } if (is_array($contents)) $contents = implode(", ",$contents); - if (isset($this->_autoFill) && $contents == "") $contents = $this->_autoFill; - $strHtml .= $contents; - $strHtml .= "</$type>\n"; - } - $strHtml .= $tabs ."\t</TR>\n"; - } - $strHtml .= $tabs . "</TABLE>"; - return $strHtml; - } // end func toHtml - - /** - * Prints the HTML table to the screen - * @access public - */ - function display() - { - print $this->toHtml(); - } // end func display - - /** - * Checks if rows or columns are spanned - * @param int $row Row index - * @param int $col Column index - * @access private - */ - function _updateSpanGrid($row, $col) - { - $colspan = $this->_structure[$row][$col]["attr"]["colspan"]; - $rowspan = $this->_structure[$row][$col]["attr"]["rowspan"]; + if (isset($this->_autoFill) && $contents == "") $contents = +$this->_autoFill; + $strHtml .= $contents; + $strHtml .= "</$type>\n"; + } + $strHtml .= $tabs ."\t</TR>\n"; + } + $strHtml .= $tabs . "</TABLE>"; + return $strHtml; + } // end func toHtml + + /** + * Checks if rows or columns are spanned + * @param int $row Row index + * @param int $col Column index + * @access private + */ + function _updateSpanGrid($row, $col) + { + $colspan = $this->_structure[$row][$col]["attr"]["colspan"]; + $rowspan = $this->_structure[$row][$col]["attr"]["rowspan"]; if ($colspan) { for ($j = $col+1; (($j < $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) { $this->_structure[$row][$j] = "SPANNED"; @@ -518,7 +510,7 @@ } } } - } // end func _updateSpanGrid + } // end func _updateSpanGrid } // end class HTML_Table ?>
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]