<?php
/*------------------------------*/
/*
Titre : Moteur de recherche Multi Fonctions - MySQLi
Auteur : R@f
Date édition : 10 Mai 2006
Date mise a jour : 18 Sept 2019
Rapport de la maj:
- refactoring du code en PHP 7
- fonctionnement du code vérifié
*/
/*------------------------------*/?>
********
Formulaire
********
<form action="" method="post">
<input type="text" name="search" size="100"><br>
<input type="radio" name="option" value="all" checked>Recherche tous les
mots<br>
<input type="radio" name="option" value="one">Rechercher un de ces mots<br>
<input type="radio" name="option" value="sentence">Rechercher l'expression
exacte
<br><br>
<input type="submit" value="Rechercher" style="position:relative">
</form>
*******
Fonction
*******
<?php
function requete($table,$champs,$select,$order,$sens,$limit_start,$limit_nb,
$count='')
{
// option de recherche
$option = $_POST['option'];
// texte de recherche
$search = $_POST['search'];
// si c'est le premier appel de la fonction
if(!isset($fonction_requete))
{
static $fonction_requete = 1;
// si "Rechercher tous les mots" ou "Rechercher un de ces mots"
if($option == 'all' || $option == 'one')
{
// liste des mots
// sépararateur
if($option == 'all')
$sep = ' AND ';
else
$sep = ' OR ';
} // if($option == 'all' || $option == 'one')
// "Rechercher l'expression exacte"
else
{
$mots = $search;
$sep = '';
}
} // if(!isset($fonction_requete))
if(!is_array($champs))
$champs = array($champs);
if($option == 'all' || $option == 'one')
{
// pour savoir si on en est à  la première itération ou non
$i = 0;
// pour tous les mots
foreach($mots as $mot)
{
if(!$i)
{
$search = '~#^!|!^#~ LIKE \'%' . $mot . '%\'';
$i = 1;
}
else
$search .= $sep . '~#^!|!^#~ LIKE \'%' . $mot . '%\'';
} // foreach($mots as $mot)
} // if($option == 'all' || $option == 'one')
else if($option == 'sentence')
$search = '~#^!|!^#~ LIKE \'%' . $mots . '%\'';
$i = 0;
// début de requête
if(empty($count))
$req_search = 'SELECT ' . $select . ' FROM ' . $table . ' WHERE ';
else
$req_search = 'SELECT count(' . $count . ') FROM ' . $table . ' WHERE ';
// ajout des champs
foreach($champs as $champ)
{
if(!$i)
{
$req_search .= '( ' . str_replace('~#^!|!^#~', $champ, $search) .' ) ';
$i = 1;
}
else
$req_search .= 'OR ( ' . str_replace('~#^!|!^#~', $champ, $search) .
' ) ';
}
if(empty($count))
$req_search .= "ORDER BY $order $sens LIMIT $limit_start, $limit_nb";
return $req_search;
}
?>
Invité
30 Jan 2022 à 11:26merci beaucoup pour le code