J'ai développé la classe suivante, si elle vous intéresse, servez-vous !
Elle tourne comme je veux en PHP 5.6.3
Elle règle de façon générique une problématique récurrente quand on récupère des données des bases, et que l'on veut afficher autre chose.
J'explique !
On a un tableau PHP de ce type, suite à une requête en base :
Le classique tableau de tableaux associatifs quoi :
A partir de ceci, on aimerait regrouper les personnels par fonction_libelle, sans redondance. Appelons cela catégoriser, et ça pourrait prendre ces différentes formes HTML :
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 Array ( [0] => Array ( [fonction_libelle] => Manutentionnaire [employe_id] => 8 [civ] => Mme [nom] => ARNAUD [prenom] => Louise [age] => 19 ans ) [1] => Array ( [fonction_libelle] => Manutentionnaire [employe_id] => 7 [civ] => M [nom] => HENRI [prenom] => Frédéric [age] => 29 ans ) [2] => Array ( [fonction_libelle] => Ouvrier qualifié [employe_id] => 4 [civ] => M [nom] => LAKHDAR [prenom] => Mokrane [age] => 22 ans ) [3] => Array ( [fonction_libelle] => Ouvrier spécialisé [employe_id] => 9 [civ] => M [nom] => MARTIN [prenom] => Ziggy [age] => 19 ans ) [4] => Array ( [fonction_libelle] => Ouvrier spécialisé [employe_id] => 10 [civ] => Mme [nom] => MARTIN [prenom] => Zora [age] => 18 ans ) )
Une table HTML
Une ul HTML (bon on a prévu ol aussi bien sûr)
et enfin un select HTML avec des optgroup
Voici donc la classe View_categorise.class.php
Et voici le script qui utilise cette classe, employe.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 <?php class View_categorise{ protected $data=array();//array PHP tableau de tableaux associatifs protected $keys=array();//les clés des sous-tableaux associatifs protected $quelle_categorie='';//Quel clé doit être la catégorie qui provoque le cumul des autres ? protected $quel_html='';//que veut on sortir comme structure HTML (table, ul, ol, select avec optgroup) protected $quel_select_id_key='';//si select : il nous faut obligatoirement un champ comportant id pour la value protected $quel_style='';//paramètre optionnel, mais s'il y en a un, il ne faut pas qu'il soit avec seulement des espaces protected $html_genere='';//le HTML généré par la classe public function __construct(array $data, $quelle_categorie, $quel_html, $quel_style='', $quel_select_id_key=''){ if($this->set_data($data)!==false && $this->set_quel_style($quel_style) !== false && $this->set_quel_html($quel_html) !== false){ if($this->set_quelle_categorie($quelle_categorie)!==false && $this->set_quel_select_id_key($quel_select_id_key)!==false){ $this->set_html_genere(); } else{ return false; } } else{ return false; } } //pré-requis 0 //on attend un tableau de tableaux associatifs d'au moins une ligne avec au moins 2 champs protected function set_data(array $data){ if(count($data)>0){ $keys=array_keys($data[0]); if(is_array($keys)){ if(count($keys)<2){ return false; } else{ $this->data=$data; $this->keys=$keys; } } else{ return false; } } else{ return false; } } //pré-requis 0 //on attend que le style est soit non fourni, soit fourni auquel cas ne doit pas comporter que des espaces protected function set_quel_style($quel_style=''){ if(is_string($quel_style) && trim($quel_style)!=''){ $this->quel_style=trim($quel_style); } } //pré-requis 0 //on attend 4 entités HTML précises protected function set_quel_html($quel_html){ $htmls=array('table','ul','ol','select'); if(in_array($quel_html,$htmls)){ $this->quel_html=$quel_html; } else{ return false; } } //pré-requis set_data //on attend que la catégorie choisie soit bien une clé du tableau $this->keys protected function set_quelle_categorie($quelle_categorie){ if (in_array($quelle_categorie, $this->keys)) { $this->quelle_categorie=$quelle_categorie; } else{ return false; } } //pré-requis set_quel_html ET set_data //Si $this->quel_html=='select' //=> 1 il a saisi quel_select_id_key //=> 2 non alors on attend qu'une clé ET UNE SEULE possède ce format : "id" ou "id_foo" ou "foo_id" protected function set_quel_select_id_key($quel_select_id_key=''){ if($this->quel_html=='select'){ $count=0; if( !empty($quel_select_id_key) ){ if (in_array($quel_select_id_key, $this->keys)) { $count=1; $this->quel_select_id_key=$quel_select_id_key; } } else{ foreach($this->keys as $key){ if($key=='id' || strtolower(substr($key,0,3))=='id_' || strtolower(substr($key,-3))=='_id'){ $quel_select_id_key=$key; $count++; } } if($count==1){ $this->quel_select_id_key=$quel_select_id_key; } } if($count!=1){ return false; } } } //pré-requis : tous les setters OK protected function set_html_genere(){ switch($this->quel_html){ case 'table': $this->html_genere='<table'.($this->quel_style==''?NULL:' class="'.$this->quel_style.'"').'>'."\n"; $this->html_genere.='<thead>'."\n"; $this->html_genere.='<tr>'; foreach($this->keys as $key){ if($key!=$this->quelle_categorie){ $this->html_genere.='<th>'.mb_convert_case($key , MB_CASE_TITLE).'</th>'; } } $this->html_genere.='</tr>'."\n"; $this->html_genere.='</thead>'."\n"; $this->html_genere.='<tbody>'."\n"; $categorie = "";//passage de témoin foreach($this->data as $row){ //tr changement de categorie if($row[$this->quelle_categorie]!=$categorie){ $this->html_genere.='<tr><th colspan="'.(count($this->keys)-1).'">'.mb_convert_case($row[$this->quelle_categorie] , MB_CASE_TITLE).'</th></tr>'."\n"; $categorie=$row[$this->quelle_categorie]; } //tr ordinaire $this->html_genere.='<tr>'; foreach($this->keys as $key){ if($this->quelle_categorie!=$key){ $this->html_genere.='<td>'.$row[$key].'</td>'; } } $this->html_genere.='</tr>'."\n"; } $this->html_genere.='</tbody>'."\n"; $this->html_genere.='</table>'."\n"; break; case 'ul': case 'ol': $start=true; //ul niveau 1 $this->html_genere='<'.$this->quel_html.($this->quel_style==''?NULL:' class="'.$this->quel_style.'"').'>'."\n"; $categorie = "";//passage de témoin foreach($this->data as $row){ //ul niveau 2 ferme if($row[$this->quelle_categorie]!=$categorie){ if(! $start){ $this->html_genere.='</'.$this->quel_html.'>'."\n".'</li>'."\n"; } //ul niveau 2 ouvre $this->html_genere.='<li>'.mb_convert_case($row[$this->quelle_categorie] , MB_CASE_TITLE)."\n"; $this->html_genere.='<'.$this->quel_html.'>'."\n"; $categorie=$row[$this->quelle_categorie]; } //li ordinaire $this->html_genere.='<li>'; foreach($this->keys as $key){ if($this->quelle_categorie!=$key){ $this->html_genere.=$row[$key].' '; } } $this->html_genere.='</li>'."\n"; $start=false; } //FERMETURE de 3 balises : ul niveau 2, puis li niveau 2, puis ul niveau 1 $this->html_genere.='</'.$this->quel_html.'></li></'.$this->quel_html.'>'."\n"; break; case 'select': $start=true; $this->html_genere='<select'.($this->quel_style==''?NULL:' class="'.$this->quel_style.'"').' name="'.$this->quel_select_id_key.'">'."\n"; $categorie = "";//passage de témoin foreach($this->data as $row){ //opgroup ferme if($row[$this->quelle_categorie]!=$categorie){ if(! $start){ $this->html_genere.='</optgroup>'."\n"; } //opgroup ouvert $this->html_genere.='<optgroup label="'.mb_convert_case($row[$this->quelle_categorie] , MB_CASE_TITLE).'">'."\n"; $categorie=$row[$this->quelle_categorie]; } //option ordinaire $this->html_genere.='<option value="'.$row[$this->quel_select_id_key].'">'; foreach($this->keys as $key){ if($this->quelle_categorie!=$key && $this->quel_select_id_key!=$key){ $this->html_genere.=$row[$key].' '; } } $this->html_genere.='</option>'."\n"; $start=false; } $this->html_genere.='</optgroup></select>'."\n"; break; } } public function get_html_genere(){ return $this->html_genere; } }
ENJOY !
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
57
58
59
60
61
62
63
64
65
66 <?php spl_autoload_register(function ($class) { include '../classes/' . $class . '.class.php'; }); $sql='SELECT f.libelle AS fonction_libelle,e.id as employe_id,e.civ,IF(e.nom_usage IS NOT NULL,e.nom_usage,e.nom_naissance) AS nom,e.prenom,concat(TIMESTAMPDIFF(YEAR,e.naissance,now()), " ans") AS age FROM employe e INNER JOIN fonction f ON e.fonction_id=f.id WHERE f.libelle like ? or f.libelle=? ORDER BY fonction_libelle,nom,prenom;'; $data=array(); try { //(affiche ou pas erreur, UTF8 par défaut -latin1 accepté) $connex=new Connexion_db(true); $connex->set_pdo_rh_local(); $db=$connex->get_pdo(); $stmt = $db->prepare($sql); $stmt->execute(array('Ouvrier%','Manutentionnaire')); while($row= $stmt->fetch(PDO::FETCH_ASSOC)){ $data[]=$row; } unset($db); if(sizeof($data)>0){ $view=new View_categorise($data,'fonction_libelle','table','elegant'); if($view!==false){ $tableau=$view->get_html_genere(); } $view=new View_categorise($data,'fonction_libelle','ul'); if($view!==false){ $ul=$view->get_html_genere(); } $view=new View_categorise($data,'fonction_libelle','ol'); if($view!==false){ $ol=$view->get_html_genere(); } $view=new View_categorise($data,'fonction_libelle','select'); if($view!==false){ $select=$view->get_html_genere(); } } } catch (Exception $e) { print "Erreur ! " . $e->getMessage() . "<br/>"; } ?> <!DOCTYPE html> <html lang="fr"> <head> <meta charset="utf-8"> <title>Liste des Employés</title> <link rel="stylesheet" href="../style.css" media="screen"> </head> <body> <div> <?php echo isset($tableau)?'<div>'.$tableau.'</div>':NULL; echo isset($ol)?'<div>'.$ol.'</div>':NULL; echo isset($ul)?'<div>'.$ul.'</div>':NULL; if(isset($select)){ echo '<div><form action="traitement.php" method="POST">'; echo $select; echo '<input type="submit" name="submit" value="OK" />'; echo '</form></div>'; } ?> </div> </body> </html>
Merci Jreaux62 et Rawsrc, relecteurs sévères mais justes comme je les aime !
Et pour les sceptiques, je mets le code source HTML de la page employe.php, pour prouver qu'il n'y a aucun bug résiduel :
Bien sûr, j'ai indenté à la main...
Code HTML : 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 <!DOCTYPE html> <html lang="fr"> <head> <meta charset="utf-8"> <title>Liste des Employés</title> <link rel="stylesheet" href="../style.css" media="screen"> </head> <body> <div> <div> <table class="elegant"> <thead> <tr><th>Employe_Id</th><th>Civ</th><th>Nom</th><th>Prenom</th><th>Age</th></tr> </thead> <tbody> <tr><th colspan="5">Manutentionnaire</th></tr> <tr><td>8</td><td>Mme</td><td>ARNAUD</td><td>Louise</td><td>19 ans</td></tr> <tr><td>7</td><td>M</td><td>HENRI</td><td>Frédéric</td><td>29 ans</td></tr> <tr><th colspan="5">Ouvrier Qualifié</th></tr> <tr><td>4</td><td>M</td><td>LAKHDAR</td><td>Mokrane</td><td>22 ans</td></tr> <tr><th colspan="5">Ouvrier Spécialisé</th></tr> <tr><td>9</td><td>M</td><td>MARTIN</td><td>Ziggy</td><td>19 ans</td></tr> <tr><td>10</td><td>Mme</td><td>MARTIN</td><td>Zora</td><td>18 ans</td></tr> </tbody> </table> </div> <div> <ol> <li>Manutentionnaire <ol> <li>8 Mme ARNAUD Louise 19 ans </li> <li>7 M HENRI Frédéric 29 ans </li> </ol> </li> <li>Ouvrier Qualifié <ol> <li>4 M LAKHDAR Mokrane 22 ans </li> </ol> </li> <li>Ouvrier Spécialisé <ol> <li>9 M MARTIN Ziggy 19 ans </li> <li>10 Mme MARTIN Zora 18 ans </li> </ol> </li> </ol> </div> <div> <ul> <li>Manutentionnaire <ul> <li>8 Mme ARNAUD Louise 19 ans </li> <li>7 M HENRI Frédéric 29 ans </li> </ul> </li> <li>Ouvrier Qualifié <ul> <li>4 M LAKHDAR Mokrane 22 ans </li> </ul> </li> <li>Ouvrier Spécialisé <ul> <li>9 M MARTIN Ziggy 19 ans </li> <li>10 Mme MARTIN Zora 18 ans </li> </ul> </li> </ul> </div> <div> <form action="traitement.php" method="POST"> <select name="employe_id"> <optgroup label="Manutentionnaire"> <option value="8">Mme ARNAUD Louise 19 ans </option> <option value="7">M HENRI Frédéric 29 ans </option> </optgroup> <optgroup label="Ouvrier Qualifié"> <option value="4">M LAKHDAR Mokrane 22 ans </option> </optgroup> <optgroup label="Ouvrier Spécialisé"> <option value="9">M MARTIN Ziggy 19 ans </option> <option value="10">Mme MARTIN Zora 18 ans </option> </optgroup> </select> <input type="submit" name="submit" value="OK" /> </form> </div> </div> </body> </html>
Partager