<?php
include("bd/sql_variables.php");
$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");

$mes=4;
$anno=2018;
$idj=3;
$ids=5;
$idtp=1;

//$mes=$_GET['mes'];
//$anno=$_GET['anno'];
//$idj=$_GET['idj'];
//$ids=$_GET['ids'];
//$idtp=$_GET['idtp'];

//Pregunta por el area
$sqlgarea ="select distinct(ga_id) as idga from tb_horareprog2018 where tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mes' and j_id='$idj' and hr_idserva='$ids' order by a_id asc";
$resultga = mysql_query($sqlgarea);

while ($rowga= mysql_fetch_array($resultga)){
$idga=$rowga['idga'];

$sql = "select ga_desc as nombre from tb_garea where ga_id='$idga'";
$result = mysql_query($sql);
while ($row= mysql_fetch_array($result)){
echo "Area: ".$row['nombre']."</br> ";
}

$cadena = "<table>";
$cadenadia = "<tr>";
$cadenaM = "<tr>";
$cadenaT = "<tr>";
$cadenaN = "<tr>";

for($i=1;$i<32;$i++){

$cadenadia = $cadenadia."<td>".$i."</td>";

//personal de maņana
$sqlaskm="SELECT count(*) as nper FROM tb_horareprog2018 a inner join tb_horario b on a.h_id=b.h_id where b.h_horaturn in ('D','M') and hr_dia='$i' and a.uap_anno='$anno' and a.uap_mes='$mes' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id not in (select a_id from tb_area  where ga_id='$idga' and a_desc='CH')";
$resultservm = mysql_query($sqlaskm);
while ($rowsm= mysql_fetch_array($resultservm)){
$nperM=$rowsm['nper'];
$cadenaM = $cadenaM."<td>".$nperM."</td>";
}
//personas de tarde
$sqlaskt="SELECT count(*) as nper FROM tb_horareprog2018 a inner join tb_horario b on a.h_id=b.h_id where b.h_horaturn in ('D','T') and hr_dia='$i' and a.uap_anno='$anno' and a.uap_mes='$mes' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id not in (select a_id from tb_area  where ga_id='$idga' and a_desc='CH')";
$resultservt = mysql_query($sqlaskt);
while ($rowst= mysql_fetch_array($resultservt)){
$nperT=$rowst['nper'];
$cadenaT = $cadenaT."<td>".$nperT."</td>";
}

//personal de noche
$sqlask="SELECT count(*) as nper FROM tb_horareprog2018 a inner join tb_horario b on a.h_id=b.h_id where b.h_horaturn in ('N') and hr_dia='$i' and a.uap_anno='$anno' and a.uap_mes='$mes' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga'";
$resultserv = mysql_query($sqlask);
while ($rows= mysql_fetch_array($resultserv)){
$nperN=$rows['nper'];
$cadenaN = $cadenaN."<td>".$nperN."</td>";
}

}
$cadenadia = $cadenadia."</tr>";
$cadenaM = $cadenaM."</tr>";
$cadenaT = $cadenaT."</tr>";
$cadenaN = $cadenaN."</tr>";

$cadena = $cadena.$cadenadia.$cadenaM.$cadenaT.$cadenaN."</table>";

echo $cadena;


}

?>