Bonjour,
Je construis un script de recherche avec plusieurs niveaux, une première ligne où on peut sélectionner selon des checkboxs.
Une seconde ligne où l'on choisit recherche fulltexte ou recherche exacte et une troisième avec trois input recherche où on peut ajouter avec liste déroulante "and, or, and not".
C'est sur la seconde et troisième partie que je rencontre des difficultés, la recherche ne se fait pas lorsqu'on clique sur recherche fulltext ou recherche exacte. Le premier input est pris en compte mais pas les suivants.
Je ne sais pas trop comment articuler mes conditions, c'est ça qui bloque.
Merci beaucoup d'avance


Code php : 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 
 
<?php
mysql_connect('localhost','root','') or die("erreur de connexion au serveur");;
mysql_select_db('bdd');
 
//On determine l'expression a rechercher
if(isset($_GET['recherche']) || isset($_GET['recherche1']) || isset($_GET['recherche2']))
{
        /*$recherche = htmlentities($_GET['recherche']);
		$recherche1 = htmlentities($_GET['recherche1']);
		$recherche2 = htmlentities($_GET['recherche2']);*/
		$recherche = mysql_real_escape_string(htmlspecialchars($_GET['recherche']));
		$recherche1 = mysql_real_escape_string(htmlspecialchars($_GET['recherche1']));
		$recherche2 = mysql_real_escape_string(htmlspecialchars($_GET['recherche2']));
 
		$mode = mysql_real_escape_string(htmlspecialchars($_GET['mode']));
}
else
{
        $recherche = 'votre recherche';
}
 
 
//On determine le type de recherche
if(isset($_GET['type']))
{
        if($_GET['type']=='produit1')//code article
        	{
                $type = 1;
        	}
        	elseif($_GET['type']=='produit2')//champs4
        	{
                $type = 2;
        	}
			elseif($_GET['type']=='produit3')//champs3
       	 	{
                $type = 3;
        	}
			elseif($_GET['type']=='produits4')//champs2
        	{
                $type = 4;
        	}
 
			elseif($_GET['type']=='produits5')//champs5
        	{
                $type = 5;
        	}
 
       		else 
			//recherche mot
        	{
                $type = 1;
        	}
	}
	else
	{
        $type = 1;
		//type par defaut: la rechercher par product number (champ1)
}
 
 
 
 
//On determine le type de recherche
if($mode == "full_text_search")
	{
		$and_ou_or = ' AND '; //on utilisera AND dans la boucle
		}
		else
		{
		$and_ou_or = ' OR '; //on utilisera OR dans la boucle
	}
 
 
if ($mode == "exact_search") //si le mode de recherche est par expression exacte
	{
		$selection_recherche = mysql_query("SELECT * FROM table WHERE champ1 OR champs4 LIKE '%$recherche%' ORDER BY champ1");
	}
			else //si le mode de recherche n'est pas par expression exacte
	{
			$mots = explode(" ", $recherche); //séparation des mots
			$nombre_mots = count ($mots); //compte le nombre de mots
			$valeur_requete = '';
 
			for($nombre_mots_boucle = 0; $nombre_mots_boucle < $nombre_mots; $nombre_mots_boucle++) 
			//tant que le nombre de mots de la recherche est supérieur à celui de la boucle, on continue en augmentant le nombre de mots de 1 à chaque fois
			{
				$valeur_requete .= '' . $and_ou_or . 'champ1 OR champs4 LIKE \'%' . $mots[$nombre_mots_boucle] . '%\'';
				//modification de la variable $valeur_requete
			}
			$valeur_requete = ltrim($valeur_requete,$and_ou_or); //suppression de AND ou de OR au début de la boucle
 
			$requete_recherche = "SELECT * FROM table WHERE $valeur_requete ORDER BY champ1";
			$selection_recherche = mysql_query($requete_recherche) or die("Erreur dans $requete_recherche :<br />".mysql_error()); //requête avec le résultat de la boucle dedans
 
			/////////////////////////////
			echo $requete_recherche;
			/////////////////////////////
	}
 
	$nombre_resultats = mysql_num_rows($selection_recherche); //compte le nombre d'entrées sélectionnées par la recherche
 
 
 
 
 
 
 
 
 
//Si surligner mots dans resultats
if(!isset($_GET['surligner']) or $_GET['surligner']!='true')
{
        $surligner = false;
}
else
{
        $surligner = true;
}
 
 
	//Requete...
	$req = 'SELECT champ1,champs2,champs3,champs4,champs5 FROM table WHERE ';
 
 
	if($type==1)
	{
			$req .= ' champ1 LIKE "%'.$recherche.'%" ';
	}
 
		elseif($type==2)
		{		
				$mots = explode(' ',$recherche);
				//on sépare les mots en mots clés
 
				foreach($mots as $mot)
				{
						$req .= ' champs4 LIKE "%'.$mot.'%" AND';
				}
				$req .= ' 1=1';
		}
 
		elseif($type==3)
		{
				$req .= ' champs3 LIKE "%'.$recherche.'%" ';
		}
 
 
		elseif($type==4)
		{
				$req .= ' champs2 LIKE "%'.$recherche.'%" ';
		}
 
 
		elseif($type==5)
		{
				$req .= ' champs5 LIKE "%'.$recherche.'%" ';
		}
 
		elseif($type==6)//full text
		{
				$req .= ' MATCH(champ1, champs5, champs2, champs3, champs4) AGAINST ('+$recherche*' IN BOOLEAN MODE)';
 
		}
 
		elseif($type==7)//recherche exacte
		{
				$req .= ' MATCH(champ1, champs5, champs2, champs3, champs4) AGAINST ('+$recherche*' IN BOOLEAN MODE)';
		}
	else
	{
			$req .= ' MATCH (champ1, champs4, champs2) AGAINST ('%'.$recherche.'%')
					  FROM table			  
					  ORDER BY champ1 DESC';
	}
 
 
	//Les résultats sont classés par ordre croissant
	$req .= ' order by champ1 asc';
	$requete = mysql_query($req);
 
 
?>
 
 
 
 
 
 
<!-- form de recherche -->
 
<form action="" method="get">
  <p>Type de recherche: </p>
  <table width="86%" border="0">
    <tr>
      <td width="12%"><input type="radio" name="type" value="produit1"<?php if($type==1){echo 'checked="checked"';} ?> /> produit1</td>
      <td width="14%"><input type="radio" name="type" value="produit2"<?php if($type==2){echo 'checked="checked"';} ?> /> produit2</td>
      <td width="13%"><input type="radio" name="type" value="produit3"<?php if($type==3){echo 'checked="checked"';} ?> /> produit3</td>
      <td width="14%"><input type="radio" name="type" value="produits4"<?php if($type==4){echo 'checked="checked"';} ?> /> produit4</td>
      <td width="12%"><input type="radio" name="type" value="produits5"<?php if($type==5){echo 'checked="checked"';} ?> /> produit5</td>
      <td width="14%">&nbsp;</td>
      <td width="21%">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="radio" name="mode" value="full_text_search"<?php if($mode=='full_text_search'){echo 'checked="checked"';} ?> /> Full Text Search   
	  </td>
      <td>&nbsp;</td>
      <td><input type="radio" name="mode" value="exact_search"<?php if($mode=='exact_search'){echo 'checked="checked"';} ?> />Exact search </td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td rowspan="2">NEW E-Search </td>
    </tr>
    <tr>
      <td>Search for: </td>
      <td><input type="text" name="recherche" value="<?php echo $recherche; ?>" /></td>
      <td>
	  <select name="choix1" id="choix1">           
               <option value="and">And</option>
               <option value="or">Or</option>
               <option value="and not">And not</option>
       </select>	  </td>
      <td><input type="text" name="recherche1" value="<?php echo $recherche1; ?>" /></td>
      <td>
	  <select name="choix2" id="choix2">           
               <option value="and">And</option>
               <option value="or">Or</option>
               <option value="and not">And not</option>
       </select>	  </td>
      <td><input type="text" name="recherche2" value="<?php echo $recherche2; ?>" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <p>
  	<br />
    <br />
    <br />
    <br />
    <br />
    <br />
    Mettre en gras les mots recherch&eacute;s:
    <input type="checkbox" name="surligner" value="true" <?php if($surligner){echo 'checked="checked"';} ?> />
    <br />
    <br />
    <br />
    <input type="submit" value="Rechercher" />
  </p>
</form>
 
<!-- FIN form recherche -->
 
 
 
 
 
<h2>R&eacute;sultats</h2>
<table>
        <tr>
                <th>Identifiant</th>
                <th>Nom</th>
                <th>Informations</th>
        </tr>
 
 
<?php
//On affiche les resultats
while($resultat = mysql_fetch_array($requete))
{
?>
 
        <tr>
                <td><?php echo $resultat['champ1']; ?></td>
                <td><?php //echo preg_replace('#('.preg_quote($recherche).')#i', '<strong>$1</strong>', $resultat['champs4']); 
						  echo $resultat['champs4'];
				    ?>
				</td>
                <td>			
					<?php
						if($surligner)
						//Si demande pour surligner les mots
						{
								if($type=="produit2")
								{
										echo preg_replace('#('.preg_quote($recherche).')#i', '<strong>$1</strong>', $resultat['champs4']);
										//On surligne l'expression exacte
								}
								else
								{
										echo preg_replace('#('.str_replace(' ','|',preg_quote($recherche)).')#i', '<strong>$1</strong>', $resultat['champs4']);
										//On surligne les mots cles de la recherche
								}
						}
						else
						{
								echo $resultat['champs4'];
								//Pas surligné
						}
					?>
				</td>
        </tr>
<?php
}
?>
</table>