<?php require('dbclass.php');
class Search {
var $_db; var $_searchterms; var $_numterms;
function Search($keywords) { $this->_db = new DB_Class('test', 'username', 'password'); if (!get_magic_quotes_gpc()) { $keywords = addslashes ($keywords); } $this->_searchterms = explode(' ', $keywords); $this->_numterms = count($this->_searchterms); }
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; }
function _highpercent(&$item, $key, $array) { if ($item < 60) unset($array["$key"]); }
function _slashit(&$item, $key) { $item = addslashes($item); }
function doFuzzy() { $match = "LEFT(keyword,1) in ('" . substr($this->_searchterms[0], 0, 1) . "'"; for ($i = 1; $i < $this->_numterms; $i++) { $match .= ", '" . substr($this->_searchterms[$i], 0, 1) . "'"; } $match .= ")"; $query = "SELECT DISTINCT(keyword) FROM keywords " . "WHERE $match"; $keywords = $this->_db->fetch($query); reset($this->_searchterms); foreach ($this->_searchterms as $term) { foreach ($keywords as $keyword) { $word = $keyword['keyword']; similar_text($term, $word, $matches["$term"]["$word"]); } array_walk ($matches["$term"], array($this, '_highpercent'), &$matches["$term"]); } if($this->_numterms > 1) { $merge = '$merged = array_merge($matches["' . $this->_searchterms[0] . '"]'; for ($i = 1; $i < $this->_numterms; $i++) { $merge .= ', $matches["' . $this->_searchterms[$i] . '"]'; } $merge .= ');'; eval ($merge); } else { $merged = $matches[$this->_searchterms[0]]; } arsort($merged); $search = array_keys($merged); array_walk($search, array($this, '_slashit')); $match = "keywords.keyword in ('" . $search[0] . "'"; $numwords = count ($search); for ($i = 1; $i < $numwords; $i++) { $match .= ", '" . $search[$i] . "'"; } $match .= ")"; $query = "SELECT urls.url, keywords.keyword, " . "count(*) as counter " . "FROM urls, keywords " . "WHERE $match " . "AND keywords.url_id = urls.id " . "GROUP BY keywords.url_id, keywords.keyword " . "ORDER BY counter DESC"; $result = $this->_db->fetch($query); if (count($result) > 0) $return = $result; else $return = FALSE;
return $return; } } ?> |