Imbriquer foreach et while

Résolu/Fermé
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024 - 9 juin 2022 à 07:38
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024 - 11 juin 2022 à 07:36
Bonjour

J'ai un ma requette qui me donne ce résultat
ANNEE PAYS QU
2022 Argentina 1670
2022 Japón 1499
2022 Gabón 1463
2022 Argelia 1380
2022 Jamaica 1162
2022 Haití 99
2022 Ciudad del Vaticano 30
2022 Albania 22
2022 Laos 11
2021 ES 3000
2021 FR 2800
2021 FRANCE 222
2021 Tailandia 99
2021 NULL 55
2021 Haití 55
2021 Japón 9
2021 Argelia 5
2021 0 0
2020 Japón 453

ETt je veux extraire les 5 premieres lignes de chaque dates dans un tableau de cette manière.

PAIS 1 QU 1 PAIS 2 QU 2 PAIS 3 QU 3 PAIS 4 QU 4 PAIS 5 QU 5
2022 ARGENTINA 1670 JAPON 1499 Gabón 1463 Argelia 1380 Jamaica 1162
2021 ES 3000 FR 2800 FRANCE 222 Taillandia 99 Null 55
2020 japon 453 THAIL 99 JAMAICA 55

]


Voic mon code
error_reporting(-1);
$sqlQuery_pais = 'SELECT RESUME.D AS ANNEE, RESUME.COUNTRY AS PAIS, SUM(RESUME.QU) AS TOTAL  FROM (
							SELECT YEAR(DATE) AS D, PAIS_1 AS COUNTRY,NB_1 AS QU FROM camino
							UNION
							SELECT YEAR(DATE) AS D,PAIS_2 AS COUNTRY,NB_2 AS QU FROM camino
							UNION
							SELECT YEAR(DATE) AS D,PAIS_3 AS COUNTRY,NB_3  AS QU FROM camino ) AS RESUME
							GROUP BY ANNEE, PAIS 
							ORDER BY  ANNEE DESC, TOTAL DESC';
			$recipesStatement_pais = $db->prepare($sqlQuery_pais);
			$recipesStatement_pais->execute();
			$recipes_pais = $recipesStatement_pais->fetchAll();
			
			?>
			 <p id="pais" class="mes">NB PEREGRINOS POR PAIS</p><br>
			 <table class="meteo">
						<tr class="meteo" > 
							<th class="meteo">ANNEE</th>
							<th class="meteo">PAIS 1</th>
							<th class="meteo">TOTAL 1</th>
							<th class="meteo">PAIS 2</th>
							<th class="meteo">TOTAL 2</th>
						</tr>
			<?php
			// On affiche chaque donnée une à une
			foreach ($recipes_pais as $recipe_pais) {
			?>
			   
						<tr class="meteo">
							<td class="meteo"><?php echo $recipe_pais['ANNEE']; ?></td>  // ICI OK BIEN AFFICHÉ
				
							<td class="meteo"><?php echo $recipe_pais['PAIS'];?></td>  // ICI OK BIEN AFFICHÉ
							<td class="meteo"><?php echo $recipe_pais['TOTAL'];?></td>		// ICI OK BIEN AFFICHÉ
							<td class="meteo"><?php echo $recipe_pais['PAIS[1]'];?></td>  // ICI ERROR Notice: Undefined index: PAIS[1]
							<td class="meteo"><?php echo $recipe_pais['TOTAL[1]'];?></td>			 // ICI ERROR Notice: Undefined index: PAIS[1					
				
			}		
						</tr>
			<?php
			};
			?>
			</table><br>



J'ai pensé faire une boucle dans le foreach
<?php
			// On affiche chaque donnée une à une
			foreach ($recipes_pais as $recipe_pais) {
			?>
			   
						<tr class="meteo">
							<td class="meteo"><?php echo $recipe_pais['ANNEE']; ?></td>
				<?php
				$z = 0;
				while ($z < 5) {
				?>
							<td class="meteo"><?php echo $recipe_pais['PAIS'];?></td>
							<td class="meteo"><?php echo $recipe_pais['TOTAL'];?></td>				
			<?php				
			$z++;	
			}		
						</tr>
			<?php
			};
			?>
			</table><br>


mais je n'ai pas de résultat qui s'affiche et ma page cherche sans s'arreter les résultats.

Pouvez-vous m'aider? Merci.

6 réponses

yg_be Messages postés 22920 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 4 juillet 2024 1 481
9 juin 2022 à 08:26
bonjour,
peux-tu donner suite, ou marquer comme résolues, les autres discussions que tu laisses ouvertes?
0
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024
9 juin 2022 à 20:54
Vu que je n'arrive pas au bon résultat avec les requettes, j'essai de la faire en php.
$an = "2022";
						
			$sqlQuery_pais1 = 'SELECT ANNEE, PAYS, TOTAL FROM resultat WHERE ANNEE ='.$an.' LIMIT 1';
			$recipesStatement_pais1 = $db->prepare($sqlQuery_pais1);
			$recipesStatement_pais1->execute();
			$recipes_pais1 = $recipesStatement_pais1->fetchAll();
			
			$sqlQuery_pais2 = 'SELECT ANNEE AS ANNEE2, PAYS AS PAYS2, TOTAL AS TOTAL2 FROM resultat WHERE ANNEE ='.$an.'  LIMIT 2,1';
			$recipesStatement_pais2 = $db->prepare($sqlQuery_pais2);
			$recipesStatement_pais2->execute();
			$recipes_pais2 = $recipesStatement_pais2->fetchAll();
			
			?>
			 <p id="pais" class="mes">NB PEREGRINOS POR PAIS</p><br>
			 <table class="meteo">
						<tr class="meteo" > 
							<th class="meteo">ANNEE</th>
							<th class="meteo">PAIS 1</th>
							<th class="meteo">TOTAL 1</th>
							<th class="meteo">PAIS 2</th>
							<th class="meteo">TOTAL 2</th>
						</tr>
						<tr class="meteo">
			<?php
			// On affiche chaque donnée une à une
			foreach ($recipes_pais1 as $recipe_pais1) {
			?>
				<td class="meteo"><?php echo $recipe_pais1['ANNEE']; ?></td>
				<td class="meteo"><?php echo $recipe_pais1['PAYS'];?></td>
				<td class="meteo"><?php echo $recipe_pais1['TOTAL'];?></td>
			//QUAND JE MET CETTE PARTIE LA REQUETTE N'ABOUTIE PAS ET CONTINUE DE CHERCHER LES DONNEES. LA REQUETEASSOCIE  EN SQL FONCTIONNE BIEN ET DONNE LE RESULTAT ESPÉRÉ
<?php
			};	
			foreach ($recipes_pais2 as $recipe_pais2) {
				<td class="meteo"><?php echo $recipe_pais2['PAYS2'];?></td>
				<td class="meteo"><?php echo $recipe_pais2['TOTAL2'];?></td>				
			};
			?>	
// FIN DE LA PARTIE
						</tr>
			<?php
			};
			?>
			</table><br>


pouvez-vous me dire ou est mon erreur? Merci.
0
yg_be Messages postés 22920 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 4 juillet 2024 1 481
9 juin 2022 à 20:57
Qu'as-tu ajouté à ton code afin de comprendre comment il se comportait?
0
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024
10 juin 2022 à 09:14
Erreur corrigé: une } mal placée.

J'ai réssui a obtenir le passage de une colonne a ligne. je m'explique

annee pays total
2022 france 1500
2022 espagne 1325
2022 italie 1000

vers 2022 france 1500 espagne 1325 italie 1000

Maintenant je n'ai plus qu'a le faire pour toutes les années.

<p id="pais" class="mes">NB PEREGRINOS POR PAIS</p><br>
			 <table class="meteo">
						<tr class="meteo" > 
							<th class="meteo">ANNEE</th>
							<th class="meteo">PAIS 1</th>
							<th class="meteo">TOTAL</th>
							
						</tr>
						<tr class="meteo">
			<?php
			//Mes variables pour la boucle des année
            $an_debut = "2004"; //premiere annee stat
			$an_hoy = date('Y'); // annee courante
			for ($i = $an_debut; $i <= $an_hoy; $i++)
			{
			$z = array($i);
			
			foreach ($z as $an)
				{
				$sqlQuery_annee = 'SELECT ANNEE FROM resultat WHERE ANNEE ='.$an.' LIMIT 1';
				$recipesStatement_annee = $db->prepare($sqlQuery_annee);
				$recipesStatement_annee->execute();
				$recipes_annee = $recipesStatement_annee->fetchAll();
				foreach ($recipes_annee as $recipe_annee) 
					{
					?>
					<td class="meteo"><?php echo $recipe_annee['ANNEE']; ?></td>
					<?php
					}
				$sqlQuery_pais1 = 'SELECT ANNEE, PAYS, TOTAL FROM resultat WHERE ANNEE ='.$an.' LIMIT 1';
				$recipesStatement_pais1 = $db->prepare($sqlQuery_pais1);
				$recipesStatement_pais1->execute();
				$recipes_pais1 = $recipesStatement_pais1->fetchAll();
				foreach ($recipes_pais1 as $recipe_pais1) 
					{
					?>
						
						<td class="meteo"><?php echo $recipe_pais1['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais1['TOTAL'];?></td>
					<?php
					};	
				
				
				};
			};
			?>	
						</tr>
			
			</table><br>
			

Le resulta n'est pas correct.

Affichage:
anne pays total anne pays total
2004 0 0 2005 0 0 2006 0 0 ect

resultat attendu
2022 france 1500 espagne 1325 italie 1000
2021 espagne 1325 italie 1000 france 950
2020 france 500 espagne 325 italie 250

Ou est mon erreur dans la boucle?
0
yg_be Messages postés 22920 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 4 juillet 2024 1 481
10 juin 2022 à 11:05
je pense que tu peux remplacer
			for ($i = $an_debut; $i <= $an_hoy; $i++)
			{
			$z = array($i);
			
			foreach ($z as $an)

par
			for ($an = $an_debut; $an <= $an_hoy; $an++)
0
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024
10 juin 2022 à 14:45
J'étais justement en train de el tester et ca fonctionne enfinnnnnnnnnnn...

mon resultat est:


Ou et comment je doit mettre une condition pour n'afficher qu'une seule fois chaque date?
0
yg_be Messages postés 22920 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 4 juillet 2024 1 481
10 juin 2022 à 14:55
Si tu as modifié ton code, il est utile de nous montrer ton nouveau code.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024
10 juin 2022 à 16:04
Oups le voici

<p id="pais" class="mes">NB PEREGRINOS POR PAIS</p><br>
			 <table class="meteo">
						<tr class="meteo" > 
							<th class="meteo">ANNEE</th>
							<th class="meteo">PAIS 1</th>
							<th class="meteo">TOTAL</th>
							
						</tr>
						
			<?php
			$an_debut = "2004"; //premiere annee stat
			$an_hoy = date('Y'); // anne courante
			for ($an = $an_debut; $an <= $an_hoy; $an++)
				{
					
					$sqlQuery_annee = 'SELECT ANNEE FROM resultat WHERE ANNEE ='.$an.' ';
					$recipesStatement_annee = $db->prepare($sqlQuery_annee);
					$recipesStatement_annee->execute();
					$recipes_annee = $recipesStatement_annee->fetchAll();
					foreach($recipes_annee as $recipe_annee) 
					{
					?>
					<tr class="meteo">
					<td class="meteo"><?php echo $recipe_annee['ANNEE']; ?></td>
					<?php
					};
					$sqlQuery_pais1 = 'SELECT ANNEE, PAYS, TOTAL FROM resultat WHERE ANNEE ='.$an.' LIMIT 1';
					$recipesStatement_pais1 = $db->prepare($sqlQuery_pais1);
					$recipesStatement_pais1->execute();
					$recipes_pais1 = $recipesStatement_pais1->fetchAll();
					foreach ($recipes_pais1 as $recipe_pais1) 
					{
					?>
						
						<td class="meteo"><?php echo $recipe_pais1['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais1['TOTAL'];?></td>
					<?php
					};	
					$sqlQuery_pais2 = 'SELECT ANNEE AS ANNEE2, PAYS , TOTAL  FROM resultat WHERE ANNEE ='.$an.'  LIMIT 1,1';
					$recipesStatement_pais2 = $db->prepare($sqlQuery_pais2);
					$recipesStatement_pais2->execute();
					$recipes_pais2 = $recipesStatement_pais2->fetchAll();
					foreach ($recipes_pais2 as $recipe_pais2) 
					{
					?>
						<td class="meteo"><?php echo $recipe_pais2['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais2['TOTAL'];?></td>				
					<?php
					};
					
					$sqlQuery_pais3 = 'SELECT ANNEE AS ANNEE2, PAYS , TOTAL  FROM resultat WHERE ANNEE ='.$an.'  LIMIT 2,1';
					$recipesStatement_pais3 = $db->prepare($sqlQuery_pais3);
					$recipesStatement_pais3->execute();
					$recipes_pais3 = $recipesStatement_pais3->fetchAll();
					foreach ($recipes_pais3 as $recipe_pais3) 
					{
					?>
						<td class="meteo"><?php echo $recipe_pais3['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais3['TOTAL'];?></td>				
					<?php
					};
					
					$sqlQuery_pais4 = 'SELECT ANNEE AS ANNEE2, PAYS , TOTAL  FROM resultat WHERE ANNEE ='.$an.'  LIMIT 3,1';
					$recipesStatement_pais4 = $db->prepare($sqlQuery_pais4);
					$recipesStatement_pais4->execute();
					$recipes_pais4 = $recipesStatement_pais4->fetchAll();
					foreach ($recipes_pais4 as $recipe_pais4) 
					{
					?>
						<td class="meteo"><?php echo $recipe_pais4['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais4['TOTAL'];?></td>				
					<?php
					};
					
					$sqlQuery_pais5 = 'SELECT ANNEE AS ANNEE2, PAYS , TOTAL  FROM resultat WHERE ANNEE ='.$an.'  LIMIT 4,1';
					$recipesStatement_pais5 = $db->prepare($sqlQuery_pais5);
					$recipesStatement_pais5->execute();
					$recipes_pais5 = $recipesStatement_pais5->fetchAll();
					foreach ($recipes_pais5 as $recipe_pais5) 
					{
					?>
						<td class="meteo"><?php echo $recipe_pais5['PAYS'];?></td>
						<td class="meteo"><?php echo $recipe_pais5['TOTAL'];?></td>				
					<?php
					};
				
				};
			
			?>	
						</tr>
			
			</table><br>

0
yg_be Messages postés 22920 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 4 juillet 2024 1 481
10 juin 2022 à 18:58
Pourquoi as-tu supprimé le
LIMIT 1
à la ligne 16?
0
mont_dani Messages postés 232 Date d'inscription mardi 6 juin 2017 Statut Membre Dernière intervention 7 mai 2024
11 juin 2022 à 07:36
BRAVO yg_be pour ta patience et pour me guider vers la solution. J'ai appris beaucoup avec les piste que tu m'a donné et avec toutes les recherches faites sur le net.

Pour le fun voici le resultat.


MERCI et je clos ce post.
0