bonsoir,
dans un projet, je fais une requête dynamique qui se construit, entre autres, en fonction de cases à cocher; cela donne çà:
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
<?php
if($verif==1){
OuvrirBase();
$req="SELECT id_resultat ";
//$count= count($qui);
for($x=0;$x<=3;$x++){
	if($qui[$x]=="DE") {$req1.=",contact_entite, prenom_entite, civilite_entite, mail_contact_entite";}
	if($qui[$x]=="RH") {$req1.= ", contact_rh, prenom_rh, civilite_rh, mail_contact_rh";}
	if($qui[$x]=="RQ") {$req1.= ", contact_rq, prenom_rq, civilite_rq, mail_contact_rq";}
	if($qui[$x]=="DS") {$req1.= ", contact_ds, prenom_ds, civilite_ds, mail_contact_ds";}
}
$req.= $req1;
$req.=" FROM resultat_v2";
if(!empty($type))$etablissement=" and type_etablissement= '$type'";
if(empty($dep)){$codepostal="";}else{$codepostal="cp_entite like '$dep%'";}
$req.=" WHERE $codepostal $etablissement";
if(empty($codepostal) && empty($type)){$and="";}else{$and="and";}
if($Certif==1){
$req.=" $and date_certif_entite < '$inferieur_an-$inferieur_mois-01'";
}
if($Certif==2){
$req.=" $and date_certif_entite > '$superieur_an-$superieur_mois-01'";
}
if($Certif==3){
$req.=" $and date_certif_entite BETWEEN '$entre_an1-$entre_mois1-01' AND '$entre_an2-$entre_mois2-01'";
}
$req.=" order by cp_entite";
$res= mysql_query($req) or die (mysql_error());
print(mysql_num_rows($res)."<br />");  // le print est pour les essais
$req1= substr($req1,1);$req1T=explode(",",$req1);
print_r($req1T); // la, je mets en tableau les champs qui vont me servir pour le résultat
 
print($req."<br />"); // j'affiche ma requête pour le contrôle du code
?>
jusque là, pas de problème; c'est après que çà m....de
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
<?php
while($ligneRes=mysql_fetch_array($res)){
 
$r=1;
	for($x=0;$x<= count($req1T)-1;$x++){
	if($ligneRes[$x+3+$r]<>""){
	$insert.="INSERT INTO  tmp_mail VALUES('$ligneRes[0]',";
		if($ligneRes[$x+$r]<>""){$insert.="'".$ligneRes[$x+$r]."',";}else{$insert.="'',";}
		if($ligneRes[$x+1+$r]<>""){$insert.="'".$ligneRes[$x+1+$r]."',";}else{$insert.="'',";}
		if($ligneRes[$x+2+$r]<>""){$insert.="'".$ligneRes[$x+2+$r]."',";}else{$insert.="'',";}
		if($ligneRes[$x+3+$r]<>""){$insert.="'".$ligneRes[$x+3+$r]."')";}else{$insert.="'')";}
		$insert.="<br />";
//mysql_query($insert) or die(mysql_error()."erreur");
print($insert);
			}
		$r+=2;
		}
$insert="";
}
FermerBase();
}
?>
en fait, je n'arrive pas à faire ma boucle pour qu'elle prenne les champs demandés dans la requête.
je vous joint aussi le form pour la compréhension
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
199
200
201
202
203
204
205
206
<?php
session_start (); // On initialise une session 
require("../constantes.php");
if(! session_is_registered ( "GENERAL_SESSION" ))
{ 
header("location:ident.php");
     }
require("../adm/gerebase.php");
//***********requete date certif*****************
		OuvrirBase();
		$selectdateCertifY="select distinct  EXTRACT(YEAR FROM date_certif_entite) from resultat_v2 order by date_certif_entite desc";
		$resdateCertif1= mysql_query($selectdateCertifY) or die ( mysql_error());
		$resdateCertif2= mysql_query($selectdateCertifY) or die ( mysql_error());
		$resdateCertif3= mysql_query($selectdateCertifY) or die ( mysql_error());
		$resdateCertif4= mysql_query($selectdateCertifY) or die ( mysql_error());
		FermerBase();
//************fin requete date certif************
?>
<html>
<head>
<title>MAILING</title>
</head>
<body>
<a name="haut"></a><div align="center">
<div style="width: 200px;float: right;"><a href="../deconnect.php">d&eacute;connexion</a></div><div style="width: 200px;float: left;"><a href="../index.php">Retour accueil</a></div>
<h2>GESTION DES ENVOIS DE MAILINGS</h2>
<?php
/*
if(empty($dep)){
Ouvrirbase();
$res = mysql_query('SELECT COUNT(*) AS total FROM resultat_v2') OR die(mysql_error());
$row = mysql_fetch_assoc($res);

print("Nombre total d'enregistrements dans la base de donn&eacute;es = " . $row['total']."<br /><br />");
Fermerbase();
}*/
?>
Pour quel d&eacute;partement et quel type d'&eacute;tablissement?
<form method="get" action="<?php $php_self ?>">
d&eacute;partement <select name="dep">
<option value="">tous</option>
 
<?php
for($x=1;$x<100;$x++){
print("<option value=\"".str_pad($x,2, "0", STR_PAD_LEFT)."\"");
if($dep==$x)print("selected=\"selected\"");
print(">".str_pad($x,2, "0", STR_PAD_LEFT)."</option>\r\n");
}
?>
</select>
<?php
OuvrirBase();
$select_type="select distinct type_etablissement from resultat_v2";
$res_type=mysql_query($select_type) or die (mysql_error());
?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type d'&eacute;tablissement <select name="type">
<option value="">Tous</option>
<?php
while($ligne=mysql_fetch_array($res_type)){
print("<option value=\"$ligne[0]\"");
if($ligne[0]==$type)print(" selected=\"selected\"");
print(">$ligne[0]</option>\r\n");
}
FermerBase();
?>
</select>
<br /><br />
<table width="450" border="1"><caption>Pour quel cr&eacute;neau de date de certification?<br /><span style="font-style: italic;">(cochez la case concern&eacute;e et choisisez les mois et ann&eacute;e)</span></caption>
    <tr>
      <td width="40%">inf&eacute;rieur au 1<sup>er</sup> jour de</td>
      <td width="5%"> 
        <input type="radio" name="Certif" value="1">
      </td>
      <td width="51%" align="center"> 
        Ann&eacute;e <select name="inferieur_an">
		<?php
		while($ligneDateCertifY=mysql_fetch_array($resdateCertif1)){
		print("<option value=\"$ligneDateCertifY[0]\"");
if($ligneDateCertifY[0]==$type)print(" selected=\"selected\"");
print(">$ligneDateCertifY[0]</option>\r\n");
		}
		?>
        </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		Mois <select name="inferieur_mois">
		<?php
		for($x=1;$x<13;$x++){
print("<option value=\"".str_pad($x,2, "0", STR_PAD_LEFT)."\"");
if($dep==$x)print("selected=\"selected\"");
print(">".str_pad($x,2, "0", STR_PAD_LEFT)."</option>\r\n");
}
		?>
		</select>
      </td>
    </tr>
    <tr>
      <td width="40%">sup&eacute;rieur au 1<sup>er</sup> jour de</td>
      <td width="5%"> 
        <input type="radio" name="Certif" value="2">
      </td>
      <td width="51%" align="center"> 
        Ann&eacute;e <select name="superieur_an">
		<?php
		while($ligneDateCertifM=mysql_fetch_array($resdateCertif2)){
		print("<option value=\"$ligneDateCertifM[0]\"");
if($ligneDateCertifM[0]==$type)print(" selected=\"selected\"");
print(">$ligneDateCertifM[0]</option>\r\n");
		}
		?>
        </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		Mois <select name="superieur_mois">
		<?php
		for($x=1;$x<13;$x++){
print("<option value=\"".str_pad($x,2, "0", STR_PAD_LEFT)."\"");
if($dep==$x)print("selected=\"selected\"");
print(">".str_pad($x,2, "0", STR_PAD_LEFT)."</option>\r\n");
}
		?>
		</select>
      </td>
    </tr>
    <tr>
      <td width="40%">compris entre le 1<sup>er</sup> jour de</td>
      <td width="5%"> 
        <input type="radio" name="Certif" value="3">
      </td>
      <td width="51%" align="center"> 
        <span style="background-color: #ffffcc;">Ann&eacute;e <select name="entre_an1">
		<?php
		while($ligneDateCertifX=mysql_fetch_array($resdateCertif3)){
		print("<option value=\"$ligneDateCertifX[0]\"");
if($ligneDateCertifX[0]==$type)print(" selected=\"selected\"");
print(">$ligneDateCertifX[0]</option>\r\n");
		}
		?>
        </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		Mois <select name="entre_mois1">
		<?php
		for($x=1;$x<13;$x++){
print("<option value=\"".str_pad($x,2, "0", STR_PAD_LEFT)."\"");
if($dep==$x)print("selected=\"selected\"");
print(">".str_pad($x,2, "0", STR_PAD_LEFT)."</option>\r\n");
}
 
		?>
        </select></span>
        <br />et le 1<sup>er</sup> jour de<br /> 
        <span style="background-color: #ffffcc;">Ann&eacute;e <select name="entre_an2">
		<?php
		while($ligneDateCertifZ=mysql_fetch_array($resdateCertif4)){
		print("<option value=\"$ligneDateCertifZ[0]\"");
if($ligneDateCertifZ[0]==$type)print(" selected=\"selected\"");
print(">$ligneDateCertifZ[0]</option>\r\n");
		}
		?>
        </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		Mois <select name="entre_mois2">
		<?php
		for($x=1;$x<13;$x++){
print("<option value=\"".str_pad($x,2, "0", STR_PAD_LEFT)."\"");
if($dep==$x)print("selected=\"selected\"");
print(">".str_pad($x,2, "0", STR_PAD_LEFT)."</option>\r\n");
}
		?>
        </select></span>
      </td>
    </tr>
  </table>
  <br />
  <br />
 <table width="300" border="1"><caption>Pour quel destinataire?<br /><span style="font-style: italic;">cochez la(les) case(s) concern&eacute;e(s)</span></caption>
  <tr> 
    <td width="135">Directeur &eacute;tablissement</td>
    <td width="49"> 
      <div align="center">
        <input type="checkbox" name="qui[]" value="DE">
      </div>
    </td>
  </tr>
  <tr> 
    <td width="135">Responsable RH</td>
    <td width="49"> 
      <div align="center">
        <input type="checkbox" name="qui[]" value="RH">
      </div>
    </td>
  </tr>
  <tr> 
    <td width="135">Responsable Qualit&eacute;</td>
    <td width="49"> 
      <div align="center">
        <input type="checkbox" name="qui[]" value="RQ">
      </div>
    </td>
  </tr>
  <tr> 
    <td width="135">Directeur des Soins</td>
    <td width="49"> 
      <div align="center">
        <input type="checkbox" name="qui[]" value="DS">
      </div>
    </td>
  </tr>
</table>
<input type="hidden" name="verif" value="1" /> 
<br /><br /><input type="submit" value="Ok"><span style="color: red;font-weight: bold;">document en cours d'&eacute;laboration</span>
</form>
j'espère que vous avez saisi et que vous pourrez me donner une piste.
merci d'avance