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
| <?php
mysql_connect('localhost','root','');
mysql_select_db('tests');
//On determine l'expression a rechercher
if(isset($_GET['recherche']))
{
$rec = htmlentities($_GET['recherche']);
}
else
{
$rec = 'php MYSQL';
}
//On determine le type de recherche
if(isset($_GET['type']))
{
if($_GET['type']=='un')//Un des mots
{
$type = 1;
}
elseif($_GET['type']=='tout')//Tout les mots
{
$type = 2;
}
else//L'expression exacte
{
$type = 3;
}
}
else
{
$type = 1;//type par defaut: L'expression exacte
}
//On determine si on doit surligner les mots dans les resultats
if(!isset($_GET['surligner']) or $_GET['surligner']!='true')
{
$surligner = false;
}
else
{
$surligner = true;
}
//On dertermine les identifiants, les noms et les informations des utilisateur
$req = 'SELECT id, username, email FROM forumusers WHERE ';
if($type==1)
{//ayant un des mots dans leurs informations
$mots = explode(' ',$rec);//En separre lexpression en mots cles
foreach($mots as $mot)
{
$req .= ' email LIKE "%'.$mot.'%" OR';
}
$req .= ' 1=0';
}
elseif($type==2)
{//ayant tout des mots dans leurs informations
$mots = explode(' ',$rec);//En separre lexpression en mots cles
foreach($mots as $mot)
{
$req .= ' email LIKE "%'.$mot.'%" AND';
}
$req .= ' 1=1';
}
else
{//ayant l'expression exacte dans leurs informations
$req .= 'email LIKE "%'.$rec.'%"';
}
//Les utilisateur seront ranges par identifiant en ordre croissant
$req .= ' order by id asc';
$requete = mysql_query($req);
//Le formulaire de recherche
?>
<form action="" method="get">
Expression à rechercher: <input type="text" name="recherche" value="<?php echo $rec; ?>" /><br />
Type de recherche: <input type="radio" name="type" value="un"<?php if($type==1){echo 'checked="checked"';} ?> /> Un des mots <input type="radio" name="type" value="tout"<?php if($type==2){echo 'checked="checked"';} ?> /> Tout les mots <input type="radio" name="type" value="exacte"<?php if($type==3){echo 'checked="checked"';} ?> /> Expression exacte<br />
Mettre en gras les mots recherchés: <input type="checkbox" name="surligner" value="true" <?php if($surligner){echo 'checked="checked"';} ?> /><br />
<input type="submit" value="Rechercher" />
</form>
<h2>Résultats</h2>
<table>
<tr>
<th>Identifiant</th>
<th>Nom</th>
<th>Informations</th>
</tr>
<?php
//On affiche les resultats
// LIGNE 225
while($dnn = mysql_fetch_array($requete))
{
?>
<tr>
<td><?php echo $dnn['id']; ?></td>
<td><?php echo $dnn['username']; ?></td>
<td><?php
if($surligner)//Si il faut surligner les mots, on les surligne
{
if($type==3)
{
echo preg_replace('#('.preg_quote($rec).')#i', '<strong>$1</strong>', $dnn['email']);//On surligne l'expression exacte
}
else
{
echo preg_replace('#('.str_replace(' ','|',preg_quote($rec)).')#i', '<strong>$1</strong>', $dnn['email']);//On surligne les mots cles de la recherche
}
}
else
{
echo $dnn['email'];//On ne surligne pas
}
?></td>
</tr>
<?php
}
?>
</table> |
Partager