<?php
echo '<table>';
$creneau = $creneau_info['hor_name'];
$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces ORDER BY espace_nom");
$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
$inscrits_select_esp = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.cas, freq.h_inscription, freq.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
	FROM (
	    SELECT * 
	    FROM ".$prefix."_freq 
	    WHERE date_freq = '".$day."' AND hor_name = '".$ccren_cren."' AND cas NOT IN ('sor', 'exc') AND espace_id = 0
	) freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	LEFT JOIN ".$prefix."_espaces espaces
	ON espaces.espace_id = freq.espace_id
	".$filtre_consult."
	".$ordre."";
	$inscrits_esp = mysqli_query($bdd,$inscrits_select_esp);
$nb_inscrits_esp = mysqli_num_rows($inscrits_esp);
if($nb_inscrits_esp>0) {
	$crennum = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT quota_freq FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'"));
	if($freq_gestion_jourscren['valeur']==0) {
		$quota_freq = @intval(substr($crennum['quota_freq'],0,3));
	}
	else {
		$quota_freq = @intval(substr($crennum['quota_freq'], strpos($crennum['quota_freq'], date('D')) + strlen(date('D')), 3));
	}
	if($quota_freq==0) {
		$vibs_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'"));
	}
	else {
		$vibs_info['valeur'] = $quota_freq;
	}
	$vibsr_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'"));
	echo '<tr id="tresp0"><td><b>'.$esp0['valeur'].'</b></td><td><span id="nb_tresp0">'.$nb_inscrits_esp.'</span> '.FREQ_INF10.'</td><td>';
	if($vibs_info['valeur']==0) { echo '('.FREQ_INF11.')'; }
	else { if($vibsr_info['valeur']=='stopp') { echo '<span class="signal">'; }
		echo ON.' '.$vibs_info['valeur'].' '.PLACES.'</td></tr>'; 
		if($vibsr_info['valeur']=='stopp') { echo '</span>'; }
	}
}
foreach($espaces as $espace) {
	$inscrits_select_esp = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.cas, freq.h_inscription, freq.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
	FROM (
	    SELECT * 
	    FROM ".$prefix."_freq 
	    WHERE date_freq = '".$day."' AND hor_name = '".$creneau."' AND cas NOT IN ('sor', 'exc') AND espace_id = '".$espace['espace_id']."'
	) freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	LEFT JOIN ".$prefix."_espaces espaces
	ON espaces.espace_id = freq.espace_id
	".$filtre_consult."
	".$ordre."";
	$inscrits_esp = mysqli_query($bdd,$inscrits_select_esp);
	$nb_inscrits_esp = mysqli_num_rows($inscrits_esp);
	if($nb_inscrits_esp>0) {
		$vibs_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilnbi FROM ".$prefix."_espaces WHERE espace_id = '".$espace['espace_id']."'"));
		$vibsr_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilreaci FROM ".$prefix."_espaces WHERE espace_id = '".$espace['espace_id']."'"));
		echo '<tr id="tresp'.$espace['espace_id'].'"><td><b>'.$espace['espace_nom'].'</b></td><td><span id="nb_tresp'.$espace['espace_id'].'">'.$nb_inscrits_esp.'</span> '.FREQ_INF10.'</td><td>';
		if($vibs_info['seuilnbi']==0) { echo '('.FREQ_INF11.')'; }
		else { if($vibsr_info['seuilreaci']=='stopp') { echo '<span class="signal">'; }
			echo ON.' '.$vibs_info['seuilnbi'].' '.PLACES.'</td></tr>'; 
			if($vibsr_info['seuilreaci']=='stopp') { echo '</span>'; }
		}
	}
}
echo '</table><br>';
?>