<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $scSSIV_arr = array();
 $Q19a1_arr = array();
 $Q19a2_arr = array();
 $Q19a3_arr = array();
 $Q19e_arr = array();
 $Q19e3_arr = array();
 $Q19ef_arr = array();
 $Q19a_arr = array();
 $Q19b_arr = array();
 $Q19d_arr = array();
 $Q19e2O_arr = array();
 $Q19_arr = array();
 $Q23a_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
 $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($Q19a1_arr,0);
        array_push($Q19a2_arr,0);
        array_push($Q19a3_arr,0);
        array_push($Q19e_arr,0);
        array_push($Q19e3_arr,0);
        array_push($Q19ef_arr,0);
        array_push($Q19a_arr,0);
        array_push($Q19b_arr,0);
        array_push($Q19d_arr,0);
        array_push($Q19_arr,0);
        array_push($Q19e2O_arr,0); 
      }
      else
      {
          /*Q19 script start*/
          $columnS=array("record","Q9g","Q19",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
        $q19=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19` IN (1,2,3,4,5,6,7,8) ");
        $Q19=mysqli_fetch_array($q19); 
        $Q19V = round(($Q19[0] / $sql_base ) * 100,1);
        array_push($Q19_arr,$Q19V);
        /*Q19 script end*/
        
        /*Q19a1 script start*/
        $columnS=array("record","Q9g","Q19a1",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q19a1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19a1` = '1' ");
            $Q19a1=mysqli_fetch_array($q19a1); 
            $Q19a1V = round(($Q19a1[0] / $sql_base ) * 100,1);
            array_push($Q19a1_arr,$Q19a1V);
        /*Q19a1 script end*/

        /*Q19a2 script start*/
        $columnS=array("record","Q9g","Q19a2",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q19a2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19a2` = '1' ");
            $Q19a2=mysqli_fetch_array($q19a2); 
            $Q19a2V = round(($Q19a2[0] / $sql_base ) * 100,1);
            array_push($Q19a2_arr,$Q19a2V);
        /*Q19a2 script end*/

        /*Q19a3 script start*/
        $columnS=array("record","Q9g","Q19a3",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q19a3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19a3` = '1' ");
            $Q19a3=mysqli_fetch_array($q19a3); 
            $Q19a3V = round(($Q19a3[0] / $sql_base ) * 100,1);
            array_push($Q19a3_arr,$Q19a3V);
        /*Q19a3 script end*/

        /*Q19e script start*/
        $columnS=array("record","Q9g","Q19e",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q19e=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19e` = '1' ");
            $Q19e=mysqli_fetch_array($q19e); 
            $Q19eV = round(($Q19e[0] / $sql_base ) * 100,1);
            array_push($Q19e_arr,$Q19eV);
        /*Q19e script end*/
        
        /*Q19e3 script start*/
        $columnS=array("record","Q9g","Q19e3",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq19e3 = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19e3` != '' ");
            $baseq19e3=mysqli_fetch_array($Baseq19e3);
            $q19e3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19e3` = '1' ");
            $Q19e3=mysqli_fetch_array($q19e3); 
            $Q19e3V = round(($Q19e3[0] / $sql_base ) * 100,1);
            array_push($Q19e3_arr,$Q19e3V);
        /*Q19d script end*/

        /*Q19f script start*/
        $columnS=array("record","Q9g","Q19f",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq19f = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19f` != '' ");
            $baseq19f=mysqli_fetch_array($Baseq19f);
            $q19f=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19f` = '1' ");
            $Q19f=mysqli_fetch_array($q19f); 
            $Q19fV = round(($Q19f[0] / $sql_base ) * 100,1);
            array_push($Q19ef_arr,$Q19fV);
        /*Q19f script end*/

        /*Q19a script start*/
        $columnS=array("record","Q9g","Q19a",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq19a = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19a` != '' ");
            $baseq19a=mysqli_fetch_array($Baseq19a);
            $q19a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19a` = '2' ");
            $Q19a=mysqli_fetch_array($q19a); 
            $Q19aV = round(($Q19a[0] / $sql_base ) * 100,1);
            array_push($Q19a_arr,$Q19aV);
        /*Q19a script end*/

        /*Q19b script start*/
        $columnS=array("record","Q9g","Q19b",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq19b = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19b` != '' ");
            $baseq19b=mysqli_fetch_array($Baseq19b);
            $q19b=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19b` = '2' ");
            $Q19b=mysqli_fetch_array($q19b); 
            $Q19bV = round(($Q19b[0] / $sql_base ) * 100,1);
            array_push($Q19b_arr,$Q19bV);
        /*Q19b script end*/

        /*Q19d script start*/
        $columnS=array("record","Q9g","Q19d",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq19d = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19d` != '' ");
            $baseq19d=mysqli_fetch_array($Baseq19d);
            $q19d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q19d` = '2' ");
            $Q19d=mysqli_fetch_array($q19d); 
            $Q19dV = round(($Q19d[0] / $sql_base ) * 100,1);
            array_push($Q19d_arr,$Q19dV);
        /*Q19d script end*/
        
        /*Q19e2 script start*/
        $columnS=array("record","Q9g","Q19e2",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
		$q19e2O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q19e2` in (9,10) ");
		$Q19e2O=mysqli_fetch_array($q19e2O); 
		$Q19e2OV = round(($Q19e2O[0] / $sql_base ) * 100,1); 
		array_push($Q19e2O_arr,$Q19e2OV); 
		/*Q19e2 script end*/
		
		/*Q23a script start*/
        $columnS=array("record","Q9g","Q23a",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
        $Baseq23a = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q23a!= '' ");
        $baseq23a=mysqli_fetch_array($Baseq23a);
        $q23a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q23a = '1' ");
        $Q23a=mysqli_fetch_array($q23a); 
        $Q23aV = round(($Q23a[0] / $sql_base ) * 100,1);
        array_push($Q23a_arr,$Q23aV);
        /*Q23a script end*/
		
      }
        /*scSSI script start*/
        $columnS=array("record","Q9g","Q19_n1",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $scSSI=mysqli_query($conn,"SELECT avg(`Q19_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
            $scSSIV=mysqli_fetch_array($scSSI); 
            $scSSIV2 = round($scSSIV[0]);
            array_push($scSSIV_arr,$scSSIV2);
        /*scSSI script end*/
      
   }
    $data['Q19'] = $Q19_arr;
   $data['scSSI'] = $scSSIV_arr;
   $data['Q19a1'] = $Q19a1_arr;
   $data['Q19a2'] = $Q19a2_arr;
   $data['Q19a3'] = $Q19a3_arr;
   $data['Q19e'] = $Q19e_arr;
   $data['Q19e3'] = $Q19e3_arr;
   $data['Q19f'] = $Q19ef_arr;
   $data['Q23a'] = $Q23a_arr;

   $data['Q19a'] = $Q19a_arr;
   $data['Q19b'] = $Q19b_arr;
   $data['Q19d'] = $Q19d_arr;
   
   $data['Q19e2O'] = $Q19e2O_arr;
    
  
   

 echo json_encode($data);

?>