Page 1 sur 2
Probleme d'affichage php/SQL
Posté : 20 mai 2016, 07:16
par team94
Bonjour j'essaye de développer pour mon usage perso au taf sur un script de congés, absences.
Ça avance plutôt bien.
mon script affiche un calendrier avec les agents sur la droite pour chaque absences on met la case d'une couleur avec l'a valeur contenu en base de donnée.
tous cela fonctionne bien sauf que si je met par exemple agent 1 en congés du 02/05 au 05/05
et agent 1 en congés maladie du 10/05/au 11/05 il va me l'afficher sur deux ligne moi je voudrais que ce soit sur la même ligne
j'ai trois tables en bdd
une 'agents'
--id
--nom
--prenom
--secteur
une 'conges'
-- id
--nom_conges
--abrev_conges
--nbre_conges
--rest
une 'absences'
--id
--id_agents
--id_conges
--date_debut
-- date_fin
mon code :
<table class="table">
<tbody>
<tr>
<td colspan=2 style="background-color:#F6E497;">
Agents
</td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d)); ?>
<?php $day=date($d);
/*echo $day;*/
?>
<td <?php
if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent) { echo 'style="background-color:#FE9000;"';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"';}
else {echo 'style="background-color:#efecca;"';}
?>>
<?=strftime('%a', strtotime($d));?><div><?=date('d', strtotime($d));?></div>
</td>
<?php endforeach; ?>
</tr>
<?php
// On récupère tout le contenu de la table agents
$abs = $bdd->query('SELECT * FROM agents AS ag
left JOIN absences AS abs
ON abs.id_agents = ag.id
left JOIN conges AS cg
ON abs.id_conges = cg.id
ORDER BY nom');
// On affiche chaque entrée une à une
?>
<tr>
<?php while ($donnees = $abs->fetch())
{ ?>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $donnees['nom']; ?></td>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $donnees['prenom']; ?></td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d));
$days=date(strtotime($d));
$day=date($d);
$date_deb = strtotime($donnees['date_debut']);
$date_fin = strtotime($donnees['date_fin']);
?>
<td <?php
if ($date_deb<=$days && $days <=$date_fin && $donnees['abrev_conges'] == 'CA' ) { echo 'style="background-color:#ff2000;"><div>'.$donnees['abrev_conges'].'</div></td>';}
else if ($date_deb<=$days && $days <=$date_fin && $donnees['abrev_conges'] == 'CM' ) { echo 'style="background-color:#9358C6;"><div>'.$donnees['abrev_conges'].'</div></td>';}
else if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent){ echo 'style="background-color:#FE9000;"><div></div></td>';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"><div></div></td>';}
else {echo 'style="background-color:#efecca;"><div></div></td>';}?>
<?php endforeach; ?>
</tr>
<?php
}
$abs->closeCursor(); // Termine le traitement de la requête
?>
</tbody>
</table>
cela m'affiche ça :
je voudrais que les CM et CA de NOM 1 soit sur la même ligne je ne sais pas si c'est un problème dans la conception de mes tables ou dans l'affichage de la page.
si quelqu'un peut me filer un coup de main je lui en serais reconnaissant.
Merci de passer du temps à nous orienter aider et corriger.
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 08:46
par Spols
Je te dirai bien de grouper dans ta requète par nom, mais j'ai peur que si un agent a plusieurs zone de congé dans le mois, une seule ne s'affiche
Alors je te proposerai plutot de modifier ton affichage
Lors du parcours des résultats, tu détecte que le nom de l'agent est différent ou non de l'itération précédente.
si le nom est le même, tu complète un tableau de date avec les congé/absence si le nom est différents ou fin des résultats, tu affiche la ligne à l'aide du tableau
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 08:55
par team94
bonjour et merci de ta réponse,
effectivement en regroupant par nom cela n'affiche qu'une seule ligne.
par contre je vois bien ce qu'il faut faire avec tes explication mais le transcoder en php étant autodidacte et faisant ça pour le plaisir je vais avoir besoins d'un petit peu plus d'aide.
avant de poster j'ai fait plusieurs recherche ça fait deux jours que je suis dessus j'ai le cerveau qui bouillonne
merci beaucoup.
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 09:32
par Spols
Je garanti rien sur le code, car je ne sais pas le tester
$abs = $bdd->query('SELECT * FROM agents AS ag
left JOIN absences AS abs
ON abs.id_agents = ag.id
left JOIN conges AS cg
ON abs.id_conges = cg.id
ORDER BY nom');
// On affiche chaque entrée une à une
while ($donnees = $abs->fetch())
{
$name = $donnees['nom'].$donnees['prenom']; //Je concatène les deux, J'assume qu'il n'y aura pas 2 personnes ayant le même couple nom/prenom
$temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
$temp_conge[$name]['conge'][] = array( 'date_debut' => $donnees['date_debut'], 'date_fin' => $donnees['date_fin'], 'abrev_conges' => $donnees['abrev_conges']);
}
$abs->closeCursor(); // Termine le traitement de la requête
foreach($temp_conge as $conges) {
?><tr>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['nom']; ?></td>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['prenom']; ?></td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d));
$days=date(strtotime($d));
$day=date($d);
foreach($conges['conge'] as $conge) {
$date_deb = strtotime($conge['date_debut']);
$date_fin = strtotime($conge['date_fin']);
?>
<td <?php
if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CA' ) { echo 'style="background-color:#ff2000;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CM' ) { echo 'style="background-color:#9358C6;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent){ echo 'style="background-color:#FE9000;"><div></div></td>';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"><div></div></td>';}
else {echo 'style="background-color:#efecca;"><div></div></td>';}?>
}
<?php endforeach; ?>
}
Finalement j'ai changé de proposition, je récupère les données par paquet, et je réitère pour l'affichage.
Mais au point de vue performance, la triple boucle imbriqué n'est peut être pas le plus indiqué
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 09:37
par team94
merci beaucoup je vais tenter de comprendre le code je test ça ce soir et je reviens vous tenir au courant
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 17:26
par team94
en modifiant le code j'obtient bien deux lignes nom1 prenom 1 et nom2 prenom2 mais les CA de nom1 n'apparaissent pas alors que les CM oui. cependant le code ne fonctionnait pas il manquait une accolade peut etre l'ai je mise au mauvbais endroit je reposte le code j'ai mis l'accolade apres la boucle foreach sur les congés.
<table class="table">
<tbody>
<tr>
<td colspan=2 style="background-color:#F6E497;">
Agents
</td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d)); ?>
<?php $day=date($d);
/*echo $day;*/
?>
<td <?php
if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent) { echo 'style="background-color:#FE9000;"';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"';}
else {echo 'style="background-color:#efecca;"';}
?>>
<?=strftime('%a', strtotime($d));?><div><?=date('d', strtotime($d));?></div>
</td>
<?php endforeach; ?>
</tr>
<?php
// On récupère tout le contenu de la table agents
$abs = $bdd->query('SELECT * FROM agents AS ag
left JOIN absences AS abs
ON abs.id_agents = ag.id
left JOIN conges AS cg
ON abs.id_conges = cg.id
ORDER BY nom');
// On affiche chaque entrée une à une
?>
<?php while ($donnees = $abs->fetch())
{
$name = $donnees['nom'].$donnees['prenom']; //Je concatène les deux, J'assume qu'il n'y aura pas 2 personnes ayant le même couple nom/prenom
$temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
$temp_conge[$name]['conge'][] = array( 'date_debut' => $donnees['date_debut'], 'date_fin' => $donnees['date_fin'], 'abrev_conges' => $donnees['abrev_conges']);
}
$abs->closeCursor(); // Termine le traitement de la requête
foreach($temp_conge as $conges) {
?>
<tr>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['nom']; ?></td>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['prenom']; ?></td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d));
$days=date(strtotime($d));
$day=date($d);
foreach($conges['conge'] as $conge) {
$date_deb = strtotime($conge['date_debut']);
$date_fin = strtotime($conge['date_fin']);
}// j'ai rajouté cette accolade sinon ça ne passait pas
?>
<td <?php
if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CA' ) { echo 'style="background-color:#ff2000;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CM' ) { echo 'style="background-color:#9358C6;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent){ echo 'style="background-color:#FE9000;"><div></div></td>';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"><div></div></td>';}
else {echo 'style="background-color:#efecca;"><div></div></td>';} ?>
<?php endforeach; } ?>
</tr>
</tbody>
</table>
merci encore de me filer un coup de main
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 17:51
par team94
lorsque je fais print_r($conges); il m'affiche ca on dirait qu'il ne boucle pas les conges il voit les CM mais pas les CA.
Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-04 [date_fin] => 2016-05-06 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => [date_fin] => [abrev_conges] => ) ) )
Re: Probleme d'affichage php/SQL
Posté : 20 mai 2016, 23:31
par Spols
déplace l'accolade rajoutée juste avant ton end foreach plutot, sinon tu n'aura que les derniers conges d'affiché.
Je sais pas quand tu fait ton print_r, mais cela a de l'importance
Re: Probleme d'affichage php/SQL
Posté : 21 mai 2016, 09:42
par team94
Merci encore spols de t’attarder sur le sujet j'ai déplacé l'accolade comme tu me l'a dit rien y fait j'ai également mis le print_r à la suite de l'accolade je joint les résultats.
sur le tableau il m'affiche effectivement les derniers rentré:
resultat du print_r(conges);
Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 1 [prenom] => prenom 1 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-11 [date_fin] => 2016-05-31 [abrev_conges] => CA ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) ) Array ( [nom] => nom 2 [prenom] => prenom 2 [conge] => Array ( [0] => Array ( [date_debut] => 2016-05-17 [date_fin] => 2016-05-21 [abrev_conges] => CM ) ) )
je reposte le code modifié :
<table class="table">
<tbody>
<tr>
<td colspan=2 style="background-color:#F6E497;">
Agents
</td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d)); ?>
<?php $day=date($d);
/*echo $day;*/
?>
<td <?php
if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent) { echo 'style="background-color:#FE9000;"';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"';}
else {echo 'style="background-color:#efecca;"';}
?>>
<?=strftime('%a', strtotime($d));?><div><?=date('d', strtotime($d));?></div>
</td>
<?php endforeach; ?>
</tr>
<?php
// On récupère tout le contenu de la table agents
$abs = $bdd->query('SELECT * FROM agents AS ag
left JOIN absences AS abs
ON abs.id_agents = ag.id
left JOIN conges AS cg
ON abs.id_conges = cg.id
ORDER BY nom');
// On affiche chaque entrée une à une
?>
<?php while ($donnees = $abs->fetch())
{
$name = $donnees['nom'].$donnees['prenom']; //Je concatène les deux, J'assume qu'il n'y aura pas 2 personnes ayant le même couple nom/prenom
$temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
$temp_conge[$name]['conge'][] = array( 'date_debut' => $donnees['date_debut'], 'date_fin' => $donnees['date_fin'], 'abrev_conges' => $donnees['abrev_conges']);
}
$abs->closeCursor(); // Termine le traitement de la requête
foreach($temp_conge as $conges) {
?>
<tr>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['nom']; ?></td>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['prenom']; ?></td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d));
$days=date(strtotime($d));
$day=date($d);
foreach($conges['conge'] as $conge) {
$date_deb = strtotime($conge['date_debut']);
$date_fin = strtotime($conge['date_fin']);
?>
<td <?php
if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CA' ) { echo 'style="background-color:#ff2000;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CM' ) { echo 'style="background-color:#9358C6;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent){ echo 'style="background-color:#FE9000;"><div></div></td>';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"><div></div></td>';}
else {echo 'style="background-color:#efecca;"><div></div></td>';} ?>
<?php } print_r($conges); /* accolade déplacé*/ ?>
<?php endforeach; ?>
<?php } ?>
</tr>
</tbody>
</table>
Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 08:29
par Spols
Je pense avoir trouvé le problème (à supposer qu'il n'y en avait qu'un
$temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
à chaque itération de lecture des résultat de la base de donnée, cette instruction écrase les congé précédents
en ajoutant une condition, ca devrait être mieux.
if (empty($temp_conge[$name])) $temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
Pour info et pour te permettre d'évoluer, voici mon schéma de pensée.
Ton print_r affiche toujours le même tableau de conges par personnes ne comprenant toujours qu'un seul congé. Il y a donc un problème lors du remplissage de ce tableau et nonlors de son affichage. Je relis donc la partie remplissage et comprenant bien chaque instruction et en parcourant mentalement les instructions sur plusieurs itérations. Je m'aperçois qu'il y a écrasement plutot que complètion.
Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 09:27
par team94
bonjour spols, en faisant la modifs cela me fait un truc bizarre au niveau de l'affichage
le code :
<?php while ($donnees = $abs->fetch())
{
$name = $donnees['nom'].$donnees['prenom']; //Je concatène les deux, J'assume qu'il n'y aura pas 2 personnes ayant le même couple nom/prenom
if (empty($temp_conge[$name]))$temp_conge[$name] = array('nom' => $donnees['nom'], 'prenom' => $donnees['prenom']);
$temp_conge[$name]['conge'][] = array( 'date_debut' => $donnees['date_debut'], 'date_fin' => $donnees['date_fin'], 'abrev_conges' => $donnees['abrev_conges']);
}
$abs->closeCursor(); // Termine le traitement de la requête
foreach($temp_conge as $conges) {
?>
<tr>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['nom']; ?></td>
<td style="width:100px; vertical-align:middle; background-color:#F6E497;"><?php echo $conges['prenom']; ?></td>
<?php foreach($datesMonth as $d): ?>
<?php $w=date('w', strtotime($d));
$days=date(strtotime($d));
$day=date($d);
$date_deb = strtotime($conge['date_debut']);
$date_fin = strtotime($conge['date_fin']);
foreach($conges['conge'] as $conge) {
?>
<td <?php
if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CA' ) { echo 'style="background-color:#ff2000;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($date_deb<=$days && $days <=$date_fin && $conge['abrev_conges'] == 'CM' ) { echo 'style="background-color:#9358C6;"><div>'.$conge['abrev_conges'].'</div></td>';}
else if ($day==$vic_allie OR $day==$an OR $day==$prem OR $day==$fet_nation OR $day==$assomp OR $day==$touss OR $day==$arm OR $day==$no OR $day==$paq OR $day==$asc OR $day==$pent){ echo 'style="background-color:#FE9000;"><div></div></td>';}
else if ($w==0 OR $w==6) { echo 'style="background-color:#ccc;"><div></div></td>';}
else {echo 'style="background-color:#efecca;"><div></div></td>';} ?>
<?php } /** accolade déplacé */?>
<?php endforeach; print_r($conge); ?>
<?php } ?>
</tr>
</tbody>
</table>
l'affichage:

Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 09:31
par team94
et merci d'expliquer le raisonnement ce n'est pas évident quand on débute
Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 13:43
par Spols
Ok je vois,
le script crée trop de case dans ton tableau
cela est du au fait que pour chaque jour, il teste toutes les périodes de vacances. Si il y a 2 périodes, il va afficher 2 cases.
Il faut donc modifier le flux, pour que le foreach (le dernier, le plus profond) s'arête quand la date est entre les bornes d'un congé. Il faudra surement aussi voir pourquoi il affiche CM et CA alors qu'il ne devrait en afficher qu'un.
Il faut revoir les boucles entièrement, je n'ai pas le temps pour l'instant, mais si j'ai un peu de temps cet aprem, je penserai à ton problème
Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 14:49
par team94
Pas de souci et je te remercie vraiment de ne pas me lâcher ( ce qui serait compréhensible vu mon niveau

)
je vais essayer de faire des tests si j'avance je post
merci encore
bonne journée.
Re: Probleme d'affichage php/SQL
Posté : 23 mai 2016, 16:42
par Spols
En gros, voici ce que je te proposerai de faire
Pour chaque persone, pour chaque jour du mois, check si il est présent dans une des périodes du tableau sinon, check si jour férié ou WE sinon affichage par défaut
le plus simple est peut être une variable à laquelle tu donnes la valeur par défaut (jour normal) en début de boucle,
tu check si jour férie si oui tu réécris la variable (jour férié)
tu check si WE => si oui tu réécris la variable
tu check pour chacune des période de vacance si oui tu réécris la variable
en enfin juste avant l'accolade du foreach des jours du mois tu gère l'aéffichage en fonction de la valeur de ta variable.
Garde bien tous l'affichage au même endroit tu auras plus facile à maintenir / débuggé ton code.