<?php
 include '../config.php';
$data = array();
$sql_base;

$condX = $_POST['condX'];
$conX = $_POST['conX'];
$timeZ = $_POST['timeX'];

$deSSIV_arr = array();
$Q28A_1_arr = array();
$Q28A_2_arr = array();
$Q28A_3_arr = array();
$Q28A_4_arr = array();
$Q28B_1_arr = array();
$Q28B_2_arr = array();
$Q28B_3_arr = array();
$Q28E_1_arr = array();
$Q28E_2_arr = array();
$Q28E_3_arr = array();
$Q28d_arr = array();
$Q29_arr = array();
$Q30_arr = array();
$Q31_arr = array();
$Q33_arr = array();
$Q34_arr = array();
$Q27_arr = array();
$tablecondX = $_POST['tablecondX'];
$columnS=array("record","Q9g",$tablecondX);
$mq=query_marge($timeZ,$columnS);
 $poor="1,2,3,4";
 $average="5,6";
 $Outstanding="7,8,9,10";
   foreach($timeZ as $key => $value)
   {
      if($conX==1)
      {    
         $condX="";   
         $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) ");
         $baseX=mysqli_fetch_array($q);
         $base=$baseX[0];
      }
     if($conX==2)
      {
         
         $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
         $baseX=mysqli_fetch_array($q);
         $base=$baseX[0];
      }

      $sql_base = $base;
      if($sql_base==0)
      {
        array_push($Q28A_1_arr,0);
        array_push($Q28A_2_arr,0);
        array_push($Q28A_3_arr,0);
        array_push($Q28A_4_arr,0);
        array_push($Q28B_1_arr,0);
        array_push($Q28B_2_arr,0);
        array_push($Q28B_3_arr,0);
        array_push($Q28E_1_arr,0);
        array_push($Q28E_2_arr,0);
        array_push($Q28E_3_arr,0);
        array_push($Q28d_arr,0);
        array_push($Q29_arr,0);
        array_push($Q30_arr,0);
        array_push($Q31_arr,0);
        array_push($Q33_arr,0);
        array_push($Q34_arr,0);
        array_push($Q27_arr,0);
      }
      else
      {
            $columnS=array("record","Q9g","Q27",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q27=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q27` in (1,2,3,4,5,6,7,8) ");
    		$Q27=mysqli_fetch_array ($q27); 
    		$Q27V = round(($Q27[0]/$sql_base )*100,1);
    		array_push($Q27_arr,$Q27V);
		
        /*Q28a script start*/
            $columnS=array("record","Q9g","Q28a",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q28a_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28a` =1 ");
            $Q28a_1=mysqli_fetch_array ($q28a_1); 
            $Q28A_1 = round(($Q28a_1[0] / $sql_base ) * 100,1);
            array_push($Q28A_1_arr,$Q28A_1);

            $q28a_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28a` =2 ");
            $Q28a_2=mysqli_fetch_array ($q28a_2); 
            $Q28A_2 = round(($Q28a_2[0] / $sql_base ) * 100,1);
            array_push($Q28A_2_arr,$Q28A_2);

            $q28a_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28a` =3 ");
            $Q28a_3=mysqli_fetch_array ($q28a_3); 
            $Q28A_3 = round(($Q28a_3[0] / $sql_base ) * 100,1);
            array_push($Q28A_3_arr,$Q28A_3);

            $q28a_4=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28a` =4 ");
            $Q28a_4=mysqli_fetch_array ($q28a_4); 
            $Q28A_4 = round(($Q28a_4[0] / $sql_base ) * 100,1);
            array_push($Q28A_4_arr,$Q28A_4);
        /*Q28a script end*/
        
        /*Q28b script start*/	
            $columnS=array("record","Q9g","Q28b",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q28b_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28b` in (1) ");
            $Q28b_1=mysqli_fetch_array ($q28b_1); 
            $Q28B_1 = round(($Q28b_1[0] / $sql_base ) * 100,1);
            array_push($Q28B_1_arr,$Q28B_1);

            $q28b_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28b` in (2) ");
            $Q28b_2=mysqli_fetch_array ($q28b_2); 
            $Q28B_2 = round(($Q28b_2[0] / $sql_base ) * 100,1);
            array_push($Q28B_2_arr,$Q28B_2);

            $q28b_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28b` in (3) ");
            $Q28b_3=mysqli_fetch_array ($q28b_3); 
            $Q28B_3 = round(($Q28b_3[0] / $sql_base ) * 100,1);
            array_push($Q28B_3_arr,$Q28B_3);
        /*Q28b script end*/

        /*Q28e script start*/	
            $columnS=array("record","Q9g","Q28e",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q28e_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28e` in (1) ");
            $Q28e_1=mysqli_fetch_array ($q28e_1); 
            $Q28E_1 = round(($Q28e_1[0] / $sql_base ) * 100,1);
            array_push($Q28E_1_arr,$Q28E_1);

            $q28e_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28e` in (2) ");
            $Q28e_2=mysqli_fetch_array ($q28e_2); 
            $Q28E_2 = round(($Q28e_2[0] / $sql_base ) * 100,1);
            array_push($Q28E_2_arr,$Q28E_2);

            $q28e_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28e` in (3) ");
            $Q28e_3=mysqli_fetch_array ($q28e_3); 
            $Q28E_3 = round(($Q28e_3[0] / $sql_base ) * 100,1);
            array_push($Q28E_3_arr,$Q28E_3);
	    /*Q28e script end*/

        /*Q28d script start*/	
            $columnS=array("record","Q9g","Q28d",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q28d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q28d` in (2) ");
            $Q28d=mysqli_fetch_array ($q28d); 
            $Q28dV = round(($Q28d[0] / $sql_base ) * 100,1);
            array_push($Q28d_arr,$Q28dV);
        /*Q28d script end*/

        /*Q29 script start*/	
            $columnS=array("record","Q9g","Q29",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q29=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q29` in (1) ");
            $Q29=mysqli_fetch_array ($q29); 
            $Q29V = round(($Q29[0] / $sql_base ) * 100,1);
            array_push($Q29_arr,$Q29V);
        /*Q29 script end*/
		
	
        /*Q30 script start*/
            $columnS=array("record","Q9g","Q30",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q30=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q30` in (1) ");
            $Q30=mysqli_fetch_array ($q30); 
            $Q30V = round(($Q30[0] / $sql_base ) * 100,1);
            array_push($Q30_arr,$Q30V);
        /*Q30 script end*/

        /*Q31 script start*/
            $columnS=array("record","Q9g","Q31",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q31=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q31` in (1) ");
            $Q31=mysqli_fetch_array ($q31); 
            $Q31V = round(($Q31[0] / $sql_base ) * 100,1);
            array_push($Q31_arr,$Q31V);
        /*Q31 script end*/
	
	
        /*Q33 script start*/	
            $columnS=array("record","Q9g","Q33",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q33=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q33` in (1) ");
            $Q33=mysqli_fetch_array ($q33); 
            $Q33V = round(($Q33[0] / $sql_base ) * 100,1);
            array_push($Q33_arr,$Q33V);
        /*Q33 script end*/
	
        /*Q34 script start*/	
            $columnS=array("record","Q9g","Q34",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q34=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q34` in (1) ");
            $Q34=mysqli_fetch_array ($q34); 
            $Q34V = round(($Q34[0] / $sql_base ) * 100,1);
            array_push($Q34_arr,$Q34V);
        /*Q34 script end*/
      }
        /*deSSI script start*/
            $columnS=array("record","Q9g","Q27_n1",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $deSSI=mysqli_query($conn,"SELECT avg(`Q27_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
            $deSSIV=mysqli_fetch_array($deSSI); 
            $deSSIV2 = round($deSSIV[0]);
            array_push($deSSIV_arr,$deSSIV2);
        /*deSSI script end*/
   }

   $data['deSSIV_arr'] = $deSSIV_arr;
   $data['Q28A_1_arr'] = $Q28A_1_arr;
   $data['Q28A_2_arr'] = $Q28A_2_arr;
   $data['Q28A_3_arr'] = $Q28A_3_arr;
   $data['Q28A_4_arr'] = $Q28A_4_arr;
   $data['Q28B_1_arr'] = $Q28B_1_arr;
   $data['Q28B_2_arr'] = $Q28B_2_arr;
   $data['Q28B_3_arr'] = $Q28B_3_arr;

   $data['Q28E_1_arr'] = $Q28E_1_arr;
   $data['Q28E_2_arr'] = $Q28E_2_arr;
   $data['Q28E_3_arr'] = $Q28E_3_arr;

   $data['Q28d_3_arr'] = $Q28d_arr;
   $data['Q29_arr'] = $Q29_arr;
   $data['Q30_arr'] = $Q30_arr;
   $data['Q31_arr'] = $Q31_arr;
   $data['Q33_arr'] = $Q33_arr;
   $data['Q34_arr'] = $Q34_arr;
   
   $data['Q27'] = $Q27_arr;

    
  
    echo json_encode($data);

?>
























