<?php
$data = array();
require 'config.php';
$sql= "select sum(total) as total,sum(male) as male, sum(female) as female, sum(minors) as minors, sum(children_male) as children_male, sum(children_female) as children_female, sum(voluntaryreturns) as voluntaryreturns, sum(involuntaryreturns) as involuntaryreturns from ksa";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
if($total > 0){
while ($row = $stmt->fetchObject()) {
	
	$total=$row->total;
	$involuntaryreturns = $row->involuntaryreturns;
	$male=$row->male;
	$female=$row->female;
	$children_male=$row->children_male;
	$children_female=$row->children_female;
	$involuntarypercentage=round((($involuntaryreturns/$total)*100),0);
	$total_male=($male+$children_male);
	$total_female=($female+$children_female);
	$malepercentage=round((($total_male/$total)*100),0);
	$femalepercentage=round((($total_female/$total)*100),0);
	
	$data['status'] = 'ok';
	$data['result'] = array(
          "total" => $row->total,
          "male" => $total_male,
	      "female" => $total_female,
		  "minors" => $row->minors,
		  "voluntaryreturns" => $row->voluntaryreturns,
		  "involuntaryreturns" => $row->involuntaryreturns,
		  "involuntarypercentage" => $involuntarypercentage,
		  "malepercentage" =>$malepercentage,
		  "femalepercentage" =>$femalepercentage,
        );
}
}
else{
        $data['status'] = 'err';
        $data['result'] = '';
    }
	//returns data as JSON format
    echo json_encode($data);
 ?>