Hello everybody,
I'm trying to do a simple internal search engines for my website. What I was able to do till now is a little too much simple and also doesn't work very good...
Clicking on the button research you are addressed on search.php (of which I post the code below).
The research is necessary to find a kind of schedules composed by an image, a title and a description.
I would like that writing one or more words in the research form, I can get the results where appear those words, if is possible in a rilevance order.
Now, the problems are:
- if I write for example 2 words, separated by a space, and both the 2 words are in the same record, that record will be written 2 times.
- I can't limit the number of results using LIMIT... it doesn't work, I get all the results where that word exist.
- I could go on with the problems but these are already enough....
If somebody had a little of time to help me or at least suggest me a better way, I would be really grateful!!!
PHP Code:
if ($_POST['search']){
if (!empty($_POST['keywords'])) {
$searchStr = $_POST['keywords'];
if (ereg("^[0-9a-zA-Zà-ù,&,'.' ]+$", $searchStr)) {
require ("db/db.php");
print "<div class='risultati'>Risultati della ricerca per \"<b>$searchStr</b>\":</div><br>";
$keys = explode(" ", $searchStr);
for ($x = 0; $x < count($keys); $x++) {
$querystr = "SELECT img,link,numclick,descrizione,ordine FROM totale_tabelle WHERE descrizione = \"$keys[$x]\" OR descrizione LIKE \"%$keys[$x]%\" OR ordine = \"$keys[$x]\" OR ordine LIKE \"%$keys[$x]%\"";
$result = mysql_query($querystr);
if ($frow = mysql_fetch_array($result)) {
$found = true;
do {
$words = $frow["descrizione"];
$boldwords = str_replace($searchStr,"<b>".$searchStr."</b>",$words);
echo "<div class='grid_8'><div class='screen'>".$frow["img"]."</div><div class='text'><h2>".$frow["link"]."<span class='visite'> - ".$frow["numclick"]." visite</span></h2>".$boldwords."</div></div>"."<br>";
} while ($frow = mysql_fetch_array($result));
}
}
if (!$found) {
print("<div class='risultati'><b>Nessun risultato correlato alla ricerca è stato trovato.</b></div><br>");
}
}
else {
print("<div class='risultati'><br><b>La tua stringa di ricerca contiene caratteri non ammessi.</b></div><br>");
}
}
else {
print("<div class='risultati'><br><b>Devi inserire almeno una parola chiave per effettuare la ricerca.</b></div><br>");
}
}