Imbriquer foreach et while
Résolu
mont_dani
Messages postés
232
Date d'inscription
Statut
Membre
Dernière intervention
-
mont_dani Messages postés 232 Date d'inscription Statut Membre Dernière intervention -
mont_dani Messages postés 232 Date d'inscription Statut Membre Dernière intervention -
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
J'ai pensé faire une boucle dans le foreach
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.
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.
A voir également:
- Imbriquer foreach et while
- Foreach mysql ✓ - Forum MySQL
- Application error: a client-side exception has occurred while loading www.leboncoin.fr (see the browser console for more information). - Forum Services en ligne
- While imbriqués - Forum Bases de données
- Error code 1309 mac while copying ✓ - Forum MacOS
- Accès impossible à mon compte Bon Coin ✓ - Forum Réseaux sociaux
6 réponses
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
Ambassadeur
1 584
bonjour,
peux-tu donner suite, ou marquer comme résolues, les autres discussions que tu laisses ouvertes?
peux-tu donner suite, ou marquer comme résolues, les autres discussions que tu laisses ouvertes?
Vu que je n'arrive pas au bon résultat avec les requettes, j'essai de la faire en php.
pouvez-vous me dire ou est mon erreur? Merci.
$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.
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.
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?
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?
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?
mon resultat est:

Ou et comment je doit mettre une condition pour n'afficher qu'une seule fois chaque date?
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
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>