this function should do what is required. it quite intelligently scores the number of keywords found in the search fields, and even if they were complete matches or partial matches. scoring search results is the way forward 🙂 read it a couple of times and then visit my new site at www.dreamtemplates.com (yes, shameless plug)...
function base_portal_db_get_articles($parameters) {
// filter acceptable paramters
if (isset($parameters[\"ref\"])) $ref = $parameters[\"ref\"];
if (isset($parameters[\"offset\"])) $offset = $parameters[\"offset\"];
if (isset($parameters[\"count\"])) $count = $parameters[\"count\"];
if (isset($parameters[\"keywords\"])) $keywords = $parameters[\"keywords\"];
// explode keywords
if (isset($keywords) && (!is_array($keywords))) $keywords = $this->private_explode_keywords($keywords);
// build the database query
$query = \'SELECT * \';
if (isset($keywords)) {
$query .= \", (\";
if (is_array($keywords)) {
for ($i = 0; $i < count($keywords); $i++)
if ($keywords[$i] != \'\') $query .= \"if(((articles.title LIKE \'%\" . $keywords[$i] . \"%\') | (articles.content LIKE \'%\" . $keywords[$i] . \"%\')), 1, 0)+\";
} else if ($keywords != \'\') $query .= \"if(((articles.title LIKE \'%$keywords%\') | (articles.content LIKE \'%$keywords%\')), 1, 0)+\";
$query = substr($query, 0, -1);
$query .= \") as keyword_score\";
}
$query .= \' FROM articles AS articles\';
$query .= \' WHERE (1=1)\';
if (isset($ref) && ($ref != \'\'))
$query .= \" AND (articles.ref = \'$ref\')\";
if (isset($keywords)) {
$query .= \" AND (\";
if (is_array($keywords)) {
for ($i = 0; $i < count($keywords); $i++)
if ($keywords[$i] != \'\') $query .= \"(articles.title LIKE \'%\" . $keywords[$i] . \"%\') OR (articles.content LIKE \'%\" . $keywords[$i] . \"%\') OR \";
} else if ($keywords != \'\') $query .= \"(articles.title LIKE \'%$keywords%\') OR (articles.content LIKE \'%$keywords%\') OR \";
$query = substr($query, 0, -4);
$query .= \")\";
}
if (isset($status) && ($status != \'\'))
$query .= \" AND (articles.status = \'$status\')\";
if (isset($before) && ($before != \'\'))
$query .= \" AND (articles.date_live < \'$before\')\";
if (isset($after) && ($after != \'\'))
$query .= \" AND (articles.date_live > \'$after\')\";
// order results
$query .= \' GROUP BY articles.ref\';
$query .= \' ORDER BY \';
if (isset($keywords)) $query .= \' keyword_score DESC, \';
$query .= \' articles.date_live DESC \';
// limit query by offset and count
if (isset($offset)) {
$query .= \" LIMIT \" . $offset;
if (isset($count)) $query .= \", \" . $count;
}
// execute the database query
$result = mysql_db_query($this->database, $query);
// for debugging
$this->yellow_alert(\"query: $query<br>\");
$this->yellow_alert(\"error: \" . mysql_error() . \"<br><br>\");
// gather and organise results
$results = array();
if (($result) && (mysql_num_rows($result) > 0))
while($row = mysql_fetch_array($result))
$results[] = $row;
// return an array of the categories
return $results;
}