Codice PHP:
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>Ricerca</title>
</head>
<body>
<?php
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$q = 'SELECT * FROM prenotazioni WHERE ';
$parts = explode(' ', $name);
$finded = false;
$firstName = array_shift($parts);
if (count($parts)) {
$lastName = implode(' ', $parts);
$new_q = $q;
$new_q .= ' ( nome LIKE \'%' . $lastName . '\' AND cognome LIKE \'%' . $firstName . '\' )';
$new_q .= ' OR ( cognome LIKE \'%' . $lastName . '\' AND nome LIKE \'%' . $firstName . '\' )';
$exec = mysql_query( $new_q);
if (!mysql_num_rows($exec)) {
$new_q = $q;
$new_q .= ' nome LIKE \'%' . $lastName . '\' OR cognome LIKE \'%' . $firstName . '\' ';
$new_q .= ' OR nome LIKE \'%' . $firstName . '\' OR cognome LIKE \'%' . $lastName . '\' ';
$exec = mysql_query( $new_q);
if (mysql_num_rows($exec)) {
$finded = true;
}
}
}
else {
$new_q = $q;
$new_q .= ' nome LIKE \'%' . $firstName . '\' OR cognome LIKE \'%' . $firstName . '\' ';
$exec = mysql_query( $new_q);
if (mysql_num_rows($exec)) {
$finded = true;
}
}
if ($finded) {
echo '<tr>
<td width="242">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td>
</table>
<p><br>
<br>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}
?>
</body>
</html>
E' l'ultima che faccio, scusa ma ho tanto lavoro da fare...
Database:
- mario rossi
- marco rovi
- lucia bianchi
se cerchi:
"mario rossi", ti trova esattamente quelli che si chiamano "mario rossi", se cerchi "ma ro" ti trova:
marco rossi e marco rovi
se cerchi "luca rossi" ti trova sia lucia bianchi e mario rossi.