Zend - The PHP Company




Tables

Add Code


HTML_table  

Type: class
Added by: imperator_mundi
Entered: 01/10/2001
Last modified: 31/10/2000
Rating: **** (4 votes)
Views: 9715
a PHP class that allow to generate table with e.g alternate row and columns colors, various cells dimensions, alignement, set header properties, ecc.


<?
class HTML_table {

// number of the table columns
var $col_number 1;
// number of the table rows
var $row_number 1;
// style of the table border
var $border_style "";
// table width in absolute or percent
var $table_width "";
// columns widths array
var $col_width = array();
// columns names array
var $col_name = array();
// alignement of the cells containing column names
var $col_name_align = array();
// border style of the cells containing column names
var $col_name_border = array();
// style of the TH cells
var $col_name_style = array();

// cells contents
var $data = array();
// cells background colors
var $cell_color = array();
// cells horizontal alignement
var $cell_align = array();
// cells vertical alignement
var $cell_valign = array();
// cells height
var $cell_height = array();
// cell style
var $cell_style = array();

/* html code that will be written between <TABLE> und the first tag
   of the table, e.g. for form */
var $hidden_header "";
/* html code that will be written between the last first tag
   of the table and the </TABLE>, e.g. for form */
var $hidden_footer "";

      function 
HTML_table() {
               
$this->column_width[0] = "";
               
$this->column_name[0] = "";
      }

      function 
setTableWidth($width) {
               
$this->table_width $width;
      }

      function 
setColsNumber($num) {
               
$this->col_number $num;
      }

      function 
setColWidth($col$width) {
               
$this->col_width[$col] = $width;
               if((
$col <> 0)&&($this->col_width[0] ==""))
               {
$this->col_width[0] = 0;}
      }

      function 
setColumnsWidth($array) {
               
$col 0;
               foreach(
$array as $k) {
                   
$this->setColWidth($col$k);
                   
$col++;
               }
      }

      function 
setColName($col$name) {
               
$this->col_name[$col] = $name;
               if((
$col <> 0)&&($this->col_name[0]==""))
               {
$this->col_name[0] = "&nbsp";}
      }

      function 
setColumnsName($array) {
               
$col 0;
               foreach(
$array as $k) {
                   
$this->setColName($col$k);
                   
$col++;
               }
      }

      function 
setColNameAlign($col$align) {
               
$this->col_name_align[$col] = $align;
               if((
$col <> 0)&&($this->col_name_align[0]==""))
               {
$this->col_name_align[0] = "&nbsp";}
      }

      function 
setColumnsNameAlign($array) {
               
$col 0;
               foreach(
$array as $k) {
                   
$this->setColNameAlign($col$k);
                   
$col++;
               }
      }

      function 
setColNameBorder($col$border) {
               
$this->col_name_border[$col] = $border;
               if((
$col <> 0)&&($this->col_name_border[0]==""))
               {
$this->col_name_border[0] = "&nbsp";}
      }


      function 
setColNameStyle($col$style) {
               
$this->col_name_style[$col] = $style;
               if((
$col <> 0)&&($this->col_name_style[0]==""))
               {
$this->col_name_style[0] = "&nbsp";}
      }

      function 
setColumnsNameStyle($array) {
               
$col 0;
               foreach(
$array as $k) {
                   
$this->setColNameStyle($col$k);
                   
$col++;
               }
      }

      function 
setRowsNumber($num) {
               
$this->row_number $num;
      }

      function 
setBorderStyle($border) {
               
$this->border_style $border;
      }

      function 
setCellColor($row$col$color) {
               
$this->cell_color[$row][$col] = $color;
      }

      function 
setRowColor($row$color) {
               for(
$col 0$col $this->col_number$col++) {
                     
$this->cell_color[$row][$col] = $color;
               }
      }

      function 
setColColor($col$color) {
               for(
$row 0$row $this->row_number$row++) {
                     
$this->cell_color[$row][$col] = $color;
               }
      }

      function 
setCellAlign($row$col$align) {
               
$this->cell_align[$row][$col] = $align;
      }

      function 
setCellValign($row$col$valign) {
               
$this->cell_valign[$row][$col] = $valign;
      }

      function 
setCellHeight($row$col$height) {
               
$this->cell_height[$row][$col] = $height;
      }

      function 
setRowHeight($row$height) {
               for(
$col 0$col $this->col_number$col++) {
                     
$this->setCellHeight($row$col$height);
               }
      }

      function 
insertCell($row$col$content) {
               
$this->data[$row][$col] = $content;
      }

      function 
insertRow($row$content) {
               
$col 0;
               foreach(
$content as $k) {
                   
$this->insertCell($row$col$k);
                   
$col++;
               }
      }

      function 
insertHiddenHeader($string) {
               
$this->hidden_header .= $string;
      }

      function 
insertHiddenFooter($string) {
               
$this->hidden_footer .= $string;
      }

      function 
returnTableOpenTag() {
               
$output "";
               
$output .= "n".'<TABLE CELLSPACING="1" CELLPADDING="1"';
               if(
$this->border_style <> "") {
                    
$output .=" border="".$this->border_style."" ";
               }
               if(
$this->table_width <> "") {
                    
$output .="width="".$this->table_width."" ";
               }
               
$output .= ">n";
               return 
$output;
      }

      function 
returnTableHeader() {
               
$output "";
               
$output .= "<TR align = "center">n";
               if((
$this->col_width[0] <> "")||($this->col_name[0] <> "")) {
                   for(
$col 0$col $this->col_number$col++) {
                      
$output .= '<TH';
                      if(
$this->col_width[$col] <> "") {
                           
$output .= " width="".$this->col_width[$col].""";
                      }
                      if(
$this->col_name_style[$col] <> "") {
                           
$output .= " style="".$this->col_name_style[$col].""";
                      }
                      
$output .= ">";
                      if(
$this->col_name[$col] <> "") {
                           
$output .= " ".$this->col_name[$col];
                      }
                      else {
                           
$output .= "&nbsp";
                      }
                      
$output .= "</TH>n";
                   }
               }
               
$output .= "</TR>";
               
$output .= "n";
               return 
$output;
      }
      function 
returnRow($row) {
               
$output "";
               for(
$col 0$col $this->col_number$col++) {
                          
$output .= "<TD";
                          if(
$this->cell_color[$row][$col] <> "") {
                                
$output .= " bgcolor="".$this->cell_color[$row][$col].""";
                          }
                          if(
$this->cell_align[$row][$col] <> "") {
                                
$output .= " align="".$this->cell_align[$row][$col].""";
                          }
                          if(
$this->cell_valign[$row][$col] <> "") {
                                
$output .= " valign="".$this->cell_valign[$row][$col].""";
                          }
                          if(
$this->cell_height[$row][$col] <> "") {
                                
$output .= " height="".$this->cell_height[$row][$col].""";
                          }
                          
$output .= ">";
                          
$output .= $this->data[$row][$col];
                          
$output .= "&nbsp"."</TD>n";
               }
               return 
$output;
      }

      function 
returnHTML() {
            
$output "";
            
$output .= $this->returnTableOpenTag();
            
$output .= $this->hidden_header."n";
            
$output .= $this->returnTableHeader();
            for(
$k 0$k $this->row_number$k++) {
                   
$output .= "<TR valign="middle">n";
                   
$output .= $this->returnRow($k);
                   
$output .= "</TR>n";
            }
            
$output .= $this->hidden_footer."n";
            
$output .= "</TABLE>n";
            return 
$output;
      }
}
?>


Usage Example


<TABLE CELLSPACING="1" CELLPADDING="1"width="100%" >
<TR align = "center">
<TH width="13%" style="border-style: solid; border-color: #88BBFF; border-width: 2px"> <a href="?namesort=descending&inputname=">Name</a></TH>
<TH width="15%" style="border-style: solid; border-color: #88BBFF; border-width: 2px"> <a href="?vornamesort=ascending&inputname=">First Name</a></TH>
<TH width="18%" style="border-style: solid; border-color: #88BBFF; border-width: 2px"> <a href="?emailsort=ascending&inputname=">Email</a></TH>
<TH width="8%" style="border-style: solid; border-color: #88BBFF; border-width: 2px"> <a href="?internsort=ascending&inputname=">From</a></TH>
<TH width="8%" style="border-style: solid; border-color: #88BBFF; border-width: 2px"> <a href="?mobilsort=ascending&inputname=">To</a></TH>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Bush&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">George&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1988&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1992&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Carter&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">Jimmy&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1976&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1980&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Clinton&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">Billy&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1992&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">2000&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Eisenhower&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">Dwight David&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1952&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1960&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Jefferson&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">Tommy&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1796&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1808&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Kennedy&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">John Fitzgerarld&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1960&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1963&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Lincoln&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">Abe&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1860&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1872&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Nixon&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">Richie&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1968&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1974&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Reagan&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">Ronnie&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1980&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1988&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Roosvelt&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">Teddy&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1900&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1908&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Roosvelt&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">Frankie Delano&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1932&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1945&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DB5" height="30">Truman&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">Harry&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1945&nbsp&nbsp</TD>
<TD bgcolor="#396DB5" height="30">1952&nbsp&nbsp</TD>
</TR>
<TR valign="middle">
<TD bgcolor="#396DA5" height="30">Washington&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">George&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30"><a href="mailto:"></a>&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1789&nbsp&nbsp</TD>
<TD bgcolor="#396DA5" height="30">1796&nbsp&nbsp</TD>
</TR>

</TABLE>


Rate This Script





Search



This Category All Categories