Location: PHPKode > scripts > mysqlLikeDisplay > mysqllikedisplay/mysqllikedisplay.inc
<?php
/* Class to render php structs as mysql tables or HTML tables

/////////////////////////////////////////////////
// Class by Mark Williamson                   //
// hide@address.com        //
//////////////////////////////////////////////

 eg this struct: 

	$array = array(array("id"=>1, "name"=>"mark"), 
					array("id"=>2, "name"=>"jayne"));

 would display as:

	+-------+---------+
	| id    | name    |
	+-------+---------+
 	| 1     | Mark    | 
 	| 2     | Jayne   | 
	+-------+---------+

	Also added (somewhat bolted on and nasty) is ability to render
	html tables (full stylesheet support) 

	The only thing to be aware of is the use of "-" and "+" signs - if these
	are in the field values then they need to be replaced with &minus; or &plus; depending	
	- this will make sure the class doesn't get confused as to where cells end etc. 


example usage using above array: 

<?php
// MYSQL STYLE TABLE DISPLAY 
include("./mysqllikedisplay.inc");

	$array = array(array("id"=>1, "name"=>"mark"), 
					array("id"=>2, "name"=>"jayne"));

$ms = new display($array);
$ms->make_layout(1);

// HTML TABLE DISPLAY 
$htmltable = new display($array);
$htmltable->make_layout(0); //pass 0 here so that mysql style table isnt rendered
$htmltable->set_borderwidth(0);  
$htmltable->set_tablewidth("100%");  
$htmltable->set_cellspacing(1);
$htmltable->set_cellpadding(2);
$htmltable->create_stylesheet("cccccc", "000000", "x-small", "000066", "ffffff", "medium"); //style values for various settings
$htmltable->make_html_table(); //creates the table
$htmltable->parsehtml(); //renders the table to the screen

?>

*/

class display {
	var $widths = array();
	var $biggest = array();
	var $data = array();
	var $dis;
	var $divider;
	var $rows;
	var $emptyset=FALSE;
	var $write; //echo the output
	var $ascii_output;
	var $finalhtmltable;
	var $stylesheet;
	
	var $borderwidth = 0;
	var $tablewidth=600;
	var $bordercolor = "#000000";
	var $cellpadding = 2;
	var $cellspacing = 1;
	

	function calculate_widths($array){
		if(empty($array)){
			//check that there is some data to display
			$this->emptyset=TRUE;
			return(false);
		}
		//loop through each row
		$this->data = $array;
		$x=0;
		if(is_array($array)){
			foreach($array as $a){
				if(is_array($a)){
				while(list($key, $val) = each($a)){ 
						$this->widths[$x][$key] = strlen($val);
				}
				}
				++$x;
			}
		}
		$this->biggest = $this->get_longest_only();
		return($this->widths);
	}

	function get_longest_only(){
		$x=0;
		$array = $this->widths;
		foreach($array as $a){
			while(list($key, $val) = each($a)){ 
					if($val > $this->biggest[$key] || empty($this->biggest[$key])){	
						$this->biggest[$key] = $val;
					}
					if(strlen($key) > $this->biggest[$key]){
						$this->biggest[$key] = strlen($key);
					}
			}
			++$x;
		}
		return($this->biggest);
	}	

	function make_layout($write=1){
		$this->write = $write;
		if($this->emptyset){
			return("Empty set (0.00 hehehe)\n");
		}
			$first="+";	
			while(list($key, $val) = each($this->biggest)){ 

			$dis.="+";
				for($x=0;$x<$this->biggest[$key];$x++){
					$first .= "-";
				}
				$first.="+";
				$s="|".ucwords(str_replace("_", " ",$key));
				if(strlen($s)<= $this->biggest[$key]){
					for($x=strlen($s);$x<=$this->biggest[$key];$x++){
						$s.=" ";
					}
				}
				$second.=$s;
			}
		$this->divider = $first;
		$re = $first."\n".$second."|\n".$first."\n";
		$re.=$rows;	

		$this->rows = $this->make_body();
		$re.=$this->rows;
		if($this->write){
			echo "".$re."";
		}
		$this->ascii_out = $re;
		return($re);
	}

	function create_stylesheet($bg="ededed", $fontcol="000000", $fontsize="small", $bg2="444444", $fontcol2="ffffff", $fontsize2="medium"){
		$this->stylesheet = "
			<STYLE type='text/css'> 
			<!--
				.column-data { background-color:$bg; color:$fontcol; font-size:$fontsize; }
				.table-header { background-color:$bg2; color:$fontcol2; font-weight:bold; text-align:center; font-size:$fontsize2 }
			//-->
			</style>";
	}

	function make_body(){
		if(is_array($this->data)){
			foreach($this->data as $row){	
				if(is_array($row)){
				while(list($key, $val)=each($row)){

				if(is_array($val)){
					$out[0]=$val;
					$tr = new display($out);
					$tr->make_layout(0);
					$tr->set_borderwidth($this->borderwidth);
					$tr->set_cellpadding($this->cellpadding);
					$tr->set_cellspacing($this->cellspacing);
					$tr->set_bordercolor($this->bordercolor);		
					$val = "<Table><TR><TD> ".$tr->make_html_table()."</TD></tR></tAble>";
				}
					$r .= "|".$val;
					if(strlen($val)<= $this->biggest[$key]){
						for($x=strlen($val);$x < $this->biggest[$key]; $x++){	
							$r.=" ";
						}
					}
				}
				}
				$r.="|\n";
			}	
		}	
		$r.=$this->divider."\n";
		return($r);	

	}

	function get_divider(){
		return($this->divider);
	}
	
	var $use_tdwidths;
	var $num_cells;	
	function display($stuff, $widths=0){
			//constructor function
			$this->use_tdwidths = $widths;
			foreach($stuff as $s){
				$this->num_cells = count($s);	
				continue; 
			}
			$this->num_cells = ($this->num_cells) ? $this->num_cells : count($stuff);
			$this->widths = $this->calculate_widths($stuff);
	}

	function get_num_cells(){
		return($this->num_cells);
	}

	function set_borderwidth($wid){
		$this->borderwidth = $wid;
	}

	function set_cellpadding($pad){
		$this->cellpadding=$pad;
	}
	
	function set_cellspacing($spac){
		$this->cellspacing=$spac;
	}

	function set_tablewidth($tbwidth){
		$this->tablewidth=$tbwidth;
	}

	function set_bordercolor($col){
		$this->bordercolor=$col;
	}
	
	var $tdwidth;
	function make_html_table(){
		//converts ascii display into a proper html table
		$text = $this->ascii_out;			

		$this->tdwidth = floor(100/$this->num_cells)."%";

		if(!$this->use_tdwidths){
			$this->tdwidth="";
		}

		$rows = explode("\n", $text);
		$x=0;
		foreach($rows as $row){
			$last = strlen($row);
			$class = "column-data";	
			if($x==1){	
				$class = "table-header";
			}
			if(!ereg("^\+\-*", $row) && strlen($row)>0){
				$row = "<TR>\n <td class='$class' align='center' valign='middle' width=$this->tdwidth>".$row;
				$row .= "</td>\n</TR>\n";
				$row = str_replace("+", "</td><td class='$class' align='center' width=$this->tdwidth>", $row);
				$row = str_replace("|", "</td><td class='$class' align='center' width=$this->tdwidth>", $row);
				$row = str_replace("<td class='$class' align='center' width=$this->tdwidth></td>", "", $row); //remove any blanks
				$row = str_replace("<td class='$class' align='center' valign='middle' width=$this->tdwidth></td>", "", $row); //remove any blanks
				$htmloutput.=$row;
			}
			$x++;
		}

		$style = $this->stylesheet;
		$htmloutput = $style."\n<TABLE border='".$this->borderwidth."' bordercolor='".$this->bordercolor."' cellpadding='".$this->cellpadding."' cellspacing='".$this->cellspacing."' width='".$this->tablewidth."'>\n ".$htmloutput."\n</TABLE>";

		$this->finalhtmltable = $htmloutput;
		return($htmloutput);	
	}

	function returnhtml(){
		$this->finalhtmltable = str_replace("&plus;", "+", $this->finalhtmltable);
		$this->finalhtmltable = str_replace("&minus;", "-", $this->finalhtmltable);
		return($this->finalhtmltable);
	}
	function parsehtml(){
		$this->finalhtmltable = str_replace("&plus;", "+", $this->finalhtmltable);
		$this->finalhtmltable = str_replace("&minus;", "-", $this->finalhtmltable);
		echo $this->finalhtmltable;
	}
}
			
		
?>
Return current item: mysqlLikeDisplay