Forum Moderators: coopster

Message Too Old, No Replies

search function implement

search function implement

         

xbl01234

12:31 pm on Dec 11, 2006 (gmt 0)

10+ Year Member



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.

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 girl
2) i want a job
3) want want
4) want ' job
5) i want a man

for 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.

CODE<?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 girl
2) i want a job
3) want want
4) want ' job
5) i want a man
<br><br>

<code>
<?php

$con = mysql_connect("localhost", "#*$!x","#*$!xx");
if (! $con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("#*$!xx", $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>

</code>

[edited by: dreamcatcher at 3:03 pm (utc) on Dec. 11, 2006]
[edit reason] No personal links, thanks. [/edit]

eelixduppy

2:06 am on Dec 12, 2006 (gmt 0)



If you are up to reading, here's some related threads:
[webmasterworld.com...]
[webmasterworld.com...]
search [google.com]

Best of luck.

restless

2:57 am on Dec 12, 2006 (gmt 0)

10+ Year Member



Try creating a "full-text index" in your search table it is an index of type FULLTEXT

Basically you just choose the columns you want to match the query text on and create an Index on those columns. Then when you do a "match" query using your search text mysql will give you results of the closest matching rows.

There's lots of info and good examples on this just google "mysql search functions index match"