<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $Q20_arr = array(); 
 $Q21_arr = array();
 $Q22O_arr = array(); 
 $Q23a_arr = array();
 $Q23b_arr = array();
 $Q23c_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
 $poor="1,2,3,4";
$average="5,6,7,8";
$Outstanding="9,10";
$tablecondX = $_POST['tablecondX'];
$columnS=array("record","Q9g",$tablecondX);
$mq=query_marge($timeZ,$columnS);
   foreach($timeZ as $key => $value)
   {
    $Q23V_arr = array();
      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($Q20_arr,0);
        array_push($Q21_arr,0);
        array_push($Q22O_arr,0);
        array_push($Q23a_arr,0);
        array_push($Q23b_arr,0);
        array_push($Q23c_arr,0);
      }
      else
      {
        /*Q20 script start*/
            $columnS=array("record","Q9g","Q20",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q20=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q20` = '1' ");
            $Q20=mysqli_fetch_array($q20); 
            $Q20V = round(($Q20[0] / $sql_base ) * 100,1);
            array_push($Q20_arr,$Q20V);
        /*Q20 script end*/

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

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

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

            $q23b=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q23` = '2' ");
            $Q23b=mysqli_fetch_array($q23b); 
            $Q23B = round(($Q23b[0] / $sql_base ) * 100,1);
            array_push($Q23b_arr,$Q23B);

            $q23c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q23` = '3' ");
            $Q23c=mysqli_fetch_array($q23c); 
            $Q23C = round(($Q23c[0] / $sql_base ) * 100,1);
            array_push($Q23c_arr,$Q23C);

        /*Q23 script end*/
      }
        
      
   }
   $data['Q20'] = $Q20_arr;
   $data['Q21'] = $Q21_arr;
   $data['Q22O'] = $Q22O_arr;
   $data['Q23a'] = $Q23a_arr;
   $data['Q23b'] = $Q23b_arr;
   $data['Q23c'] = $Q23c_arr;
  
   

 echo json_encode($data);

?>