Пример #1
0
function search_keyword($terms)
{
    con();
    $terms = search_split_terms($terms);
    $terms_db = search_db_escape_terms($terms);
    $terms_rx = search_rx_escape_terms($terms);
    $parts = array();
    foreach ($terms_db as $term_db) {
        $parts[] = "article_keywords.keywords RLIKE '{$term_db}'";
    }
    //search for similar items on the keywords table
    $parts = implode(' AND ', $parts);
    $sql = "SELECT article_keywords.articleID FROM article_keywords WHERE {$parts}";
    $rows = array();
    //perform query to the database
    $result = mysql_query($sql);
    //start fetching results
    if (!$result) {
        return false;
    } else {
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $rows[] = $row;
        }
        //print_r($rows);
        return $rows;
        //return array of id numbers
    }
}
Пример #2
0
 /**
  * Search the info from the DB
  *
  * Uses a smarter search engine to search through the fields and return certain fields, puts the results in a local $results array
  *
  * @param $keywords A string that we are going to be searching the DB for
  * @param $search_fields An array of the field keys that we are going to be searching through
  * @param $return_fields An array of the field keys that are going to be returned
  * @return int
  */
 public function Search($keywords, $search_fields, $return_fields)
 {
     // Use the global db class
     global $db;
     $returns = array();
     // Push $this into the array
     array_unshift($this->join_class, $this);
     array_unshift($this->join_type, '');
     array_unshift($this->join_on, '');
     // Setup the return fields
     if (!isMultiArray($return_fields)) {
         array_unshift($returns, $return_fields);
     } else {
         $returns = $return_fields;
     }
     // Split up the terms
     $terms = search_split_terms($keywords);
     $terms_db = search_db_escape_terms($terms);
     $terms_rx = search_rx_escape_terms($terms);
     // Create list of statements
     $parts = array();
     foreach ($terms_db as $term_db) {
         if (is_array($search_fields)) {
             foreach ($search_fields as $field) {
                 $parts[] = '`' . $this->database . '`.`' . $this->table . '`.' . $field . ' RLIKE \'' . $term_db . '\'';
             }
         } else {
             $parts[] = '`' . $this->database . '`.`' . $this->table . '`.' . $search_fields . ' RLIKE \'' . $term_db . '\'';
         }
     }
     $parts = '(' . implode(' OR ', $parts) . ')';
     if ($this->conditions != '') {
         $parts .= ' AND ' . $this->conditions;
     }
     // Loop through all the joined classes
     foreach ($this->join_class as $key => $class) {
         // Create the return fields
         if (is_array($returns[$key]) && count($returns[$key]) > 0) {
             // Require primary key returned
             if (!in_array($class->primary, $returns[$key])) {
                 $return .= '`' . $class->database . '`.`' . $class->table . '`.' . $class->primary . ', ';
             }
             // List all other fields to be returned
             foreach ($returns[$key] as $field) {
                 if (!is_array($field)) {
                     $return .= trim($field) != '' ? '`' . $class->database . '`.`' . $class->table . '`.' . $field . ', ' : '';
                 } else {
                     foreach ($field as $sub_field) {
                         $return .= trim($sub_field) != '' ? '`' . $class->database . '`.`' . $class->table . '`.' . $sub_field . ', ' : '';
                     }
                 }
             }
         } else {
             // Local class return values
             if (is_array($returns[$key])) {
                 foreach ($returns[$key] as $field) {
                     $fields[] = '`' . $this->database . '`.`' . $this->table . '`.' . $field;
                 }
                 $return .= implode(', ', $fields);
             } else {
                 $return .= '`' . $class->database . '`.`' . $class->table . '`.*, ';
             }
         }
         // Create the Joins
         if ($key > 0) {
             $join .= ' ' . $this->join_type[$key] . ' JOIN `' . $this->join_class[$key]->database . '`.`' . $this->join_class[$key]->table . '` ON (`' . $this->join_class[$key]->database . '`.`' . $this->join_class[$key]->table . '`.' . $this->join_on[$key][0] . ' = `' . $this->database . '`.`' . $this->table . '`.' . $this->join_on[$key][1] . ') ';
         }
     }
     $query = 'SELECT ' . substr($return, 0, -2) . ' FROM `' . $this->database . '`.`' . $this->table . '`' . $join . ' WHERE ' . $parts;
     $query .= $this->group_by != '' ? ' GROUP BY ' . $this->group_by : '';
     $result = $db->Query($query, $this->database);
     $results = array();
     while ($info = $db->FetchArray($result)) {
         $info['score'] = 0;
         foreach ($terms_rx as $term_rx) {
             if (is_array($search_fields)) {
                 foreach ($search_fields as $field) {
                     $info['score'] += preg_match_all("/{$term_rx}/i", $info[$field], $null);
                 }
             } else {
                 $info['score'] += preg_match_all("/{$term_rx}/i", $info[$search_fields], $null);
             }
         }
         $results[] = $info;
     }
     // Pop $this from the array
     array_shift($this->join_class);
     array_shift($this->join_type);
     array_shift($this->join_on);
     uasort($results, 'search_sort_results');
     $this->results = $results;
     return count($results);
 }