Re: HTML_Table.php
| From: | Bertrand Mansion | Date: | Fri, 09 Mar 2001 17:57:02 +0000 |
| Subject: | Re: HTML_Table.php | ||
| References: | 1 | Groups: | php.pear |
| Request: | Send a blank email to php-pear+get-1750@lists.php.net to get a copy of this message | ||
le 9/03/01 18:04, Jon Parise à jon@csh.rit.edu a écrit :
> On Fri, Mar 09, 2001 at 01:37:46PM +0100, Bertrand Mansion wrote:
>
>> Now I am wondering what I should do with it. I guess it should go in
>> /PEAR/HTML with HTML_Form.php but I would like people to review the code and
>> tell me if it could work better.
>
> That sounds like the correct place for it.
>
>> Could someone give me instructions on what I am supposed to do now ?
>
> Post your code to the list. People will (hopefully) review and
> comment. Then, someone will (probably) commit it to the PEAR
> repository. If you're interested in maintaining the code yourself,
> apply for a PEAR cvs account.
The code of HTML_Table class is attached. Thank for reviewing it. I am
testing it with my own apps at the moment. Please send me your comments,
feedback and critics.
I would not mind maintaining it if someone think this code can fit in there..
Please let me know, then I will apply for an account.
Bertrand Mansion
http://www.tapage.fr
<?php // // $Id: HTML_Table.php,v 1.2 2001/03/09 14:01:25 mansion Exp $ // /** * HTML table utility functions. * * This class helps you create html tables on the fly. * It is easy to use and yet powerful enough to create almost any kind * of tables you would like. You can also create a table into another * table cell. It makes php code easier to read and maintain, with * less html tags in it. * * Tables can be sized dynamically and you can add rows or columns as * you want using addRow or addCol methods. * * html attributes are given to methods with arrays which allow you * to define and reuse your own styles. All valid HTML 3.0 attributes * should be supported. * * Example : * * $table = new HTML_Table(array( "rows" => 3, "cols" => 3, "bgcolor" => "#CCCCEE", "border" => 3, "width" => 400, "cellpadding" => 6, "cellspacing" => 3)); $table->setCellAttributes(2,2,array("bgcolor" => "green")); $table->setCellContent(2,2,"hello"); $content2="world"; $table->setCellContent(3,3,$content2); $table->setRowAttributes(1, array( "bgcolor" => "blue")); $table->setRowAttributes(4, array( "bgcolor" => "yellow", "height" => 80)); $content[1]="hello1"; $content[2]="hello2"; $content[6]="hello6"; $table->addCol($content); $table->setCellAttributes(2,2,array("colspan" => 3, "rowspan" => 3)); $table->showHTML_Table(); */ class HTML_Table { // {{{ properties /** construction parameters of <TABLE> tags */ var $attributes = array(); /** automaticly grows the table if rows or columns are added*/ var $autogrow = 1; /** automaticly fills cells with nobackspaces */ var $autofill = " "; /** if true, replace existing attributes with empty values */ var $overwrite = 0; // }}} // {{{ constructor /** * HTML_Table constructor. * * @param $attributes array of attributes used to create the table * @access public */ function HTML_Table($attributes = "") { $this->rows = $attributes["rows"]; $this->cols = $attributes["cols"]; $this->border = $attributes["border"]; $this->bgcolor = $attributes["bgcolor"]; $this->background = $attributes["background"]; $this->width = $attributes["width"]; $this->cellspacing = $attributes["cellspacing"]; $this->cellpadding = $attributes["cellpadding"]; } // }}} // {{{ makeHTML_Table() function makeHTML_Table () { $html = ""; if ($this->rows == 0 || $this->cols == 0) { return 0; } $html .= "<TABLE"; $html .= ($this->border ? " BORDER='".$this->border."'" : ""); $html .= ($this->bgcolor ? " BGCOLOR='".$this->bgcolor."'" : ""); $html .= ($this->background ? " BACKGROUND='".$this->background."'" : ""); $html .= ($this->width ? " WIDTH='".$this->width."'" : ""); $html .= ($this->cellspacing ? " CELLSPACING='".$this->cellspacing."'" : ""); $html .= ($this->cellpadding ? " CELLPADDING='".$this->cellpadding."'" : ""); $html .= ">\n"; if ($this->caption) { $html .= "<CAPTION"; $html .= ($this->caption_align ? " ALIGN='".$this->caption_align."'" : ""); $html .= ">".$this->caption."</CAPTION>\n"; } for ($i = 1 ; $i <= $this->rows ; $i++) { $html .= "<TR>"; for ($j = 1 ; $j <= $this->cols ; $j++) { if ($this->cellspan["$i:$j"] && $this->cellspan["$i:$j"] == "SPANNED") { $html .= "<!-- span -->"; continue; } if ($this->cellhead["$i:$j"]) { $html .= "<TH"; } else { $html .= "<TD"; } if ($this->cellalign["$i:$j"]) $html .= " ALIGN='".$this->cellalign["$i:$j"]."'"; if ($this->cellvalign["$i:$j"]) $html .=" VALIGN='".$this->cellvalign["$i:$j"]."'"; if ($this->cellwidth["$i:$j"]) $html .= " WIDTH='".$this->cellwidth["$i:$j"]."'"; if ($this->cellheight["$i:$j"]) $html .= " HEIGHT='".$this->cellheight["$i:$j"]."'"; if ($this->cellbgcolor["$i:$j"]) $html .= " BGCOLOR='".$this->cellbgcolor["$i:$j"]."'"; if ($this->cellnowrap["$i:$j"]) $html .= " NOWRAP='".$this->cellnowrap["$i:$j"]."'"; if ($this->cellcolspan["$i:$j"]) $html .= " COLSPAN='".$this->cellcolspan["$i:$j"]."'"; if ($this->cellrowspan["$i:$j"]) $html .= " ROWSPAN='".$this->cellrowspan["$i:$j"]."'"; $html .= ">"; $html .= $this->cellstartstring["$i:$j"]; if ($this->cellcontent["$i:$j"]) { $html .= $this->cellcontent["$i:$j"]; } else { $html .= $this->autofill; } $html .= $this->cellendstring["$i:$j"]; if ($this->cellhead["$i:$j"]) { $html .= "</TH>"; } else { $html .= "</TD>"; } } $html .= "</TR>\n"; } $html .= "</TABLE>\n"; return $html; } // }}} // {{{ showHTML_Table() function showHTML_Table () { print $this->makeHTML_Table(); } // }}} // {{{ autoGrow() function autoGrow ($autogrow = 1) { $this->autogrow = $autogrow; } // }}} // {{{ autoFill() function autoFill ($autofill = " ") { $this->autofill = $autofill; } // }}} // {{{ overWrite() function overWrite ($overwrite = 0) { $this->overwrite = $overwrite; } // }}} // {{{ setTableAttributes() function setTableAttributes ($attributes) { if ($this->overwrite) { $this->bgcolor = $attributes["bgcolor"]; $this->border = $attributes["border"]; $this->width = $attributes["width"]; $this->cellspacing = $attributes["cellspacing"]; $this->cellpadding = $attributes["cellpadding"]; $this->background = $attributes["background"]; } else { if ($attributes["bgcolor"]) $this->bgcolor = $attributes["bgcolor"]; if ($attributes["border"]) $this->border = $attributes["border"]; if ($attributes["width"]) $this->width = $attributes["width"]; if ($attributes["cellspacing"]) $this->cellspacing = $attributes["cellspacing"]; if ($attributes["cellpadding"]) $this->cellpadding = $attributes["cellpadding"]; if ($attributes["background"]) $this->background = $attributes["background"]; } } // }}} // {{{ setCaption() function setCaption ($caption, $caption_align="") { $this->caption = $caption; if ($caption_align == "TOP" || $caption_align == "BOTTOM") { $this->caption_align = $caption_align; } else { $error[] = "setCaption : Invalid Alignment\n"; return false; } } // }}} // {{{ setCellContent() function setCellContent ($row, $col, $content="") { if ($row > $this->row) { if ($this->autogrow) { $this->row = $row; } else { $error[] = "setCellContent : Invalid table row reference"; } } if ($col > $this->col) { if ($this->autogrow) { $this->col = $col; } else { $error[] = "setCellContent : Invalid table column reference"; } } $this->cellcontent["$row:$col"] = $content; } // }}} // {{{ getCell() function getCell ($row, $col) { return $this->cellcontent["$row:$col"]; } // }}} // {{{ setCellAttributes() function setCellAttributes ($row, $col, $attributes) { if ($this->overwrite) { $this->cellalign["$row:$col"] = $attributes["align"]; $this->cellvalign["$row:$col"] = $attributes["valign"]; $this->cellhead["$row:$col"] = $attributes["head"]; $this->cellnowrap["$row:$col"] = $attributes["nowrap"]; $this->cellwidth["$row:$col"] = $attributes["width"]; $this->cellheight["$row:$col"] = $attributes["height"]; $this->cellbgcolor["$row:$col"] = $attributes["bgcolor"]; $this->cellstartstring["$row:$col"] = $attributes["startstring"]; $this->cellendstring["$row:$col"] = $attributes["endstring"]; } else { if ($attributes["align"]) $this->cellalign["$row:$col"] = $attributes["align"]; if ($attributes["valign"]) $this->cellvalign["$row:$col"] = $attributes["valign"]; if ($attributes["head"]) $this->cellhead["$row:$col"] = $attributes["head"]; if ($attributes["nowrap"]) $this->cellnowrap["$row:$col"] = $attributes["nowrap"]; if ($attributes["width"]) $this->cellwidth["$row:$col"] = $attributes["width"]; if ($attributes["height"]) $this->cellheight["$row:$col"] = $attributes["height"]; if ($attributes["bgcolor"]) $this->cellbgcolor["$row:$col"] = $attributes["bgcolor"]; if ($attributes["startstring"]) $this->cellstartstring["$row:$col"] = $attributes["startstring"]; if ($attributes["endstring"]) $this->cellendstring["$row:$col"] = $attributes["endstring"]; } if ($attributes["rowspan"]) { $this->cellrowspan["$row:$col"] = $attributes["rowspan"]; $this->updateSpanGrid ($row, $col); } if ($attributes["colspan"]) { $this->cellcolspan["$row:$col"] = $attributes["colspan"]; $this->updateSpanGrid ($row, $col); } } // }}} // {{{ addRow() function addRow ($content) { end ($content); $col = key ($content); if ($col > $this->cols) { $this->cols = $col; } $this->rows++; for ($i=0;$i <= $col;$i++) { $this->cellcontent["$this->rows:$i"] = $content[$i]; } } // }}} // {{{ setRowAttributes() function setRowAttributes ($row, $attributes) { for ($i = 1; $i <= $this->cols; $i++) { $this->setCellAttributes($row,$i,$attributes); } } // }}} // {{{ addCol() function addCol ($content) { end ($content); $row = key ($content); if ($row > $this->rows) { $this->rows = $row; } $this->cols++; for ($i=0;$i <= $row;$i++) { $this->cellcontent["$i:$this->cols"] = $content[$i]; } } // }}} // {{{ setColAlign() function setColAttributes ($col, $attributes) { for ($i = 1; $i <= $this->rows; $i++) { $this->setCellAttributes($i,$col,$attributes); } } // }}} // {{{ updateSpanGrid() function updateSpanGrid ($row, $col) { $colspan = $this->cellcolspan["$row:$col"]; $rowspan = $this->cellrowspan["$row:$col"]; if ($colspan) { for ($j = $col+1; (($j <= $this->cols) && ($j <= ($col + $colspan - 1))); $j++) { $this->cellspan["$row:$j"] = "SPANNED"; } } if ($rowspan) { for ($i = $row+1; (($i <= $this->rows) && ($i <= ($row + $rowspan - 1))); $i++) { $this->cellspan["$i:$col"] = "SPANNED"; } } if ($colspan && $rowspan) { for ($i = $row+1; (($i <= $this->rows) && ($i <= ($row + $rowspan - 1))); $i++) { for ($j = $col+1; (($j <= $this->cols) && ($j <= ($col + $colspan - 1))); $j++) { $this->cellspan["$i:$j"] = "SPANNED"; } } } } } ?>
<?php // // $Id: HTML_Table.php,v 1.2 2001/03/09 14:01:25 mansion Exp $ // /** * HTML table utility functions. * * This class helps you create html tables on the fly. * It is easy to use and yet powerful enough to create almost any kind * of tables you would like. You can also create a table into another * table cell. It makes php code easier to read and maintain, with * less html tags in it. * * Tables can be sized dynamically and you can add rows or columns as * you want using addRow or addCol methods. * * html attributes are given to methods with arrays which allow you * to define and reuse your own styles. All valid HTML 3.0 attributes * should be supported. * * Example : * * $table = new HTML_Table(array( "rows" => 3, "cols" => 3, "bgcolor" => "#CCCCEE", "border" => 3, "width" => 400, "cellpadding" => 6, "cellspacing" => 3)); $table->setCellAttributes(2,2,array("bgcolor" => "green")); $table->setCellContent(2,2,"hello"); $content2="world"; $table->setCellContent(3,3,$content2); $table->setRowAttributes(1, array( "bgcolor" => "blue")); $table->setRowAttributes(4, array( "bgcolor" => "yellow", "height" => 80)); $content[1]="hello1"; $content[2]="hello2"; $content[6]="hello6"; $table->addCol($content); $table->setCellAttributes(2,2,array("colspan" => 3, "rowspan" => 3)); $table->showHTML_Table(); */ class HTML_Table { // {{{ properties /** construction parameters of <TABLE> tags */ var $attributes = array(); /** automaticly grows the table if rows or columns are added*/ var $autogrow = 1; /** automaticly fills cells with nobackspaces */ var $autofill = " "; /** if true, replace existing attributes with empty values */ var $overwrite = 0; // }}} // {{{ constructor /** * HTML_Table constructor. * * @param $attributes array of attributes used to create the table * @access public */ function HTML_Table($attributes = "") { $this->rows = $attributes["rows"]; $this->cols = $attributes["cols"]; $this->border = $attributes["border"]; $this->bgcolor = $attributes["bgcolor"]; $this->background = $attributes["background"]; $this->width = $attributes["width"]; $this->cellspacing = $attributes["cellspacing"]; $this->cellpadding = $attributes["cellpadding"]; } // }}} // {{{ makeHTML_Table() function makeHTML_Table () { $html = ""; if ($this->rows == 0 || $this->cols == 0) { return 0; } $html .= "<TABLE"; $html .= ($this->border ? " BORDER='".$this->border."'" : ""); $html .= ($this->bgcolor ? " BGCOLOR='".$this->bgcolor."'" : ""); $html .= ($this->background ? " BACKGROUND='".$this->background."'" : ""); $html .= ($this->width ? " WIDTH='".$this->width."'" : ""); $html .= ($this->cellspacing ? " CELLSPACING='".$this->cellspacing."'" : ""); $html .= ($this->cellpadding ? " CELLPADDING='".$this->cellpadding."'" : ""); $html .= ">\n"; if ($this->caption) { $html .= "<CAPTION"; $html .= ($this->caption_align ? " ALIGN='".$this->caption_align."'" : ""); $html .= ">".$this->caption."</CAPTION>\n"; } for ($i = 1 ; $i <= $this->rows ; $i++) { $html .= "<TR>"; for ($j = 1 ; $j <= $this->cols ; $j++) { if ($this->cellspan["$i:$j"] && $this->cellspan["$i:$j"] == "SPANNED") { $html .= "<!-- span -->"; continue; } if ($this->cellhead["$i:$j"]) { $html .= "<TH"; } else { $html .= "<TD"; } if ($this->cellalign["$i:$j"]) $html .= " ALIGN='".$this->cellalign["$i:$j"]."'"; if ($this->cellvalign["$i:$j"]) $html .=" VALIGN='".$this->cellvalign["$i:$j"]."'"; if ($this->cellwidth["$i:$j"]) $html .= " WIDTH='".$this->cellwidth["$i:$j"]."'"; if ($this->cellheight["$i:$j"]) $html .= " HEIGHT='".$this->cellheight["$i:$j"]."'"; if ($this->cellbgcolor["$i:$j"]) $html .= " BGCOLOR='".$this->cellbgcolor["$i:$j"]."'"; if ($this->cellnowrap["$i:$j"]) $html .= " NOWRAP='".$this->cellnowrap["$i:$j"]."'"; if ($this->cellcolspan["$i:$j"]) $html .= " COLSPAN='".$this->cellcolspan["$i:$j"]."'"; if ($this->cellrowspan["$i:$j"]) $html .= " ROWSPAN='".$this->cellrowspan["$i:$j"]."'"; $html .= ">"; $html .= $this->cellstartstring["$i:$j"]; if ($this->cellcontent["$i:$j"]) { $html .= $this->cellcontent["$i:$j"]; } else { $html .= $this->autofill; } $html .= $this->cellendstring["$i:$j"]; if ($this->cellhead["$i:$j"]) { $html .= "</TH>"; } else { $html .= "</TD>"; } } $html .= "</TR>\n"; } $html .= "</TABLE>\n"; return $html; } // }}} // {{{ showHTML_Table() function showHTML_Table () { print $this->makeHTML_Table(); } // }}} // {{{ autoGrow() function autoGrow ($autogrow = 1) { $this->autogrow = $autogrow; } // }}} // {{{ autoFill() function autoFill ($autofill = " ") { $this->autofill = $autofill; } // }}} // {{{ overWrite() function overWrite ($overwrite = 0) { $this->overwrite = $overwrite; } // }}} // {{{ setTableAttributes() function setTableAttributes ($attributes) { if ($this->overwrite) { $this->bgcolor = $attributes["bgcolor"]; $this->border = $attributes["border"]; $this->width = $attributes["width"]; $this->cellspacing = $attributes["cellspacing"]; $this->cellpadding = $attributes["cellpadding"]; $this->background = $attributes["background"]; } else { if ($attributes["bgcolor"]) $this->bgcolor = $attributes["bgcolor"]; if ($attributes["border"]) $this->border = $attributes["border"]; if ($attributes["width"]) $this->width = $attributes["width"]; if ($attributes["cellspacing"]) $this->cellspacing = $attributes["cellspacing"]; if ($attributes["cellpadding"]) $this->cellpadding = $attributes["cellpadding"]; if ($attributes["background"]) $this->background = $attributes["background"]; } } // }}} // {{{ setCaption() function setCaption ($caption, $caption_align="") { $this->caption = $caption; if ($caption_align == "TOP" || $caption_align == "BOTTOM") { $this->caption_align = $caption_align; } else { $error[] = "setCaption : Invalid Alignment\n"; return false; } } // }}} // {{{ setCellContent() function setCellContent ($row, $col, $content="") { if ($row > $this->row) { if ($this->autogrow) { $this->row = $row; } else { $error[] = "setCellContent : Invalid table row reference"; } } if ($col > $this->col) { if ($this->autogrow) { $this->col = $col; } else { $error[] = "setCellContent : Invalid table column reference"; } } $this->cellcontent["$row:$col"] = $content; } // }}} // {{{ getCell() function getCell ($row, $col) { return $this->cellcontent["$row:$col"]; } // }}} // {{{ setCellAttributes() function setCellAttributes ($row, $col, $attributes) { if ($this->overwrite) { $this->cellalign["$row:$col"] = $attributes["align"]; $this->cellvalign["$row:$col"] = $attributes["valign"]; $this->cellhead["$row:$col"] = $attributes["head"]; $this->cellnowrap["$row:$col"] = $attributes["nowrap"]; $this->cellwidth["$row:$col"] = $attributes["width"]; $this->cellheight["$row:$col"] = $attributes["height"]; $this->cellbgcolor["$row:$col"] = $attributes["bgcolor"]; $this->cellstartstring["$row:$col"] = $attributes["startstring"]; $this->cellendstring["$row:$col"] = $attributes["endstring"]; } else { if ($attributes["align"]) $this->cellalign["$row:$col"] = $attributes["align"]; if ($attributes["valign"]) $this->cellvalign["$row:$col"] = $attributes["valign"]; if ($attributes["head"]) $this->cellhead["$row:$col"] = $attributes["head"]; if ($attributes["nowrap"]) $this->cellnowrap["$row:$col"] = $attributes["nowrap"]; if ($attributes["width"]) $this->cellwidth["$row:$col"] = $attributes["width"]; if ($attributes["height"]) $this->cellheight["$row:$col"] = $attributes["height"]; if ($attributes["bgcolor"]) $this->cellbgcolor["$row:$col"] = $attributes["bgcolor"]; if ($attributes["startstring"]) $this->cellstartstring["$row:$col"] = $attributes["startstring"]; if ($attributes["endstring"]) $this->cellendstring["$row:$col"] = $attributes["endstring"]; } if ($attributes["rowspan"]) { $this->cellrowspan["$row:$col"] = $attributes["rowspan"]; $this->updateSpanGrid ($row, $col); } if ($attributes["colspan"]) { $this->cellcolspan["$row:$col"] = $attributes["colspan"]; $this->updateSpanGrid ($row, $col); } } // }}} // {{{ addRow() function addRow ($content) { end ($content); $col = key ($content); if ($col > $this->cols) { $this->cols = $col; } $this->rows++; for ($i=0;$i <= $col;$i++) { $this->cellcontent["$this->rows:$i"] = $content[$i]; } } // }}} // {{{ setRowAttributes() function setRowAttributes ($row, $attributes) { for ($i = 1; $i <= $this->cols; $i++) { $this->setCellAttributes($row,$i,$attributes); } } // }}} // {{{ addCol() function addCol ($content) { end ($content); $row = key ($content); if ($row > $this->rows) { $this->rows = $row; } $this->cols++; for ($i=0;$i <= $row;$i++) { $this->cellcontent["$i:$this->cols"] = $content[$i]; } } // }}} // {{{ setColAlign() function setColAttributes ($col, $attributes) { for ($i = 1; $i <= $this->rows; $i++) { $this->setCellAttributes($i,$col,$attributes); } } // }}} // {{{ updateSpanGrid() function updateSpanGrid ($row, $col) { $colspan = $this->cellcolspan["$row:$col"]; $rowspan = $this->cellrowspan["$row:$col"]; if ($colspan) { for ($j = $col+1; (($j <= $this->cols) && ($j <= ($col + $colspan - 1))); $j++) { $this->cellspan["$row:$j"] = "SPANNED"; } } if ($rowspan) { for ($i = $row+1; (($i <= $this->rows) && ($i <= ($row + $rowspan - 1))); $i++) { $this->cellspan["$i:$col"] = "SPANNED"; } } if ($colspan && $rowspan) { for ($i = $row+1; (($i <= $this->rows) && ($i <= ($row + $rowspan - 1))); $i++) { for ($j = $col+1; (($j <= $this->cols) && ($j <= ($col + $colspan - 1))); $j++) { $this->cellspan["$i:$j"] = "SPANNED"; } } } } } ?>