Location: PHPKode > scripts > Gerar Tabela Semanticamente Correta > gerar-tabela-semanticamente-correta/tabela.class.php
<?php

/*
* @author: Thiago S.F. - hide@address.com
* @example: 
* $tabela = new tabela("id=\"dados\"", 1);
* $tabela->setCapition("Tabela Gerada Dinamicamente Pela Classe <b>" . get_class($tabela) . "</b>" );
* $tabela->th( array( array("Nome", " class=\"cinza\""), "Idade") );
* $tabela->td( array("Joao", 25) );
* $tabela->td( array("Maria", 20), " class=\"vermelho\"" );
*/

class tabela
{
	// Variaveis
	public $thTotal;
	public $tdTotal;
	public $atributos;
	public $tabulcaoTotal;
	
	public function __construct ( $atributos = "", $tabulcaoTotal = 0 ) {
		$this->atributos = $atributos;
		$this->tabulcaoTotal = $tabulcaoTotal;
	}
	
	private function tab ($n) {
		$tabulacao = "";
		$n += $this->tabulcaoTotal;
		for( $i=0; $i < $n; $i++ ) {
			@$tabulacao .= "\t";
		}
		return $tabulacao;
	}
	
	public function setCapition($caption) {
		$this->capition = $this->tab(1) . "<caption>{$caption}</caption>\n";
	}
	
	public function th ($args, $attTr = "") {
		$this->thTotal = $this->tab(1) . "<thead>\n";
		$this->thTotal .= $this->tab(2) . "<tr" . $attTr . ">\n";
		
		foreach ( $args as $valor ) {
			if ( gettype($valor) == "array" ) {
				$this->thTotal .= $this->tab(3) . "<th" . $valor[1] . ">" . $valor[0] . "</th>\n";
			}
			else {
				$this->thTotal .= $this->tab(3) . "<th>{$valor}</th>\n";
			}
		}
		
		$this->thTotal .= $this->tab(2) . "</tr>\n";
		$this->thTotal .= $this->tab(1) . "</thead>\n";
	}
	
	public function td ($args, $attTr = "") {
		@$this->tdTotal .= $this->tab(2) . "<tr" . $attTr . ">\n";		
		
		foreach ( $args as $valor ) {
			if ( gettype($valor) == "array" ) {
				$this->tdTotal .= $this->tab(3) . "<td" . $valor[1] . ">" . $valor[0] . "</td>\n";
			}
			else {
				$this->tdTotal .= $this->tab(3) . "<td>{$valor}</td>\n";
			}
		}
		
		$this->tdTotal .= $this->tab(2) . "</tr>\n";
	}
	
	public function exibe() {
		print $this->tab(0) . "<table " . $this->atributos . ">\n";
		print $this->capition;
		print $this->thTotal;
		print $this->tab(1) . "<tbody>\n";
		print $this->tdTotal;
		print $this->tab(1) . "</tbody>\n";
		print $this->tab(0) . "</table>\n";
	}
}

?>
Return current item: Gerar Tabela Semanticamente Correta