I have a search form. If the user makes a typo like ager
instead of anger
, it should still show the relevant results instead of displaying 0 results found.
I came across the PHP levenshtein function and the example that they have given with array is exactly what I want [except that the user can input a sentence rather than one word], but I would like to implement it with database, but have no idea as to how go about implementing it with database.
This is my code:
if(!empty($search))
{
try {
$query = $this->_db->prepare($sql);
$query->execute();
if(!$query->rowCount()==0)
{
$foundRows = $this->_db->query("SELECT FOUND_ROWS()")->fetchColumn();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$cQuote = $this->highlightWords(htmlspecialchars($row['cQuotes']),$search);
$search_result[] = array('success' => true, 'totalRows' => $foundRows, 'cQuotes' => $cQuote, 'vAuthor' => $this->h($row['vAuthor']), 'vBookName' => $this->h($row['vBookName']), 'vRef' => $this->h($row['vRef']));
}
$response = json_encode($search_result);
echo $response;
return TRUE;
}
else
{
$ex = "No results found for " .$search;
$this->errorMsg($ex);
}
$query->closeCursor();
}
catch (Exception $ex){
$ex = "Problem: " .$ex;
$this->errorMsg($ex);
}
}
else
{
$ex = "Please enter something";
$this->errorMsg($ex);
}
I should add that I'm using MySQL + PDO.