Login   Register  
PHP Classes
elePHPant
Icontem

File: table.php3

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Herman Veluwenkamp  >  Table - OO Interface  >  table.php3  >  Download  
File: table.php3
Role: ???
Content type: text/plain
Description: Contains table class file plus demo script in one file.
Class: Table - OO Interface
Automatic generation of HTML for tables
Author: By
Last change:
Date: 14 years ago
Size: 7,162 bytes
 

Contents

Class file image Download
<html>
<head>
<title>Table</title>
	
<style type="text/css">
	.header { background: yellow; text-align: center; }
</style>
	
</head>
<body>


<?
class html_table {
	var $cols = 0;
	var $rows = 0;
	var $cell = array();
	var $html = '';
	var $cell_content = '&nbsp;'; //default cell content.
	var $content_style = ''; //default content style. content is surrounded by <SPAN> tag.
  
	var $col_style = array(); // default style for table column. enclosed in <TD> tags.
  var $col_cell_style = array(); // default style for column cells. enclosed in <SPAN> tags.
  
	var $cell_style = ''; //  default cell style. enclosed in <TD> tags.
  var $table_style = ''; // default table style. enclosed in <TABLE> tags.
  
function cell_init() {
	return array(
		'content'   => $this->cell_content,	
		'style'     => '',	
		'cell_style'=> '');
}
	
function init($parameters) {
	if (isset($parameters['cols'])) $this->cols = $parameters['cols'];
	if (isset($parameters['rows']))$this->rows = $parameters['rows'];
	for ($row = 1; $row <= $this->rows; $row++) {
		for ($col = 1; $col <= $this->cols; $col++) {
			$this->cell[$row][$col] = $this->cell_init();
		}
	}
}

function add_rows($num_rows) {
	for ($row = 1; $row <= $num_rows; $row++) {
		for ($col = 1; $col <= $this->cols; $col++) {
			$this->cell[$row + $this->rows][$col] = $this->cell_init();
		}
	}
	$this->rows += $num_rows;
}

function add_cols($num_cols) {
	for ($row = 1; $row <= $this->rows; $row++) {
		for ($col = 1; $col <= $num_cols; $col++) {
			$this->cell[$row][$col+$this->cols] = $this->cell_init();
		}
	}
	$this->cols += $num_cols;
}

function code() {
	if (!empty($this->html)) return 1;
	$this->html = '<TABLE '.$this->table_style.'>'."\n";
	for ($row = 1; $row <= $this->rows; $row++) {
		$this->html .= '  <TR>'."\n";
		for ($col = 1; $col <= $this->cols; $col++) {
			$extra = '';
			//check if "colspan" defined. if so then hide cells that get merged.
			if (isset($this->cell[$row][$col]['colspan'])) {
				$extra .= 'COLSPAN="'.$this->cell[$row][$col]['colspan'].'"';
				for ($hidden_col = 1; $hidden_col < $this->cell[$row][$col]['colspan']; $hidden_col++) {
					$this->cell[$row][$col+$hidden_col]["hide"] = true;
					//check if "rowspan" defined. if so then propogate "colspan" into merged rows.
					if (isset($this->cell[$row][$col]["rowspan"])) {
						for ($hidden_row = 1; $hidden_row < $this->cell[$row][$col]['rowspan']; $hidden_row++) {
							$this->cell[$row+$hidden_row][$col]["colspan"] = $this->cell[$row][$col]['colspan'];
						}
					}					
				}
			}
			//check if "rowspan" defined. if so then hide cells that get merged.
			if (isset($this->cell[$row][$col]["rowspan"])) {
				$extra .= 'ROWSPAN="'.$this->cell[$row][$col]['rowspan'].'"';
				for ($hidden_row = 1; $hidden_row < $this->cell[$row][$col]['rowspan']; $hidden_row++) {
					$this->cell[$row+$hidden_row][$col]["hide"] = true;
				}
			}
			// code to draw cell html...
			if (isset($this->cell[$row][$col]['hide'])) continue; // if hide then skip this cell.
			
			// otherwise draw cell with style...
			if (!empty($this->cell[$row][$col]['cell_style'])) 
				$this->html .= '    <TD '.$this->cell[$row][$col]['cell_style'].' '.$extra.'>';
      else if (!empty($this->col_style[$col]))  
        $this->html .= '    <TD '.$this->col_style[$col].' '.$extra.'>';
			else 
				$this->html .= '    <TD '.$this->cell_style.' '.$extra.'>';
				
			// draw content of cell with style...
			if (!empty($this->cell[$row][$col]['style'])) $this->html .= '<SPAN '.$this->cell[$row][$col]['style'].'>';
      else if (!empty($this->col_cell_style[$col])) $this->html .= '<SPAN '.$this->col_cell_style[$col].'>';
			else if (!empty($this->content_style)) $this->html .= '<SPAN '.$this->content_style.'>';
			
			$this->html .= $this->cell[$row][$col]['content'];
			
			if (!empty($this->cell[$row][$col]['style']) or !empty($this->col_cell_style[$col]) or !empty($this->content_style))
        $this->html .= '</SPAN>';
			
			$this->html .= '</TD>'."\n";
			
		}
		$this->html .= '  </TR>'."\n";
	}
	$this->html .= '</TABLE>'."\n";
}

function display() {
  if (empty($this->html)) $this->code();
	print $this->html;
}

} // end of class html_table.


class html_table_pretty extends html_table {
	var $background = '';
	var $border_width = '';
	var $border_color = '';
  var $html_pretty = '';

function code_pretty() {
  $this->code();
	$border_style = '';
  
  if (!empty($this->border_width)) $border_style .= ' CELLSPACING="0" CELLPADDING="0" BORDER="'.$this->border_width.'"';
  if (!empty($this->border_color)) $border_style .= ' BORDERCOLOR="'.$this->border_color.'"';


	$this->html_pretty  = '<TABLE'.$border_style.'><TR><TD '.$this->background.'>'."\n";
	$this->html_pretty .= $this->html;
	$this->html_pretty .='</TD></TR></TABLE>'."\n";
}

function display_pretty() {
  if (empty($this->html_pretty)) $this->code_pretty();
	print $this->html_pretty;
}


} // end of class html_table_pretty
?>

<?
/*
  examples below...
*/

$data = new html_table;

// define table size.
$data->init(array("cols"=>3, "rows"=>"3"));

// default styles.
$data->table_style = 'BORDER="1" CELLPADDING="2" CELLSPACING="2"';
$data->col_style[1] = 'BGCOLOR="red"';
$data->col_style[2] = 'BGCOLOR="green"';
$data->col_style[3] = 'BGCOLOR="blue"';

$data->col_cell_style[1] = 'STYLE="font: bold 12pt monospace;"';

// define table content.
$data->cell[1][1]["content"]="apple";
$data->cell[2][2]["content"]="orange";
$data->cell[2][3]["content"]="pear";
$data->cell[3][1]["content"]="plum";
$data->cell[2][1]["content"]="grape";

// code and display.
$data->code();
$data->display();
?>
<BR><BR>
<?
$data = new html_table_pretty;

// define background properties.
$data->background = 'BGCOLOR="black"';
$data->border_color = 'red';
$data->border_width = '8';

// define table size.
$data->init(array("cols"=>3));
$data->add_rows("3");

// default styles.
$data->table_style = 'BORDER="0" CELLPADDING="2" CELLSPACING="1"';
$data->cell_style = 'WIDTH="100" BGCOLOR="#cccccc"';
$data->content_style = 'STYLE="font: bold 9pt monospace;"';

// define table content.
$data->cell[1][1]["content"]="apple";
$data->cell[2][2]["content"]="orange";
$data->cell[2][3]["content"]="pear";
$data->cell[3][1]["content"]="plum";
$data->cell[2][1]["content"]="grape";
$data->cell[1][1]["colspan"]=3;
$data->cell[2][3]["rowspan"]=2;
$data->cell[3][1]["colspan"]=2;

// define fancy styles for individual cells.
$data->cell[1][1]["style"]      = 'STYLE="color: red; font: italic 18pt sans-serif;"';
$data->cell[1][1]["cell_style"] = 'CLASS="header"';

$data->cell[2][2]["style"]      = 'STYLE="font: 18pt monospace;"';
$data->cell[2][3]["style"]      = 'STYLE="background: red"';

$data->cell[3][1]["style"]      = 'STYLE="color: white; font: bold 18pt serif;"';
$data->cell[3][1]["cell_style"] = 'ALIGN="right" bgcolor="blue"';

// code and display.
$data->code_pretty();
$data->display_pretty();
?>
</body>
</html>