<?php
header('Content-Type: text/html; charset=ISO-8859-1');
require_once "db_connect.php";
$province = mysqli_real_escape_string($db,$_GET['code']);
$queryIs = "SELECT sum(number) as number,gender,month FROM `suicide_data` group by gender,month order by id asc" ;

if($province != 'all'){
  $queryIs = $queryIs." where province = '$province'";
}

//$queryIs = $queryIs." GROUP by month(date)";

$query = "SELECT sum(number) as number, month FROM `suicide_data` group by month order by id asc";

if($province != 'all'){
  $query = $query." where province = '$province'";
}

$result = mysqli_query($db,$queryIs);
$result1 = mysqli_query($db,$query);

$maleArray = array();
$femaleArray = array();
$otherArray = array();
$totalArray = array();
$finalArray = array();
$monthArray = array();

//echo $queryIs;
//array_push($finalArray, $sexualArray);


while ($row=mysqli_fetch_assoc($result)) {
         //echo "hello";
        if($row['month'] == 'August'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'September'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'October'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'November'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'December'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'January'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            //$sum
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        }else if($row['month'] == 'February'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
            array_push($maleArray, $row['number']);
            //$sum
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        } else if($row['month'] == 'March'){
          if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
          if (in_array("March", $monthArray)) {
                
          } else{
             array_push($monthArray, $row['month']);
          }
            if($row['gender'] == 'Male'){
              if (in_array($row['month'], $monthArray)) {
                //echo "Got Irix";
          } else{
             array_push($monthArray, $row['month']);
          }
            array_push($maleArray, $row['number']);
            } else if($row['gender'] == 'Female'){
            array_push($femaleArray, $row['number']);
            } else if($row['gender'] == 'Others'){
            array_push($otherArray, $row['number']);
            }
          
        }  
           
}  
   while ($row1=mysqli_fetch_assoc($result1)) {
    array_push($totalArray, $row1['number']);
   }     
//$maleArray = array($feb, $mar, $apr, $may, $jun, $jul, $aug, $sep,$oct,$nov,$dec);
 array_push($finalArray,$monthArray);
 array_push($finalArray,$maleArray);
  array_push($finalArray,$femaleArray);
   array_push($finalArray,$otherArray);
   array_push($finalArray,$totalArray);

echo json_encode($finalArray);
