topical media & game development
[] readme course(s) preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthought(s) appendix reference(s) example(s) resource(s) _

talk show tell print

portal-classes-core.search.php



  <?PHP
  
  class Search {
  
          var core;
          var found_ids;
          
          function Search(&core) {
                  this->core = &core;
                  this->core->Db->connectDb();
          }
  
          function checkVariables() {
                  if(isset(_GET['s']) && _GET['s'] != "") {
                          return true;
                  }
                  return false;
          }        
          
          function getSearchWords(searchstring) {
                  searchwords = explode(' ',searchstring);
  
                  foreach(searchwords as word) {
                          if(word != "") {
                                  searchwords2[] = trim(word);
                          }
                  }
  
                  return searchwords2;
  
          }
          
          function doQuickSearch() {
          
                  if(!checkVariables) {
                          // bla
                  }
                  
                  searchstring = _GET['s'];
                  
                  global found_ids;
                  
                  found_ids[] = "";
  
                  //echo '<h1>Searching for "', searchstring, '"...</h1>';
                  
                  searchwords = this->getSearchWords(searchstring);
                  searchstring = implode(" ", searchwords);
                  
  
                  //echo 'Executing queries...<br />';
  
                  results[] = this->titleSearch(searchstring,searchwords);
                  results[] = this->textSearch(searchstring,searchwords);
                  results[] = this->keywordSearch(searchwords);
                  
                  //echo '<pre>',var_dump(results),'</pre>';
                  
                  return results;
          
          }
                  
          function titleSearch(searchstring,searchwords) {
                  global found_ids;
  
                  query = 'select content_id,content_title,content_binary FROM content WHERE MATCH (content_title) AGAINST (\'';
  
                  query .= '>"'.searchstring.'"';
  
                  foreach(searchwords as word) {
                          query .= ' +'.word;
                  }
  
                  query .= '\' IN BOOLEAN MODE);';
  
                  //echo '<pre>',query,'</pre>';
  
                  result = this->core->Db->getPlainQueryResult(query);
                  
                  if(!result) {
                                  //echo('invalid query, ignoring...<br />');
                  }
                  else {
                          while(row = mysql_fetch_array(result, MYSQL_NUM)) {
                                  if(in_array(row[0],found_ids)) {
                                          //echo 'found duplicate search result with ID ',row[0],', ignoring...<br />';
                                  }
                                  else {
                                          results[] = row;
                                          found_ids[] = row[0];
                                  }
                          }
                          //echo '<pre>';
                          //var_dump(results[0]);
                          //echo '</pre>';
  
                          mysql_free_result(result);
                  }
                  
                  return results;
  
          }
  
          function textSearch(searchstring,searchwords) {
                  global found_ids;
  
                  query = 'select content_id,content_title,content_binary FROM content WHERE MATCH (content_binary) AGAINST (\'';
  
                  query .= '>"'.searchstring.'"';
  
                  foreach(searchwords as word) {
                          query .= ' +'.word;
                  }
  
                  query .= '\' IN BOOLEAN MODE);';
  
                  //echo '<pre>',query,'</pre>';
  
                  result = this->core->Db->getPlainQueryResult(query);
                  
                  if(!result) {
                                  //echo('invalid query, ignoring...<br />');
                  }
                  else {
                          while(row = mysql_fetch_array(result, MYSQL_NUM)) {
                                  if(in_array(row[0],found_ids)) {
                                          //echo 'found duplicate search result with ID ',row[0],', ignoring...<br />';
                                  }
                                  else {
                                          results[] = row;
                                          found_ids[] = row[0];
                                  }
                          }
                          //echo '<pre>';
                          //var_dump(results);
                          //echo '</pre>';
  
                          mysql_free_result(result);
                  }
                  
                  return results;
  
          }
  
          function keywordSearch(searchwords) {
                  global found_ids;
                  
                  query = 'SELECT content_id,content_title,content_binary
                  FROM content
                  WHERE content.content_id
                  IN (
                          SELECT content_id
                          FROM content_keywords
                          WHERE content_keywords.keyword_id
                          IN (
                                  SELECT keyword_id
                                  FROM keywords
                                  WHERE keywords.keyword_name = \''.searchwords[0].'\'';
                                  
                  if(count(searchwords) > 1) {
                          array_shift(searchwords);
                          foreach(searchwords as word) {
                                  query .= ' OR keywords.keyword_name = \''.word.'\'';
                          }
                  }
                  
                  query .= '
                          )
                  )
          ;';
  
                  //echo '<pre>',query,'</pre>';
  
                  result = this->core->Db->getPlainQueryResult(query);
                  
                  if(!result) {
                          //echo('invalid query, ignoring...<br />');
                  }
                  else {
                          while(row = mysql_fetch_array(result, MYSQL_NUM)) {
                                  if(in_array(row[0],found_ids)) {
                                          //echo 'found duplicate search result with ID ',row[0],', ignoring...<br />';
                                  }
                                  else {
                                          results[] = row;
                                          found_ids[] = row[0];
                                  }
                          }
                          //echo '<pre>';
                          //var_dump(results['fullstring_keywords']);
                          //echo '</pre>';
  
                          mysql_free_result(result);
                          
                  }
                  
                  return results;
          }
  
          function doAdvancedSearch() {
                  search_fields = array("title", "description", "keywords", "matchconditions", "beforedd", "beforemm", "beforeyyyy", "afterdd", "aftermm", "afteryyyy", "sortby");
                  sortby_options = array("relevance" => "Relevance", "datedes" => "Date (newest first)", "dateasc" => "Date (oldest first)");
  
                  foreach(search_fields as search_field) {
                          if(isset(_GET[search_field]) && !empty(_GET[search_field])) {
                                  //echo "found ",search_field," ";
                                  $$search_field = _GET[search_field];
                                  search_fields_used[] = search_field;
                          }
                  }
  
                  if(isset(beforedd) && isset(beforemm) && isset(beforeyyyy)) {
                          if(!this->checkValidDate(beforedd,beforemm,beforeyyyy)) {
                                  //echo "Invalid 'before' date; ignored...<br />";
                          }
                          else beforedate = beforeyyyy."-".beforemm."-".beforedd;
                  }
  
                  if(isset(afterdd) && isset(aftermm) && isset(afteryyyy)) {
                          if(!this->checkValidDate(afterdd,aftermm,afteryyyy)) {
                                  //echo "Invalid 'after' date; ignored...<br />";
                          }
                          else afterdate = afteryyyy."-".aftermm."-".afterdd;
                  }
  
                  // bouw query op
  
                  query = this->buildAdvancedQuery(title,description,keywords,matchconditions,beforedate,afterdate,sortby);
  
                  //echo '<pre>',query,'</pre>';
  
                  // maak verbinding en voer query uit
  
                  result = this->core->Db->getPlainQueryResult(query);
  
                  if(!result) {
                                  //echo('invalid query, ignoring...<br />');
                  }
                  else {
                          while(row = mysql_fetch_array(result, MYSQL_NUM)) {
                                  results[] = row;
                          }
                          //echo '<pre>';
                          //var_dump(results);
                          //echo '</pre>';
  
                          mysql_free_result(result);
                  }
                  
                  return results;
          }
          
          function checkValidDate(d,m,y) {
                  if((is_numeric(d) && strlen(d) <= 2) && (is_numeric(m) && strlen(m) <= 2) && (is_numeric(y) && strlen(y) <= 4)) {
                          if(mktime(0,0,0,m,d,y) >= mktime(0,0,0,1,1,1970) && mktime(0,0,0,m,d,y) < mktime(0,0,0,1,1,2020)) {
                                  return true;
                          }
                  }
                  return false;
          }
  
          function buildAdvancedQuery(title,description,keywords,matchconditions,beforedate,afterdate,sortby) {
  
                  query = 'SELECT content_id,content_title,content_binary FROM content WHERE ';
  
                  if(isset(title)) {
                          searchwords = this->getSearchWords(title);
                          searchstring = implode(" ", searchwords);
  
                          query .= 'MATCH content_title AGAINST (\'';
                          
                          query .= '>"'.searchstring.'"';
  
                          foreach(searchwords as word) {
                                  query .= ' +'.word;
                          }
  
                          query .= '\' IN BOOLEAN MODE) 
                          ';
  
                  }
  
                  if(isset(title) && isset(description)) {
                          query .= '
                          ';
                          if(matchconditions == "all") {
                                  query .= 'AND ';
                          }
                          else {
                                  query .= 'OR ';
                          }
                  }
  
                  if(isset(description)) {
                          searchwords = this->getSearchWords(description);
                          searchstring = implode(" ", searchwords);
  
                          query .= 'MATCH content_binary AGAINST (\'';
                          
                          query .= '>"'.searchstring.'"';
  
                          foreach(searchwords as word) {
                                  query .= ' +'.word;
                          }
  
                          query .= '\' IN BOOLEAN MODE)
                          ';
                  }
  
                  if(isset(description) && isset(keywords)) {
                          query .= '
                          ';
                          if(matchconditions == "all") {
                                  query .= 'AND ';
                          }
                          else {
                                  query .= 'OR ';
                          }
                  }
  
                  if(isset(keywords)) {
                          searchwords = getSearchWords(keywords);
                          
                          query .= 'content.content_id
                          IN (
                                  SELECT content_id
                                  FROM content_keywords
                                  WHERE content_keywords.keyword_id
                                  IN (
                                          SELECT keyword_id
                                          FROM keywords
                                          WHERE keywords.keyword_name = \''.searchwords[0].'\'';
                                          
                          if(count(searchwords) > 1) {
                                  array_shift(searchwords);
                                  foreach(searchwords as word) {
                                          query .= ' OR WHERE keywords.keyword_name = \''.word.'\'';
                                  }
                          }
                          
                          query .= '
                                  )
                          )
                   ';
                  }
  
                  if(isset(beforedate)) {
                          query .= 'AND content_date_added < \''.beforedate.' 00:00:00\' ';
                  }
  
                  if(isset(afterdate)) {
                          query .= 'AND content_date_added > \''.afterdate.' 00:00:00\' ';
                  }
  
                  if(isset(sortby)) {
                          if(sortby == "datedes") {
                                  query .= 'ORDER BY content_date_added DESC';
                          }
                          elseif(sortby == "dateasc") {
                                  query .= 'ORDER BY content_date_added';
                          }
                  }
                  
                  //echo '<pre>',query,'</pre>';
                  
                  return query;
                  
          }
  
          
  }
  
  ?>


(C) A. Eliëns 2/9/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.