Creating a Search Application -
(Page 23 of 29 )
In this function we will do an exact keyword search. This search is relatively straightforward, as it only consists of a single SQL query. Because the number of search terms can change, we need to dynamically create the query. This query will result in URLs being returned along with the number of times those URLs contained the search terms. The results will be ordered from highest to lowest by the number of search term matches.
This function will return the data from the query, or FALSE if there were not any rows in the result set.
<?php function doSearch() { $match = "keywords.keyword in ('" . $this->_searchterms[0] . "'"; for ($i = 1; $i < $this->_numterms; $i++) { $match .= ", '" . $this->_searchterms[$i] . "'"; } $match .= ")"; $query = "SELECT urls.url, count(*) as counter " . "FROM urls, keywords " . "WHERE $match " . "AND keywords.url_id = urls.id " . "GROUP BY keywords.url_id " . "ORDER BY counter DESC"; $result = $this->_db->fetch($query); if (count($result) > 0) $return = $result; else $return = FALSE;
return $return; } ?> |
Next: >>
More Database Articles Articles
More By Matt Wade