<?php
			$sqlUnion = array();
		
			foreach ($grp[$grKey] as $proj1) {
				$sql = "SELECT count(*) as n, ".$colTypesStructure[$key][0]." as varName FROM ".$proj1."_structure WHERE flag_bad = 101 AND ".$colTypesStructure[$key][0]." != ''";
					if ($filter) $sql .=" AND ".$filter;	
				$sql .= " GROUP BY ".$colTypesStructure[$key][0];
				//echo $sql;
				$sqlUnion[] = $sql;	
			}
				$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
				$res = $db->SQLexecute($sql);
				
				while ($zad = $db->fetchAssoc($res)) {
					$freq[$zad['varName']] += $zad['n'];	
					$total +=  $zad['n'];	
					if ($_GET['showPonders']) {
						// ponders
						$freqPonder[$zad['varName']] += $zad['n'] * abs($scoreFactors[$key][$zad['varName']]) ;	
						$totalPonder +=  $zad['n'] * abs($scoreFactors[$key][$zad['varName']]);	
					}
				}
								
				$Graph['titel'] = $val;
				foreach ($colAns[$key] as $labVal) {				
					$Graph['labels'][] = $labVal[1];
					$Graph['values'][] =  round( ($freq[$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[$labVal[0]]/$totalPonder) * 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 ($freq as $keyFreq => $nFreq) {
									$dat = array();
									
									$dat['val'] 	= $keyFreq;
									$dat['N'] 		= $nFreq;
									$dat['%']		= number_format(( round( ($nFreq / $total) * 100,2)),1);
									
									$dataArray['body']['data'][] = $dat;

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