I want a query that would return most close to the keywords from the search field. I search in 4 columns. title, description, author, take_place. This is the current code, that returns results from all columns. i want a more specific search result.
$v = explode(' ', strtolower($searchwords));
if($c != ''){ $and = 'AND cat_id = '.$c; }
foreach($v as $val){
$query parts[] = "'%".mysql_real_escape_string($val)."%'";
}
$title = implode(' OR title LIKE ', $query_parts);
$desc = implode(' OR desc LIKE ', $query_parts);
$author = implode(' OR author LIKE ', $query_parts);
$take_place = implode(' OR take_place LIKE ', $query_parts);
$sql_res=mysql_query("select * from nt_seminars where ((title like {$title}) or
(description like {$description}) or (author like {$author}) or
(take_place like {$take_place})) $and ORDER BY date;");
This query returns every result that has any of the words, i want to retrieve most relevant combo search keywords. it would be like:
if results in author and take_place, retrieve those only. Somehow, replace the OR with AND, but then it wouldn't return any results at all. This query is triggered by ajax.
Got something better, but still not what i wanted..
$q = $_POST['searchword'];
$arraySearch = explode(" ", $_POST['searchword']);
// table fields to search
$arrayFields = array(0 => "title", 1 => "city", 2=> "author");
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = "SELECT * FROM ".$table." WHERE (";
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
$b++;
if ($b < $countSearch)
{
$query = $query." AND ";
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.") OR (";
}
}
$query = $query.")";
$query_result = mysql_query($query);
if(mysql_num_rows($query_result) < 1)
{
echo '<span class="no-borders">No results found</span>';
}
else
{
echo '<ul class="list-group no-radius m-b-none m-t-n-xxs list-group-lg no-border">';
// output list of articles
while($row = mysql_fetch_assoc($query_result))
{
//output loop
}
}
This returns keywords found only at one of columns. I need an elimination type of query..