Précédent   Forum des professionnels en informatique > PHP > Langage > Syntaxe
Syntaxe Forum d'entraide sur la syntaxe de PHP et la POO. Avant de poster -> FAQ syntaxe, Cours d'initiation et cours de POO
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 29/09/2011, 16h27   #1
Nouveau Membre du Club
 
Femme
Inscription : janvier 2010
Messages : 78
Détails du profil
Informations personnelles :
Sexe : Femme

Informations forums :
Inscription : janvier 2010
Messages : 78
Points : 25
Points : 25
Par défaut Problème de boucle pour affichage mise en forme table

je récupère de ma sgbd des données que je voudrais afficher de façon différente.

le résultat SGBD est affiché comme ceci :

année Action NB
2011 CAB 3
2011 ECH 1
2011 IRR 8
2012 IRR 5

je voudrais un affichage :

Année CAB ECH IRR
2011 3 1 8
2012 5

Avec mon code ci-après, j'arrive à afficher le résultat comme suit :

Année CAB ECH IRR
2011 3
1
8
2012 5

Tout ne s'affiche pas sur la première ligne pour l'année 2011
Merci de vous pencher sur mon pb qui ne doit pas être compliqué pour les experts présents sur ce site.

voici mon code :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div>
	<table>
		<tr>
		<th id="" width="auto">Année</th><?php	
			for($i=0;$i<count($entete);$i++){ 		 ?>
		<th align="center" id="" width="auto"><?php echo $entete[$i]['actions_nature'];?></th><?php	
											    }      ?>
		</tr>
 
		<?php
 
		for($j=0;$j<count($stat1);$j++){
				?>
				<tr>
				<?php	
				?>
 
				<td align="center" id=""><?php if($stat1[$j]['Annee']!=$stat1[$j-1]['Annee']){echo $stat1[$j]['Annee'];}?></td> <?php	
				for($k=0;$k<count($entete);$k++){ ?>
				<td align="center" id=""><?php if($stat1[$j]['actions_nature']==$entete[$k]['actions_nature']){ echo $stat1[$j]['NB'];}?></td><?php	
													     } 
						?>
		</tr>
		<?php
		}   ?>
	</table>		
	</div>
naroco est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/09/2011, 17h07   #2
Modérateur
 
Avatar de Nesmontou
 
Homme Benjamin PREVOT
Architecte de système d'information
Inscription : septembre 2004
Messages : 1 568
Détails du profil
Informations personnelles :
Nom : Homme Benjamin PREVOT
Âge : 30
Localisation : France, Nord (Nord Pas de Calais)

Informations professionnelles :
Activité : Architecte de système d'information
Secteur : Finance

Informations forums :
Inscription : septembre 2004
Messages : 1 568
Points : 2 493
Points : 2 493
Hello,

Tu peux essayer ceci :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
// Données
$data = array(
	'CAB' => array( 2011 => 3 ),
	'ECH' => array( 2011 => 1 ),
	'IRR' => array( 2011 => 8, 2012 => 5 )
);
 
// Entête
$header = array_keys($data);
 
// Liste des années
$years = array();
foreach ($data as $value) {
	$years = array_merge($years, array_keys($value));
}
$years = array_unique($years);
 
// Tri croissant des années
asort($years);
?>
<table>
	<thead>
		<tr>
			<th>Ann&eacute;e</th>
			<?php foreach ($header as $label): ?>
				<th><?php echo $label; ?></th>
			<?php endforeach; ?>
		</tr>
	</thead>
	<tbody>
		<?php foreach($years as $year): // Une ligne par année ?>
			<tr>
				<th><?php echo $year; ?></th>
				<?php foreach ($header as $key): ?>
					<?php if (isset($data[$key][$year])): ?>
						<td><?php echo $data[$key][$year]; ?></td>
					<?php else: ?>
						<td>&nbsp;</td>
					<?php endif;?>
				<?php endforeach; ?>
			</tr>
		<?php endforeach; ?>
	</tbody>
</table>
__________________
Si vous ne pouvez expliquer un concept à un enfant de six ans, c'est que vous ne le comprenez pas complètement. Albert EINSTEIN

F.A.Q. : Java, PHP, (X)HTML / CSS

N'oubliez pas de cliquer sur le bouton Résolu en bas de page quand vous avez obtenu une solution à votre problème
Nesmontou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 30/09/2011, 11h10   #3
Nouveau Membre du Club
 
Femme
Inscription : janvier 2010
Messages : 78
Détails du profil
Informations personnelles :
Sexe : Femme

Informations forums :
Inscription : janvier 2010
Messages : 78
Points : 25
Points : 25
Par défaut résolu

N'êtant pas habitué au foreach, j'ai maitenu mes boucles for et je suis enfin arrivé au résultat que je voulais.


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<table border="1">
		<tr>
		<th id="" width="auto">Année</th><?php	
			for($i=0;$i<count($entete);$i++){ 		 ?>
		<th align="center" id="" width="auto"><?php echo $entete[$i]['actions_nature'];?></th><?php	
											    }      ?>
		</tr>
 
 
		<tr>
		<?php
		for($i=0;$i<count($annee);$i++){ 		 ?>
 
				<td align="center" id=""><?php echo $annee[$i]['Annee'];?></td> <?php	
 
				for($k=0;$k<count($entete);$k++){ 		
 
				?>
				<td ><?php for($j=0;$j<count($stat1);$j++){if($stat1[$j]['actions_nature']==$entete[$k]['actions_nature'] and $stat1[$j]['Annee']==$annee[$i]['Annee']){ echo $stat1[$k]['NB'];}}?></td><?php	
													   } 	?>
		</tr>
		<?php
		}   ?>
 
	</table>
naroco est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 20h56.


 
 
 
 
Partenaires

Hébergement Web