Location: PHPKode > scripts > Simple Table Wrapper > simpletablewrapper.php
<?php
/*
* 
* Author: Micha&#322; Paku&#322;a
* Use new SimpleTableWrapper([INPUT DATA]) and next to "render" method to generate html table
* Class recognize type of input data (single or multi-dimensional array, object or mysql resource)
* exmaple:
* 	$table = new SimpleTableWrapper(INPUT_DATA);
* 	$table->render();
* or:
*	$table->get_data();
*/
class SimpleTableWrapper{
	var $render='<style type="text/css">th{background:#333; color:#fff; text-align:center;} td, th{padding:2px 5px; border:1px dashed #999;}</style>';
	public function simpletablewrapper($in){
		switch(gettype($in))
		{
			case 'resource' :
				$tr=array();
				while ($l=mysql_fetch_assoc($in)){
					if(count($tr)==0) $tr[]='<tr><th>'.implode('</th><th>',array_keys($l)).'</th></tr>';
					$tr[]='<tr><td>'.implode('</td><td>',$l).'</td></tr>';
				}
			break;
			case 'object' :
				foreach($in as $k=>$l){
					if(is_array($l)){
						if(count($tr)==0) $tr[]='<tr><th>'.implode('</th><th>',array_keys($l)).'</th></tr>';
						$tr[]='<tr><td>'.implode('</td><td>',$l).'</td></tr>';
					} else {
						$tr[]='<tr><th>'.$k.'</th><td>'.$l.'</td></tr>';
					}
				}
			break;
			case 'array' :
				foreach($in as $k=>$l){
					if(is_array($l)){
						if(count($tr)==0) $tr[]='<tr><th>'.implode('</th><th>',array_keys($l)).'</th></tr>';
						$tr[]='<tr><td>'.implode('</td><td>',$l).'</td></tr>';
					} elseif(is_object($l)){
						if(count($tr)==0) $tr[]='<tr><th>'.implode('</th><th>',array_keys(get_object_vars($l))).'</th></tr>';
						$tr[]='<tr><td>'.implode('</td><td>',get_object_vars($l)).'</td></tr>';
					} else {
						$tr[]='<tr><th>'.$k.'</th><td>'.$l.'</td></tr>';
					}
				}
			break;
			default:
				$tr[]='<tr><th>Error</th><td>unrecognized type of input data</td></tr>';
			
		}
		$this->render .= '<table style="border:1px solid #777; border-collapse:collapse;">'.implode("\n",$tr).'</table>';;
	}
	
	public function get_table()
	{
		return $this->render;
	}

	public function nostylize()
	{
		$this->stylecode='';
		return $this;
	}
	
	public function render()
	{
		echo $this->render;
	}
}
?>
Return current item: Simple Table Wrapper