<?php
			unset($totalCondition);
	 	
			
			foreach ($colTypesStructure[$key] as $key_code => $keyVar) {
				
				$sqlUnion = array();
		
				foreach ($grp[$grKey] as $proj1) {
					$sql = "SELECT count(*) as n , ".$keyVar." as varName FROM ".$proj1."_structure WHERE flag_bad=101 AND ".$keyVar." > 0 ";
					if ($filter) $sql .=" AND ( ".$filter." )";	
					$sql .= " GROUP BY ".$keyVar;
					$sqlUnion[] = $sql;		
				}
				
				$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
				$res = $db->SQLexecute($sql);
				
				$n = 0;
				while ($zad = $db->fetchAssoc($res)) $n += $zad['n'];
				
				$freq[$keyVar] = $n;
				$totalCondition[$keyVar] = $keyVar." > 0 ";
				
				if ($_GET['showPonders']) {
					if (!$scoreFactors[$key][$zad['varName']]) $scoreFactors[$key][$zad['varName']] = 1;				
					$freqPonder[$keyVar] =  $n * $scoreFactors[$key][$zad['varName']] ;	
				}
												
			}
			
				$sqlUnion = array();
			
				foreach ($grp[$grKey] as $proj1) {
					$sql = "SELECT count(*) as n  FROM ".$proj1."_structure WHERE flag_bad=101 AND ( ";
					$sql .= implode(" OR ",$totalCondition);
					$sql .= " ) ";
					if ($filter) $sql .=" AND ( ".$filter." )";	
					$sqlUnion[] = $sql;
				}
				$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
				//echo $sql;
				$res = $db->SQLexecute($sql);
				/*$zad = $db->fetchAssoc($res);
				$total = $zad['n'];	*/
				while ($zad = $db->fetchAssoc($res)) $total += $zad['n'];
				
				//if ($specialLogging) echo $sql."<br>";
				
				$Graph['titel'] = $val;
				foreach ($colAns[$key] as $labVal) {
					$Graph['labels'][] = $labVal[1];
					$Graph['values'][] =  round( ($freq[$key."_".$labVal[0]]/$total) * 100,2);	
				}
				
				if ($_GET['showPonders']) {
					$GraphPonder['titel'] = $val."<br />* \"weighted\"";
					foreach ($colAns[$key] as $labVal) {
						$GraphPonder['labels'][] = $labVal[1];
						$GraphPonder['values'][] =  round( ($freqPonder[$key."_".$labVal[0]]/$total) * 100,2);
					}	
				}
				
				
				
				if ($_GET['showPonders']) {

					$dataArray['header']['data'] = array(($_GET['showVarName'] ? $key." " : "" ).$val,"N","%","N ponder","%");
							
				} 
				else 
				{
					$dataArray['header']['data'] = array(($_GET['showVarName'] ? $key." " : "" ).$val,"N","%");				
				}
						
				foreach ($colAns[$key] as $labVal) {
					$dat = array();
							
					$dat['val'] 	= ($_GET['showVarValue'] ? $labVal[0]." " : "" ).$labVal[1];
					$dat['N'] 		= ($freq[$key."_".$labVal[0]]) ? $freq[$key."_".$labVal[0]] : "0";
					$dat['%']		= ($freq[$key."_".$labVal[0]]) ? number_format(( round( ($freq[$key."_".$labVal[0]]/$total) * 100,2)),1) : "0";
					if ($_GET['showPonders']) {
							$dat['Nponder'] 		= ($freqPonder[$labVal[0]]) ? $freqPonder[$labVal[0]] : "0";
							$dat['%ponder']			= ($freqPonder[$labVal[0]]) ? number_format(( round( ($freqPonder[$labVal[0]]/$totalPonder) * 100,2)),1) : "0";
					}
					$dataArray['body']['data'][] = $dat;

				}
				
				if ($_GET['showPonders']) {
					$dataArray['footer']['data'] = array("TOTAL",$total,"100%",$totalPonder,"100%");
				} 
				else 
				{
					$dataArray['footer']['data'] = array("TOTAL",$total,"100%");
				}
				
				if ($CurrentPosInExcel) {
					$y = $CurrentPosInExcel[1] + 3;
				} else $y = 1;
						
				$CurrentPosInExcel = CreateTable($objPHPExcel,1,$y,$dataArray);
	
?>