php寫的 html table generator. 可以自動生成列表.很好用的

class html_table{

      var $bgcolor = "";            // Table's bgcolor
      var $width = "100%";            // Table's width
      var $cellspacing = 1;            // Table's cellspacing
      var $cellpadding = 4;         // Table's cellpadding
      var $border = 0.1;               // Table's Border
      var $row_color="#E9E9E9";     //

      var $set_row_event = FALSE;    // Enable onmouseover event to display selected table's row in a different color
      var $set_row_event_color="#9999FF"; // set the color

      var $set_alter_colors = FALSE;  // Display Table rows in a different color except header if it is enabled
      var $first_alter_color="#FFCC99"; // first row color
      var $second_alter_color="#FFFFFF"; // second row color

      var $display_header_row = false;   // Table Has a header Row
      var $set_bold_labels=TRUE;      // Fonts in header are bold
      var $set_header_font_color="#000000"; // header font color
      var $set_header_font_face="Tahoma";   // header font face
      var $set_header_font_size=2;          // header font face
      var $set_header_bgcolor ="#FFCC33";   // header background color

      var $display_fonts = FALSE;     // Output Font 's Tags in table's cells
      var $set_font_color="#000000";  // Tag font color
      var $set_font_face="Tahoma";    // Tag font face
      var $set_font_size=2;           // Tag font size

      var $current_color;            // don't change it.
      var $count;                     // count rows
      /**
       * html_table::opentable()
       *
       * @return
       **/
      function opentable(){
               $html = "/n/n<!--// Start Table //-->/n/n/n<table width='".$this->width."'border='".$this->border."' cellspacing='". $this->cellspacing."' cellpadding='". $this->cellpadding."' bgcolor='". $this->bgcolor."'>/n";
               return $html;
      }


      /**
       * html_table::build_row_event()
       *
       * @return
       **/
      function build_row_event(){
               $event = " onmouseover=/"this.style.background='$this->set_row_event_color';this.style.cursor='hand'/"
                                        onmouseout=/"this.style.background='$this->current_color'/"";
               return $event;
      }


      /**
       * html_table::row()
       *
       * @param $isheader
       * @return
       **/
      function row( $isheader ){

               if ( $this->display_header_row ){
                   if ( $isheader ){
                       $this->current_color = $this->set_header_bgcolor;
                   }else{
                       if ( $this->set_alter_colors ){
                         $this->count%2!=0 ? $this->current_color = $this->first_alter_color : $this->current_color = $this->second_alter_color;
                       }else{
                         $this->current_color = $this->row_color;
                       }
                   }
               }else{
                    if ( $this->set_alter_colors ){
                         if ( $this->set_alter_colors ){
                              $this->count%2!=0 ? $this->current_color = $this->first_alter_color : $this->current_color = $this->second_alter_color;
                         }
                    }else{
                         $this->current_color = $this->row_color;
                    }
               }

               if ( $this->set_row_event && $isheader==0) {
                       $event = $this->build_row_event();
               } else {
                       $event = "" ;
               }
               $html = "<tr bgcolor='".$this->current_color."' $event>/n";
               return $html;
      }

      /**
       * html_table::row_close()
       *
       * @return
       **/
      function row_close(){
               $html = "</tr>/n";
               return $html;
      }

      /**
       * html_table::build_html_table()
       *
       * @param $contents
       * @return
       **/
      function build_html_table( $contents ){
               $TABLE = $this->opentable();
               $TABLE .= $this->build_columns( $contents );
               $TABLE .= $this->closetable();
               return $TABLE;
      }

      /**
       * html_table::build_fonts()
       *
       * @param $txt
       * @param integer $isheader
       * @return
       **/
      function build_fonts($txt,$isheader=0){

               if ($isheader && $this->display_header_row){
                   if ($this->set_bold_labels){
                       $txt = "<b>$txt</b>";
                   }
                   $html = "<font face='$this->set_header_font_face' size='$this->set_header_font_size' color='$this->set_header_font_color'>$txt</font>";
               }else{
                   if ($this->display_fonts){
                       $html = "<font face='$this->set_font_face' size='$this->set_font_size' color='$this->set_font_color'>$txt</font>";
                   }else{
                           $html = $txt;
                   }
               }
               return $html;
      }

      /**
       * html_table::build_columns()
       *
       * @param $contents
       * @return
       **/
      function build_columns( $contents ){

               $html = "";
               reset($contents);
               while (list($key, $dis) = each($contents)){
                      !$this->count ? $header=1 : $header=0;
                      $this->count++;
                      $html .= $this->row( $header );
                      $cell="";
                      while ( list($id,$values) = each($dis)){
                              $cell .= "<td ";
                              if ( IsSet($values["align"]) ) $cell .= "align='".$values["align"]."' ";
                              if ( IsSet($values["width"]) ) $cell .= "width='".$values["width"]."' ";
                              if ( IsSet($values["bgcolor"]) ) $cell .= "bgcolor='".$values["bgcolor"]."' ";
                              if ( IsSet($values["colspan"]) ) $cell .= "colspan='".$values["colspan"]."' ";
                              if ( IsSet($values["valign"]) ) $cell .= "align='".$values["valign"]."' ";
                              if ( !IsSet($values["text"]) ) $values["text"]="&nbsp;";
                              $cell .= ">";
                              $cell .= $this->build_fonts($values["text"],$header);
                              $cell .= "</td>/n";
                      }
                      $html .= $cell;
                      $html .= $this->row_close();
               }
               return $html;
      }

      /**
       * html_table::closetable()
       *
       * @return
       **/
      function closetable(){
               $html = "</table>/n/n/n <!--// Close Table //-->/n/n/n";
               return $html;
      }

}
把上面的這段copy & paste 下來, save as html_table.class.php
###############################################################################
應用在這裏: copy paste 下來 , 試試
<?php
include ("html_table.class.php");

// create object
$mytbl = new html_table();

// General Table properties
$mytbl->width = "450";             // set table width;
$mytbl->cellspacing = 1;         // 1 is class's default value
$mytbl->cellpadding = 4;        // 4 is class's default value
$mytbl->border = 0;             // 0 is class's default value
$mytbl->rowcolor = "#E9E9E9";     // table's rows colors...default is #FFCC99

// Set table's header row
$mytbl->display_header_row = TRUE;       // enable the option. Default is FALSE
$mytbl->set_bold_labels = TRUE;             // Default is TRUE
$mytbl->set_header_font_color="#000000"; // Default is #000000
$mytbl->set_header_font_face="Tahoma";   // default is Tahoma
$mytbl->set_header_bgcolor ="#FF9933";   // Default if $FFCC33

//Set row event
$mytbl->set_row_event = TRUE; // Default is FALSE
$mytbl->set_row_event_color = "#FF9900"; //Default is #9999FF

// Set table's rows alter colors
$mytbl->set_alter_colors = TRUE;        // Default is False
$mytbl->first_alter_color = "#CCCCCC";     // Default is #FFCC99
$mytbl->second_alter_color = "#E9E9E9"; // Default is #FFFFFF

// Add Font Tags in each cell
$mytbl->display_fonts = TRUE; // Default Is FALSE


// Builbing A Table - 3 colums, 5 rows

// 1st row Colspan 3
$myarr[0][0]["colspan"]= 3;
$myarr[0][0]["align"]  = "center";
$myarr[0][0]["text"]   = "ICL";

$myarr[1][0]["text"]= "&nbsp";
$myarr[1][1]["text"]="Qtyout";
$myarr[1][2]["text"]="Yield";
##########embeded table##########################

$inner=new html_table();
$innerarr[0][0]["colspan"]=2;
$innerarr[0][0]["align"]="center";
$innerarr[0][0]["text"]="ICL";
#$innerarr[1][0]["text"]="Qtyin";
#$innerarr[1][1]["text"]="Yield";
$innerhtml=$inner->build_html_table($innerarr);

####################################
// adding rows
for ( $i=2; $i<=25; $i++){
    $myarr[$i][0]["width"] = 50;
    $myarr[$i][0]["align"] = "right";
    $myarr[$i][0]["bgcolor"] = "red";
    $myarr[$i][0]["text"]  = $i;
    $myarr[$i][1]["width"] = 300;
    $myarr[$i][1]["align"] = "center";
    $myarr[$i][1]["text"]  = "<a href=/"#/">Link to Page $i</a>";    
    $myarr[$i][2]["width"] = 100;
    $myarr[$i][2]["align"] = "left";
    $myarr[$i][2]["text"]  = "$innerhtml";        
}

// Building Html from array
$html = $mytbl->build_html_table( $myarr );
?>
<html>
<head><title>HTML TABLE CLASS ::: Example</title></head>
<body>
<center>
<?php
    // Echo the table.
    echo $html;
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章