Location: PHPKode > scripts > Table Designer > tableDesigner.php
<?php
/*
 * 	tableDesigner - A class library to manage table using arrays
 *  Copyright (C) 2010  Daniele Monti (monska13 [at] gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */
?>

<?php

class tableDesigner
{
	private $intestazione;
	private $data;
	private $id_container;
	private $table_title;
	private $id_title;
	private $class_intestazione;
	private $class_td;
	private $class_tr;
	private $class_table;
	private $pathJquery;
	
	function tableDesigner($intestazione,$data,$id_container="",$table_title="Table",$id_title="tableDesigner_title",$class_intestazione="",$class_td="",$class_tr="",$class_table="")
	{
		$this->intestazione = $intestazione;
		$this->data = $data;
		$this->id_container = $id_container;
		$this->table_title = $table_title;
		$this->id_title = $id_title;
		$this->class_intestazione = $class_intestazione;
		$this->class_td = $class_td;
		$this->class_tr = $class_tr;
		$this->class_table = $class_table;
	}
	
	function getClassTag($node)
	{
		switch($node)
		{
			case "intestazione":
			{
				if($this->class_intestazione!="")
					return " class=\"".$this->class_intestazione."\"";
				else
					return "";
			}
			break;
			case "td":
			{
				if($this->class_td!="")
					return " class=\"".$this->class_td."\"";
				else
					return "";
			}
			break;
			case "tr":
			{
				if($this->class_tr!="")
					return " class=\"".$this->class_tr."\"";
				else
					return "";
			}
			break;
			case "table":
			{
				if($this->class_table!="")
					return " class=\"".$this->class_table."\"";
				else
					return "";
			}
			break;
			default: return ""; break;
		}
	}
	
	function printTable($return=FALSE)
	{
		$tb = "";
		$close_container = FALSE;
		
		$tb .= "<div id=\"".$this->id_title."\">".$this->table_title."</div>\n";
		
		if($this->id_container!="")
		{
			$close_container = TRUE;
			$tb .= "<div id=\"".$this->id_container."\">\n";
		}
		
			$tb .= "<table".$this->getClassTag("table").">\n";
				
				$tb .= "<tr".$this->getClassTag("tr").">\n";
				for($i=0;$i<count($this->intestazione);$i++)
				{
					$tb .= "<td".$this->getClassTag("td")."><span".$this->getClassTag("intestazione").">".$this->intestazione[$i]."</span></td>\n";
				}
				$tb .= "</tr>\n";
				
				for($i=0;$i<count($this->data);$i++)
				{
					$tb .= "<tr".$this->getClassTag("tr").">\n";
						for($j=0;$j<count($this->intestazione);$j++)
						{
							if($this->data[$i][''.$this->intestazione[$j].'']=="") $this->data[$i][''.$this->intestazione[$j].'']="&nbsp;";
							$tb .= "<td".$this->getClassTag("td").">".$this->data[$i][''.$this->intestazione[$j].'']."</td>\n";
						}
					$tb .= "</tr>\n";
				}
				
			$tb .= "</table>\n";
		
		if($close_container)
			$tb .= "</div>\n";
		
		$tb .= "\n";
		
		if($return)
			return $tb;
		else
			echo $tb;
		
	}
	
	function enableJquery($path,$init_status="open",$return=FALSE)
	{
		$this->pathJquery = $path;
		
		if($init_status!="open" && $init_status!="close")
			$init_status="open";
		
		$js = "";
		
		$js .= "<script src=\"".$this->pathJquery."\"></script>\n";
		
		$js .= "<script type=\"text/javascript\">\n";
			$js .= "table_status = \"".$init_status."\";\n";
			$js .= "$('#".$this->id_title."').click( function() {\n";
				$js .= "if(table_status==\"open\")\n";
				$js .= "{\n";
					$js .= "table_status=\"close\"\n";
					$js .= "$('#".$this->id_container."').slideUp('slow', function(){});\n";
				$js .= "}\n";
				$js .= "else\n";
				$js .= "{\n";
					$js .= "table_status=\"open\"\n";
					$js .= "$('#".$this->id_container."').slideDown('slow', function(){});\n";
				$js .= "}\n";
			$js .= "});\n";
		$js .= "</script>\n";
		
		$js .= "\n";
		
		if($return)
			return $js;
		else
			echo $js;
	}
	
	/* DEBUG METHOD */
	function printAllData()
	{
		$in = print_r($this->intestazione,true);
		$dt = print_r($this->data,true);
		
		echo "INTESTAZIONE:<br />\n";
		echo "<pre>".$in."</pre>\n";
		echo "<br />\n<br />\n";
		echo "DATA:<br />\n";
		echo "<pre>".$dt."</pre>\n";
	}
	
}

?>
Return current item: Table Designer