<?php
			$sqlUnion = array();
		
			foreach ($grp[$grKey] as $proj1) {
				$sql = "SELECT count(*) as n, ".$key." as varName FROM ".$proj1."_structure WHERE flag_bad = 101 AND ".$key." > 0 ";
					if ($filter) $sql .=" AND ".$filter;	
				$sql .= " GROUP BY ".$key;
				
				$sqlUnion[] = $sql;	
			}
				$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
				$res = $db->SQLexecute($sql);
				//echo $sql;
				if ($db->NumRows($res)) {
						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']]);	
							}
						}
															
						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[$labVal[0]]) ? $freq[$labVal[0]] : "0";
									$dat['%']		= ($freq[$labVal[0]]) ? number_format(( round( ($freq[$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);
				}			
					
?>