<?php
	include_once("db.php");

	//make result object
	$result = array();
	$result['ok'] = false;

	//get post data
	$postdata = file_get_contents("php://input");
	$post = json_decode($postdata, true);

	$idchampionship = $post['idchampionship'];

	//fetch the championships
	$poulefetch = pg_prepare($dbconn, "poulefetch", "select poule.*, (select count(*) from team_poule where tier = poule.tier and poule = poule.poule and idchampionship = poule.idchampionship) teamcount, (select count(*) from race where tier = poule.tier and poule = poule.poule and idchampionship = poule.idchampionship) racecount from poule where idchampionship = $1 order by tier, poule");
	$poulefetch = pg_execute($dbconn, "poulefetch", array($idchampionship));

	//build result object
	$poules = array();
	while($row = pg_fetch_assoc($poulefetch)) {
		$poules[] = $row;
	}

	$result['poules'] = $poules;

	//return data
	$result['ok'] = true;

	//print message
	$resultjson =  json_encode($result);

	echo $resultjson;
?>
