Location: PHPKode > scripts > XHTML Table generator > xhtml-table-generator/Table.php
<?php
/**
 * Title: HTML Table generator
 * Description: Generates XHTML 1.0 Strict tables
 * @requires The LinkedList package. The packages is used to manges rows and table cells
 * @author Oddleif Halvorsen | leif-hide@address.com
 * @version 1.0
 */

include_once("HTMLTag.php");
include_once("Tr.php");
include_once("Td.php");
include_once("Th.php");
include_once("LinkedList.php");

class Table extends HTMLTag{
	//the list of rows
	var $rows;
	
	//table tag attributes
	var $border;
	var $width;
	var $cellspacing;
	var $cellpadding;
	var $frame;
	var $rules;
	var $summary;
	
	//some other nessesary attributes
	var $tableHeaderIsSet; //bool value to check if the header is set or not.
	
	function Table(){
		parent::HTMLTag();
		
		$this->rows = new LinkedList();
		$this->tableHeaderIsSet = FALSE;
	}
	
	function setBorder($border){ $this->border = $border;	}
	function setCellpadding($padding){ $this->cellpadding = $padding; }
	function setCellSpacing($spacing){ $this->cellspacing = $spacing;	}
	function setFrame($frame){ $this->frame = $frame;	}
	function setRules($rules){ $this->rules = $rules;	}
	function setSummary($summary){ $this->summary = $summary;	}
	
	/**
	 * Sets the with for the table.
	 * @param $with A numeric value
	 * @param $mesurement px || %
	 */
	function setWidth($with, $mesurement = "px"){
		$this->width = ($with . $mesurement);
	}
	
	function getBorder(){	return (isset($this->border) ? $this->border : FALSE); }
	function getCellpadding(){ return (isset($this->cellpadding) ? $this->cellpadding : FALSE);	}
	function getCellSpacing(){ return (isset($this->cellspacing) ? $this->cellspacing : FALSE);	}
	function getFrame(){ return (isset($this->frame) ? $this-frame : FALSE); }
	function getRules(){ return (isset($this->frame) ? $this->rules : FALSE);	}
	function getSummary(){ return (isset($this->frame) ? $this->summary : FALSE);	}
	function getWidth(){ return (isset($this->width) ? $this->width : FALSE);	}
	
	/**
	 * Adds a new row to the end of the table
	 * @param $row A Row object
	 */
	function addRow($row){
		$this->rows->add($row);
	}
	
	/**
	 * Sets the table header. 
	 * Uses add if the list is empty, addAtPosition does not work at a empty list.
	 * @param $tableHeader A row object with the header
	 */
	function setTableHeader($tableHeader){
		if(!$this->tableHeaderIsSet){
			if(!$this->rows->isEmpty())
				$this->rows->addAtPosition(0, $tableHeader);
			else
				$this->rows->add($tableHeader);
				
			$this->tableHeaderIsSet = TRUE;
		}
		else
			return FALSE;
	}
	
	/**
	 * Sets the column headers for the table. Inserts the
	 * coulmn headers on row 0 og row 1, depending on wheter the 
	 * table header is set.
	 * @param $tableHeader A row object with the header
	 */	
	function setColumnHeaders($columnHeaders){
		if(!$this->rows->isEmpty() && $this->rows->size()>1){
			$columnHeaderRowNum = ($this->tableHeaderIsSet ? 1 : 0);
			$this->rows->addAtPosition($columnHeaderRowNum, $columnHeaders);
		}
		else
			$this->rows->add($columnHeaders);
	}
	
	/**
	 * Return the html code for the table
	 * @return The html code for the table
	 */
	function getHtml(){
		$table = &$this->setTableAttributes();
				
		//inserts the table rows
		$iterator = $this->rows->iterator();
		while($iterator->hasNext()){
			$tmpRow = $iterator->getNext();
			$table .= $tmpRow->getHtml();
		}

		$table .= "</table>";
		return $table;
	}
	
	/**
	 * Sets the attributes for the table
	 * @return A table tag with attributes
	 */
	function setTableAttributes(){
		$table = "<table";
		
		//gets the parents attributes
		if(parent::getAttributes() != FALSE)
			$table .= parent::getAttributes();
		
		if($this->getBorder())
			$table .=  (" border=\"" . $this->getBorder() . "\"");
		if($this->getCellpadding())
			$table .=  (" cellpadding=\"" . $this->getCellpadding() . "\"");
		if($this->getCellSpacing())
			$table .=  (" cellspacing=\"" . $this->getCellSpacing() . "\"");
		if($this->getFrame())
			$table .=  (" frame=\"" . $this->getFrame() . "\"");
		if($this->getRules())
			$table .=  (" rules=\"" . $this->getRules() . "\"");
		if($this->getSummary())
			$table .=  (" summary=\"" . $this->getSummary() . "\"");
		if($this->getWidth())
			$table .=  (" width=\"" . $this->getWidth() . "\"");
			
		$table .= ">";
		
		return $table;
	}
}
?>
Return current item: XHTML Table generator