JHsearch ($ search_string)

  Type: code fragment Version: version 1.0 Requires: no_search_words.txt, no_search_tables.txt 

  <? PHP 
  /************************************************* The following 
  JHsearch () was written by James R Hartford 
  JHsearch () may be used and modified freely, but I ask that you please note 
  The original author (James R Hartford) and refer to the function by its 
  Original name (JHsearch ()) 
  If you do make changes that I may find useful, please email them to jim@jarzion.com and I will include your name in later versions of JHsearch () 
  Database connection is set up for MySQL but can be changed if needed. 
  ************************************************** **************************/ 

  / / Use these globals to set up your database connection. 
  Global $ dbhost; $ dbhost =''; 
  Global $ dbname; $ dbname =''; 
  Global $ dbuser; $ dbuser =''; 
  Global $ dbpass; $ dbpass =''; 

  /************************************************* ****************** 
  Below is an example of how the JHsearch function can be used and 
  Display the results of the search. You can test for the table name 
  And display the result accordingly for that table. 
  ************************************************** *******************/ 
  JHsearch $ searchResult = ( "test a"); 
  If (count ($ searchResult) <1) ( 
  $ Output .= "No results for search 'test a'"; 
  Else () 
  SR = $ array_keys ($ searchResult); 
  For ($ i = 0; $ i <count ($ SR); $ i + +) ( 
  If ($ SR [$ i] == "site_info "){// this is checking to see if the table name is media 
  For ($ c = 0; $ c <count ($ searchResult [$ SR [$ i]]); $ c + +) ( 
  Echo "Siteinfo:." $ SearchResult [$ SR [$ i]] [$] [c 'description']."< br> "; 
  ) 
  ) 
  If ($ SR [$ i] == "class "){// this is checking to see if the table name is media 
  For ($ c = 0; $ c <count ($ searchResult [$ SR [$ i]]); $ c + +) ( 
  Echo "Class:." $ SearchResult [$ SR [$ i]] [$] [c 'title']."< br> "; 
  ) 
  ) 
  ) 
  ) 

  /*################################################ ############## 
  ################################################## ##############*/ 

  /************************************************* ********** 
  Function: JHsearch ($ search_string) 
  Purpose: search an entire database and return the results in a 
  3 dimensional array. Words found in no_search_words.txt 
  Will not be searched. Table names found in 
  No_search_tables.txt will not be searched. 
  Result example: $ result [table_name] [row] [field_name] 
  Author: James R Hartford 
  ************************************************** **********/ 
  Function JHsearch ($ string) ( 
  $ $ Newstring = string; 
  $ Searchcrit = explode ( "", $ newstring); / / get the words to be searched 
  $ Dontsearchwords = JHgetnos ( "words"); / / get the words not to search into an array 

  / / Get the actual search criteria 
  $ Searchcrit = array_diff ($ searchcrit, $ dontsearchwords); 
  $ Searchcrit = JHarray_reindex ($ searchcrit); 

  / / Get the tables to search 
  $ Query = "Show Tables"; 
  $ Tables = JHRquery ($ query); 
  For ($ i = 0; $ i <count ($ tables); $ i + +) ( 
  $ Gettables [$ i] = $ tables [$ i] [0]; 
  ) 
  $ = $ Gettables tables; 
  $ Tabletakeout = JHgetnos ( "tables"); / / tables to leave out of search 
  $ Tables = array_diff ($ tables, $ tabletakeout); 
  $ Tables = JHarray_reindex ($ tables); 
  $ Resultcount = 0; 

  / / Build the search query 
  For ($ i = 0; $ i <count ($ tables) & count ($ searchcrit)> 0; $ i + +) ( 
  $ Query = "SELECT * FROM"; 
  $ Query .= $ tables [$ i]. "WHERE"; 
  $ Qt = "SHOW FIELDS FROM." $ Tables [$ i]; 
  $ Fields = JHRquery ($ qt); 

  For ($ m = 0; $ m <count ($ fields); $ m + +) ( 
  If (substr ($ fields [$ m] [ 'Type'], 0, 3)! = "Int" & substr ($ fields [$ m] [ 'Type'], 0, 4)! = "Date") ( 
  If ($ m> 0) ( 
  $ Query .= "("; 
  ) 
  For ($ sc = 0; $ sc <count ($ searchcrit); $ sc + +) ( 
  $ Query .= $ fields [$ m] [ 'Field']. "LIKE'%".$ searchcrit [$ sc ]."%'"; 
  If ($ sc! = Count ($ searchcrit) - 1) ( 
  $ Query .= "AND"; 
  ) 
  ) 
  If ($ m> 0 & & $ m <count ($ fields)) ( 
  $ Query .= ") OR"; 
  ) 
  ) 
  ) 
  / / This if statement is for if the query ended in WHERE because the only 
  / / Field types in the table are ints or dates 
  If (substr ($ query, strlen ($ query) - 6, 6)! = "WHERE" | | $ tempResult [$ tables [$ i] [0] [0]! = "") ( 
  $ Query = substr ($ query, 0, strlen ($ query) - 4); 
  / / Echo $ query. "<br> <br>"; 
  $ TempResult [$ tables [$ i]] = JHRquery ($ query); 
  ) 

  If ($ tempResult [$ tables [$ i] [0]! = "") ( 
  $ RealResult [$ tables [$ i]] = $ tempResult [$ tables [$ i]]; 
  ) 
  ) 
  Return $ realResult; 
  ) 

  /************************************************* ******** 
  Function: JHgetnos $ (where); 
  Purpose: Pares a text file to find out what not to search 
  Author: James R Hartford 
  ************************************************** ********/ 
  Function JHgetnos $ (where) ( 

  / / Get the words that should not be searched for 
  If ($ where == "words") ($ filename = "no_search_words.txt";) 
  / / Get the tables that should not be searched 
  Else if ($ where == "tables") ($ filename = "no_search_tables.txt";) 

  $ Handle = fopen ($ filename, "r"); 
  $ Contents = explode ( "", fread ($ handle, filesize ($ filename))); 
  Fclose ($ handle); 

  Return $ contents; 

  ) 

  /************************************************* ******** 
  Function: JHarray_reindex ($ array); 
  Purpose: reindex an array 
  Author: James R Hartford 
  ************************************************** ********/ 
  Function JHarray_reindex ($ somearray) ( 

  $ Keys = array_keys ($ somearray); 
  For ($ i = 0; $ i <count ($ keys); $ i + +) ( 
  $ Result [$ i] = $ somearray [$ keys [$ i]]; 
  ) 
  Return $ result; 
  ) 

  /************************************************* ******** 
  Function: JHRquery ($ sql_string); 
  Purpose: Execute query and return results in a 2d array 
  Author: James R Hartford 
  ************************************************** ********/ 
  Function JHRquery ($ query) ( 

  Global $ dbhost; global $ dbname; global $ dbuser; global $ dbpass; 
  $ Link = mysql_connect ($ dbhost, $ dbuser, $ dbpass); 
  Mysql_select_db ($ dbname) or die ( "Could not select database $ dbhost"); 
  $ Result = mysql_query ($ query, $ link) or die ( "Could not execute query $ query"); 
  $ ResultArray = array (); 
  For ($ i = 0; $ temp = mysql_fetch_array ($ result); $ i + +) ( 
  If ($ temp) ($ resultArray [$ i] = $ temp;) 
  ) 
  Return $ resultArray; 
  ) 
  –> 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “JHsearch ($ search_string)”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.