Bonjour,

Je viens de faire un petit bout de code qui doit afficher les résultat d'une requête dans un tableau html.
Le hic vient du fait que pour le nombre de colonne à afficher change 1 ligne sur 2.
Première ligne : j'affiche 3 enregistrements.
Deuxième ligne : j'affiche 4 enregistrements.

Ce code fonctionne mais est-il "correct" ou franchement à la ramasse et peut être bien plus optimisé ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
46
47
48
49
50
51
52
53
54
55
56
 
$sqlCategory = "select idWork, urlWork, idWorkCat, nomWork, nomWorkCat from work 
						JOIN workcategory ON work.idWorkCatWork = workcategory.idWorkCat
						where nomWorkCatUse ='".$menuUser."'
						ORDER BY anneeWork DESC ";
		$reqCategory = $bdd->prepare($sqlCategory, array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));
		$reqCategory->execute();
 
		$sqlBoulotCount = "select count(*) as nb from work 
						JOIN workcategory ON work.idWorkCatWork = workcategory.idWorkCat
						where nomWorkCatUse ='".$menuUser."'
						ORDER BY anneeWork DESC ";
		$reqBoulotCount = $bdd->query($sqlBoulotCount);
		$nbBoulotCount= $reqBoulotCount->fetchAll();
 
		$nbBoulot = $nbBoulotCount[0]['nb'];
 
echo '<table style="color:black;">';
		echo '<tbody>';
 
		$ii = 0;
		$row = $reqCategory->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
		$z = 0;
		do {
			if ($ii == 0) {
				echo '<tr class="pair">';
					for ($x=0; $x < 3; $x++) {
						if ($row != null) {
							echo '<td><a href="'.get_domain().'graphiste/'.$row[1].'" title="'.$row[3].'"><img src="'.get_domain().'img/rond.png" /></a></td>'."\n";
							$row = $reqCategory->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
							$z++;
						}
						else {
							break;
						}
					}
				echo '</tr>'."\n";
				$ii++;
			}
			else {
				echo '<tr>';
					for ($x=0; $x <4; $x++) {
						if ($row != null) {
							echo '<td><a href="'.get_domain().'graphiste/'.$row[1].'" title="'.$row[3].'"><img src="'.get_domain().'img/rond.png" /></a></td>'."\n";
							$row = $reqCategory->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
							$z++;
						}
						else {
							break;
						}
					}
					echo '</tr>'."\n";
				$ii--;
			}
		}
		while ($z < $nbBoulot);
Merci de votre aide !