Version: 1.0

Type: Class

Category: HTML

License: GNU General Public License

Description: This class will create the normal html table and also the table content. It also can create nested HTML table with some creativity :-) .



<?php
/**@Class Name : table
** @Author : kamal <k4ml@linuxmail.org>
** @Licence : GPL
** @Function : This class would be able to create the simple html table and also the content.
                      It also can handle nested html table with some creativity :-)
** @Notes : Feel free to use and modified this code. If you modified this code, please let me know
                 so I could update my code.
                 See below for the sample usage of this class.
**/

class table {
   //private
   var $table ;
   
   /**Constructor - Create new html table
   ** @param - Set <table> : border, cellpadding, cellspacing, width, height, align, valign,
   **                   bgcolor, class
   **
   **/ 
   function table($a = array()) {
      $this->table = "<table" ;
      if ($a['border']) { $this->table .= " border=\"".$a['border']."\"" ; }
      if ($a['cellpadding']) { $this->table .= " cellpadding=\"".$a['cellpadding']."\"" ; }
      if ($a['cellspacing']) { $this->table .= " cellspacing=\"".$a['cellspacing']."\"" ; }
      if ($a['width']) { $this->table .= " width=\"".$a['width']."\"" ; }
      if ($a['height']) { $this->table .= " height=\"".$a['height']."\"" ; }
      if ($a['align']) { $this->table .= " align=\"".$a['align']."\"" ; }
      if ($a['valign']) { $this->table .= " valign=\"".$a['valign']."\"" ; }
      if ($a['bgcolor']) { $this->table .= " bgcolor=\"".$a['bgcolor']."\"" ; }
      if ($a['class']) { $this->table .= " class=\"".$a['class']."\"" ; } 
      $this->table .= ">\n" ;
   }
   
   /**Create new row
   ** @param - Set <tr> : bgcolor, class
   **/
   function new_row($a = array()) {
      $this->table .= "<tr" ;
      if ($a['bgcolor']) { $this->table .= " bgcolor=\"".$a['bgcolor']."\"" ; }
      if ($a['class']) { $this->table .= " class=\"".$a['class']."\"" ; }
      $this->table .= ">\n" ; 
   }
   
   /**Create new cell
   ** @param - Set <td> : bgcolor, rowspan, colspan, width, height, align, valign
   **                                 background, class
   ** @Check for last_column. If not empty (true) will start the column on new row
   **/
   function new_cell($a = array()) {
      $this->table .= "<td" ;
      if ($a['bgcolor']) { $this->table .= " bgcolor=\"".$a['bgcolor']."\"" ; }
      if ($a['rowspan']) { $this->table .= " rowspan=\"".$a['rowspan']."\"" ; }
      if ($a['colspan']) { $this->table .= " colspan=\"".$a['colspan']."\"" ; }
      if ($a['width']) { $this->table .= " width=\"".$a['width']."\"" ; }
      if ($a['height']) { $this->table .= " height=\"".$a['height']."\"" ; }
      if ($a['align']) { $this->table .= " align=\"".$a['align']."\"" ; }
      if ($a['valign']) { $this->table .= " valign=\"".$a['valign']."\"" ; }
      if ($a['background']) { $this->table .= " background=\"".$a['background']."\"" ; }
      if ($a['class']) { $this->table .= " class=\"".$a['class']."\"" ; } 
      $this->table .= ">\n" ;
      $this->table .= $a['content'] ;
      $this->table .= "</td>\n" ;
      if ($a['last_column']) {$this->table .="</tr>\n" ; }
   }
   
   /**Return the string containing the html table code
   **
   **/
   function get_table() {
      $this->table .= "</table>\n" ;
      return $this->table ;
   }
}
//sample usage
$table = new table ( array('border'=>'0', 'width'=>'100%', 'bgcolor'=>'magenta', 'class'=>'try', 'cellpadding'=>'3',
                             'cellspacing'=>'0')) ;


$x = 1 ; //Number
//States in Malaysia
$states = array('Kelantan', 'Terengganu', 'Kedah', 'Perlis', 'Perak', 'Pulau Pinang', 'Pahang', 'Negeri Sembilan',
                      'Selangor', 'Melaka', 'Johor', 'Wilayah Persekutuan Kuala Lumpur', 'Sabah', 'Serawak') ;
foreach ($states as $state) {
   $table->new_row(array('bgcolor'=>'yellow')) ;
      $table->new_cell(array('content'=>$x, 'bgcolor'=>'red', 'width'=>'20%')) ;
      //last_column = true to make a new row
      $table->new_cell(array('last_column'=>'true', 'content'=>$state, 'bgcolor'=>'orange')) ;
   $x++ ;
}

echo $table->get_table() ;
echo "<br>\n" ;

$table2 = new table(array('border'=>'1', 'width'=>'100%')) ;
$table2->new_row() ;
$table2->new_cell(array('last_column'=>'true', 'content'=>'header', 'colspan'=>'2')) ;

$table2->new_row() ;
$table2->new_cell(array('content'=>'first left', 'rowspan'=>'2')) ;
$table2->new_cell(array('last_column'=>'true', 'content'=>'first right')) ;

$table2->new_row() ;
//$table2->new_cell(array('content'=>'second left')) ;
$table2->new_cell(array('last_column'=>'true', 'content'=>'second right')) ;

echo $table2->get_table() ;


?>