Technology Temerity

Class – Tables

<?php 

class class_tables {

	/*
	Tables
	Damon Vaughn Caskey
	2013-03-21
	
	Miscellaneous table functions.
	*/
	
	public 	$cMarkup 		= NULL;	//Resulting markup output. Typically a table.
	private $oUtl			= NULL;	//Utility class object.
	
	function __construct($oDep)
	{
		/*
		Constructor
		Damon Vaughn Caskey
		2013_01_21
		
		Class constructor.
		*/
		
		/* Import object dependencies. */
		$this->oUtl = $oDep['Utl'];
						
		/* Verify object dependencies. */
		if(!$this->oUtl)	trigger_error("Missing object dependency: Utility.", E_USER_ERROR);		
	}
	
	public function tables_db_output($oDB, $bRowCount = TRUE, $cFieldSkip = NULL, $cAuxLink = array("Header" => NULL, "Link" => NULL, "Target" => "_blank", "Text" => "Go"), $cAuxLinkFields = NULL, $cRowStyle = NULL)
	{	
		/*
		tables_db_output
		Damon Vaughn Caskey
		2013-03-20
		
		Create complete table markup from database query.
		
		Preconditions:
			Executed database query.
			$oDB: Object with object variables populated by query.
			$bRowCount: True = Display a row count preceding table.
			$cFieldSkip['<fieldname>', ...]: Array of fields from query to skip when creating table.
			$cAuxLink['Link', 'Text', 'Target']: Adds action link to end of table.
				Link: Page name.
				Target: Page target (_new, _blank, etc.)
				Text: Text to display.				
			$cAuxLinkFields['<fieldname>', ...]: Fields to pass as part of action link.
			$cRowStyle[even, odd]: Array to override default alternate row style.
				
		Postconditions:
			Populate and return $cMarkup with table markup string.
		*/
		
		$i						= NULL;	//Working counter.
		$iRowCount 				= NULL;	//Count of records retrieved by query.
		$cOutput				= NULL;	//Output string.
		$cFieldMetadata			= NULL;	//Metadata collection (field name, type, etc.) for database columns.
		$cFieldMetaDataName 	= NULL; //Individual item name from metadata colelction.
		$cFieldMetaDataValue	= NULL;	//Individual item value from metadata collection.
		$iFields				= NULL;	//Field counter/index.
		$cFieldName				= NULL; //Field name array.
		$cName					= NULL;	//Table markup write in: Field name.
		$cValue 				= NULL;	//Table markup write in: Field value.
		$cLink 					= NULL;	//Table markup write in: Action link.
		
		/* Add extra markup if cAuxLink has a value, otherwise leave NULL. " */
		
		if($cAuxLink["Link"] != NULL)
		{			
			$cAuxLink["Link"] .= "?";
			$cAuxLink["Header"] = $cAuxLink["Header"] != NULL ? "<th>".$cAuxLink["Header"]."</th>" : "<th>Action</th>";			
		}
		
		if($bRowCount)
		{
			$iRowCount = $oDB->iDBRowCount; 
			$cOutput .= '<span class="row_count">' .$iRowCount. ' records found.</span><br/><br/>';
		}
		
		$cOutput .= '<div title="Table" class="overflow"><table border="0" cellpadding="5" cellspacing="0" bgcolor="#CCCCCC"><tr>';	
		
		/* Zero counter */
		$i = 0;
		
		/* Loop each column in query result. */	
		foreach($oDB->cDBMeta as $cFieldMetadata)
		{					
			/* Loop coloumn metadata collection. */
			foreach($cFieldMetadata as $cFieldMetaDataName => $cFieldMetaDataValue)
			{			
				/* Column name? */				
				if($cFieldMetaDataName == 'Name')
				{						
					/* Check field skip array before using this field name */
					if(!in_array($cFieldMetaDataValue, $cFieldSkip))
					{						
						/* Output to table header markup and populate name array. */									
						$cOutput .= "<th>".$cFieldMetaDataValue."</th>";	//Populate table header markup.																	
					}
					
					$cFieldName[$i] = $cFieldMetaDataValue;				//Populate Name array.
					
					/* Increment field count. */
					$iFields++;
				}					
			}
			
			/* Increment counter. */
			$i++;							
		}		
					
		$cOutput .= $cAuxLink["Header"];	
				
		/* Output query results as table. */
		while($oDB->db_line(SQLSRV_FETCH_NUMERIC))
		{		
		
			$cLink 		= $cAuxLink["Link"];
			
			/* Increment line counter */
			$iLine++;
			
			/* Insert table row and style. */
			$cOutput .= "<tr bgcolor='".$this->oUtl->utl_color_alternation($iLine)."'>";
											
			for ($i = 0; $i < $iFields; $i++)
			{					
				$cName		= $cFieldName[$i];
				$cValue 	= $oDB->cDBLine[$i];								
			
				/* Check field skip array before using this field. */
				if(!in_array($cName, $cFieldSkip))
				{				
					$cOutput .= "<td>".$cValue."</td>";
				}
				
				if($cLink != NULL)
				{			
					if(in_array($cName, $cAuxLinkFields))
					{
						$cLink .= $cFieldName[$i]."=".$cValue."&";
					}				
				}
			}
			
			if($cLink != NULL)
			{
				$cOutput .= '<td><a href="'.$cLink.'" target="'.$cAuxLink["Target"].'">'.$cAuxLink["Text"].'</a></td>';								
			}
		}
					
		$cOutput .= "</table><br/><br/></div>";
		
		$this->cMarkup = $cOutput;
		
		return $this->cMarkup;
	}		
}
?>



Author: Damon Caskey

Hello all, Damon Caskey here - the esteemed owner of this little slice of cyberspace. Welcome!

Leave a Reply