voici ma fonction qui crée la requête:
public function mkQuery( $table, $select, $champs, $order, $sens, $limit_start, $limit_nb ) { $this->query_where = '';
if( !is_array( $champs ) ) $champs = array( $champs );
$count_champs = count( $champs );
// si recherche en ET ou OU if( $this->option ) { $i = false; foreach( $this->words as $key => $value ) // boucle sur les mots { // si pas première itération if( $i ) $this->query_where .= $this->sep; $i = true;
$this->query_where .= '( ';
for( $j = 0; $j < $count_champs; $j++ ) // boucle sur les champs { if( $j ) $this->query_where .= ' OR ';
$this->query_where .= '`' . $champs[ $j ] . '` LIKE \'%' . $value . '%\'';
} // for( $j = 0; $j < $this->count_words; $j++ ) // boucle sur les champs
$this->query_where .= ' )'; } // for( $i = 0; $i < $count_champs; $i++ ) // boucle sur les mots } else // recherche phrase exacte { for( $i = 0; $i < $count_champs; $i++ ) // boucle sur les champs { if( $i ) $this->query_where .= ' OR ';
$this->query_where .= $champs[$i] . ' LIKE \'%' . $this->words[0] . '%\' '; } // for( $i = 0; $j < $count_champs; $i++ ) // boucle sur les champs } // else // recherche phrase exacte
// construction de la requête finale
$sql = array( 'select' => 'SELECT ' . $select . ' FROM ' . $table . ' WHERE ' . $this->query_where, 'count' => 'SELECT count(*) FROM ' . $table . ' WHERE ' . $this->query_where );
if( !empty( $order ) ) $sql['select'] .= ' ORDER BY ' . $order . ' ' . $sens; $this->query_where = $sql['select']; if( $limit_nb ) $sql['select'] .= ' LIMIT ' . $limit_start . ', ' . $limit_nb;
return $sql; }
// création d'objet $forbidden = array( 'le', 'la', 'des','les','the','a','an'); $s = new dbSearch( $_POST['search_option'], $_POST['Inventeur'],$forbidden ); $sql = $s->mkQuery( 'tls206_person', 'person_ctry_code,doc_std_name_id,corect_person_name,corect_person_adress,postcode', array('corect_person_name'), 'corect_person_name', 'asc', 0, 0 );
// exécution de la requête $result = mysql_query( $sql['select'] ) or die( 'Erreur MySQL' ); // si erreur
//nombre de résultats $nombre_resultats = mysql_num_rows($result)or exit(mysql_error() . "<br/>$selection_recherche"); //compte le nombre d'entrées sélectionnées par la recherche
if ($nombre_resultats == 0) //s'il n'y a pas de résultat { echo '<br /><b>aucun resultat.<a href="Untitled-2bis.php">recommencer</a></b>'; } else //il y a au moins un résultat { echo '<br /><b>nombre de résultats: ' . $nombre_resultats . '<br /><br /></b>';
?>
merci d'avance pour votre aide
|