Jump to content

implement search function


xbl1

Recommended Posts

hello;I am trying to implement a search function. But the problem i found my method is not good, just roughly to find out the result. for example, when i type a string with the "a man', it will fail here to find match.Could any one help to impove it, thanks. you can click the My Webpage to see my search function.There are 5 sentence in database. and i will compaire the input text with these 5 sentences one by one, and see which one match more, then list then out by decrease order with how many time they match.and the 5 sentences are 1) i am looking for a girl2) i want a job3) want want4) want ' job 5) i want a manfor instance;input text is want, and it compair with the sentence 3 of 'want want'.then i say here has one match, if i find one match, i will stop to compair.

<?php  session_start();     $st=$_REQUEST["SText"];   $_SESSION["st"]=$st;	?></head><body> <br>There are 5 sentence in database. and i will compaire the input text with these 5 sentences one by one, and see which one match more, then list then out by decrease order and with the number of match.<br><br><br> and the 5 sentences are <br>1) i am looking for a girl2) i want a job3) want want4) want ' job 5) i want a man<br><br>	   <?php   	   	$con = mysql_connect("localhost", "xxxx","xxxxx");  if (! $con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db("xxxxx", $con);	   $result = mysql_query("select * from Customer2");	  // was it a success  if (!$result)	die ("Error processing request - check your query");   $f=0;     // process results  $nrecs = mysql_num_rows($result);       // echo "<table border=0 width=50% class='id1' bgcolor='ccffff'>";   while($row = mysql_fetch_array($result))  {	  echo "<br>";	  echo $row['Des'];	  $Des=$row['Des'];			/* Use tab and newline as tokenizing characters as well  */	 $tok = strtok($st, " ");	 while ($tok !== false) {			 echo "<br>";			 echo "$tok";			 echo "<br>";			$len=strlen($tok);			$C=$tok;		  		   if($len>2){			 $len=$len/2+1;			 $C=substr($tok,0,$len);			}		  // the "i" after the pattern delimiter indicates a case-insensitive search		  if (preg_match("/$C/i", $Des)) {			 $f++;			 $Des=preg_replace("/$C/i", "", $Des);			echo "$C match <b>";			 		   } 		   else			 echo "$C don't match";		   	 		  $tok = strtok(" ");	  } 	  echo "<br>";	 if($f>0){	   mysql_query("update Customer2 set Count=" . "'$f'" . "where Cid=" . $row['Cid']);	 }	 $f=0;		     } // echo "</table>";	$result = mysql_query("select * from Customer2 order by Count DESC");	echo "<table border=0 width=50% class='id1' bgcolor='ccffff'>";	 while($row = mysql_fetch_array($result))	 {		 if($row['Count'] >0){		 echo "<tr> <td width=10%>";		 echo $row['Count'];		 echo "</td><td >";		 echo $row['Des'];		 echo "<hr>";		 echo "</td></tr>";		 }	   }	  echo "</table>";     $f=0;   mysql_query("update Customer2 set Count=" . "'$f'");mysql_close($con);?> </body></html>

Link to comment
Share on other sites

It looks like you are using MySQL. Save yourself some time writing a new searching algoritm by using MySQL's FULLTEXT searching instead.Here's an article explaining it:http://www.onlamp.com/pub/a/onlamp/2003/06/26/fulltext.html
Thanks a lot, that's very good material.But the problem i got from that, the "Full-Text Searches with Query Expansion" does not work, For instance;mysql> SELECT * FROM articles -> WHERE MATCH (title,body) -> AGAINST ('database' WITH QUERY EXPANSION);It should come out with the following result,This example from select from table, and Create a table and insert value to the table.+----+-------------------+------------------------------------------+| id | title | body |+----+-------------------+------------------------------------------+| 1 | MySQL Tutorial | DBMS stands for DataBase ... || 5 | MySQL vs. YourSQL | In the following database comparison ... || 3 | Optimizing MySQL | In this tutorial we will show ... |+----+-------------------+------------------------------------------+but it give me the result as following;+----+-------------------+------------------------------------------+| id | title | body |+----+-------------------+------------------------------------------+| 5 | MySQL vs. YourSQL | In the following database comparison ... || 1 | MySQL Tutorial | DBMS stands for DataBase ... |+----+-------------------+------------------------------------------+And also example searching for books by Georges Simenon about Maigret, when a user is not sure how to spell “Maigret”. It does not work too.Could you tell why? Is the WITH QUERY EXPANSION old stuff ?
Link to comment
Share on other sites

It looks like you are using MySQL. Save yourself some time writing a new searching algoritm by using MySQL's FULLTEXT searching instead.Here's an article explaining it:http://www.onlamp.com/pub/a/onlamp/2003/06/26/fulltext.html
excuse me, may i ask if there is some benefits, for example in performance or flexiability, using MATCH AGAINST over LIKE ?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...