Forum Moderators: coopster

Message Too Old, No Replies

Filtering out keywords in a search!

help please!

         

dreamcatcher

12:51 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have about 1100 movie titles in a database. I am trying to do a simple search engine and have seperated each keyword using the following code:


$q = $_GET['q'];
if ($q) {
$q = strtolower($q);
$k = split(" ", $q);
$num_keywords = count($k);
for ($i=0; $i<$num_keywords; $i++) {
if ($i) {
$search_string .= "OR moviename LIKE '%" . $k[$i] . "%' "; }
else {
$search_string .= "moviename LIKE '%" . $k[$i] . "%' "; }
} end for
$and .= "WHERE ($search_string) ";
} //end if

$querycount = "SELECT * FROM titles $and";

rest of code...

This works ok, but if I type something in like 'The Killer' it fetches everything with the word 'the' as well.

I have put the words into an array that I want to filter out.


$skipwords = array("the", "of", "and"); etc

Can I use that to filter out the words? I`ve been playing around with the in_array() function, but can`t seem to get it to work like I want it to.

Any help you could give me would be appreciated.

Thank you.

dreamcatcher

4:26 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I`ve been playing around with this all afternoon and I think I`ve got it.


$q = $_GET['q'];
if ($q) {
$q = strtolower($q);
$k = split(" ", $q);
$num_keywords = count($k);
foreach ($k as $d) {
$count=0;
if (!in_array($d, $skipwords)) {
$words[] = $d;
$count=$count+1;
}
}
for ($i=0; $i<$count; $i++) {
if ($i) {
$search_string .= "OR moviename LIKE '%" . $words[$i] . "%' "; }
else {
$search_string .= "moviename LIKE '%" . $words[$i] . "%' "; }
} //end for
$and .= "WHERE ($search_string) ";
} //end if

if ($count>0)
{
$querycount = "SELECT * FROM titles $and";
}

rest of code...

Seems to work ok anyway. :)

dreamcatcher

11:13 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually for anyone who`s interested, the code should have been:

$skipwords = array("the", "and", "is", "are", "to");

$q = $_GET['q'];
if ($q) {
$q = strtolower($q);
$k = split(" ", $q);
$num_keywords = count($k);
$count=0;
foreach ($k as $d) {
if (!in_array($d, $skipwords)) {
$words[] .= $d;
$count=$count+1;
}
}
for ($i=0; $i<$count; $i++) {
if ($i) {
$search_string .= "OR moviename LIKE '%" . $words[$i] . "%' "; }
else {
$search_string .= "moviename LIKE '%" . $words[$i] . "%' "; }
} //end for
$and .= "WHERE ($search_string) ";
} //end if
if ($count>0)
{
$querycount = "SELECT * FROM titles $and";
}

rest of code...

:)

mykel79

4:00 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Also, you can put the keywords in one array, the words to be ignored in a second array and use
array_diff
to delete the words you want ignored from the keyword array. But you won't save more than a couple of lines of code :)