Page 1 sur 1

Affichage résultat boucle foreach

Posté : 03 mars 2009, 20:10
par bunk
Bonsoir,

Je liste un fichier .ini afin d'afficher la section en titre et les options en dessous. tout fonctionne bien mais je voudrais que lorsque qu'aucune options n'est présente, la catégorie ne soit pas affichée.

Voila le bout de code :
<?php

					$tableauIni = parse_ini_file($fichier, TRUE);
					foreach($tableauIni as $section_titre=>$section){

						echo '<h4>';
							echo  htmlentities($section_titre);
						echo'</h4>';

						foreach($section as $key=>$val){
							$idchp = explode (".", $key);
							if($idchp[0] == 'A' && strpos($permgrp[0], $idchp[1]) === FALSE
							OR $idchp[0] == 'B' && strpos($permgrp[1], $idchp[1]) === FALSE
							OR $idchp[0] == 'C' && strpos($permgrp[2], $idchp[1]) === FALSE
							OR $idchp[0] == 'D' && strpos($permgrp[3], $idchp[1]) === FALSE
							OR $idchp[0] == 'E' && strpos($permgrp[4], $idchp[1]) === FALSE
							OR $idchp[0] == 'F' && strpos($permgrp[5], $idchp[1]) === FALSE
							OR $idchp[0] == 'G' && strpos($permgrp[6], $idchp[1]) === FALSE
							OR $idchp[0] == 'H' && strpos($permgrp[7], $idchp[1]) === FALSE
							OR $idchp[0] == 'I' && strpos($permgrp[8], $idchp[1]) === FALSE
							OR $idchp[0] == 'J' && strpos($permgrp[9], $idchp[1]) === FALSE
							OR $idchp[0] == 'K' && strpos($permgrp[10], $idchp[1]) === FALSE
							OR $idchp[0] == 'L' && strpos($permgrp[11], $idchp[1]) === FALSE
							OR $idchp[0] == 'M' && strpos($permgrp[12], $idchp[1]) === FALSE
							OR $idchp[0] == 'N' && strpos($permgrp[13], $idchp[1]) === FALSE
							OR $idchp[0] == 'O' && strpos($permgrp[14], $idchp[1]) === FALSE
							) {
							}else{
								echo 'options';
							}
						}
					}
pour résumer par rapport à ce bout de code, si la boucle ne passe par echo 'options'; je ne voudrais pas afficher le titre (echo htmlentities($section_titre);) plus haut dans la boucle.

Merci de votre aide.

Posté : 03 mars 2009, 22:43
par Ryle
Au lieu de faire un echo directement de ton titre, stockes le dans une variable et fait de même pour ton "options". Tu peux ainsi afficher la variable contenant ton titre après avoir passé ta boucle foreach et ajouter une condition pour savoir si tu dois l'afficher ou non (tu peux utiliser un flag, ou simplement vérifier si ta seconde variable contient bien la valeur "options" ou non :))

Posté : 07 mars 2009, 16:48
par Invité
Bonjour,

Pourrais tu me donner un exemple, je me tire les cheveux.

Avant le premier foreach j'initialise 2 variables, $titre et $valeur
au lieu des echo je fais $titre .= "titre et pareil pour $valeur mais j'affiche toujours tout.

Merci pour ton aide

Posté : 07 mars 2009, 22:47
par Ryle
C'est sans doute parce qu'il te faut réinitialiser ta variable entre deux itérations pour que le test fonctionne :
                    foreach($tableauIni as $section_titre=>$section){ 

                        $str = ''; // à chaque itération on vide la variable

                        foreach($section as $key=>$val){ 
                            $idchp = explode (".", $key); 
                            if ($idchp[0] == 'A' && strpos($permgrp[0], $idchp[1]) === FALSE 
                              ...
                            ) { }
                            else { // dans le cas des options on renseigne la variable (on peut aussi utiliser un flag)
                                $str.= 'options'; 
                            } 
                        } 

                        if ($str != '') { // si la variable n'est pas vide ou le flag activé, on affiche le titre et les infos
                           echo '<h4>'; 
                           echo htmlentities($section_titre); 
                           echo '</h4>'; 

                           echo $str;
                        }

                    }