Jump to content

[SOLVED] MySQL Search Engine problem...


gabarab

Recommended Posts

Hello. I am currently working on a website, and it needs a search engine.

The current code i am using is:

 

if(isset($_REQUEST['search_msgs'])) {
   if(isset($_REQUEST['searchall'])) { 
$searching = strtoupper($_POST['searching']);
$searching = strip_tags($searching);
$searching = trim ($searching); 
$query = "select * from ****** where upper(message) like '%$searching%' order by posted DESC";
}
   else {
$searching = strtoupper($_POST['searching']);
$searching = strip_tags($searching);
$searching = trim ($searching); 
$select_category=$_POST['select_category'];
$query = "select * from ****** where message like '%$searching%' AND category='$select_category' order by posted DESC";
}
}
else {
$query="SELECT * FROM ****** where category='$showall' order by posted DESC";

$getmsg = mysql_query("$query") or die(mysql_error()

 

The code works perfectly for single word search..."word"

but i need to add the possibility to search by multiple words.

Now, if I search for "some word" the search will be made only for "word"...

I know it should be something that will include explode()  but i don't know how to add to the query every word inserted.

Can someone please help me with my problem?

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/118967-solved-mysql-search-engine-problem/
Share on other sites

if you search for "LIKE '%some word%' " every row that is fetched should have "some word" in it. Are you saying that if a user types "some word" that you want it to search for both "some" and "word" separately? Or do you wanna just make sure the rows searched have both "some" and "word" but not necessarily next to eachother?

if you search for "LIKE '%some word%' " every row that is fetched should have "some word" in it. Are you saying that if a user types "some word" that you want it to search for both "some" and "word" separately? Or do you wanna just make sure the rows searched have both "some" and "word" but not necessarily next to each other?

I haven't worked with foreach() so far... did not need it, i guess...

I want to search for both some and word from the input, but, as you said, not necessarily next to each other.

my problem is that i haven't the slightest idea on how to make it.

I suppose that

foreach($keywords as $keyword){
     $sql .=  " AND  keyword LIKE '%$keyword%'";
}

would work...i just don't know how to implement it in to my script :(

I would think that you just need to add the keywords to an array then loop over it as I previously suggested.

 

the preg_split() function might be worth looking at is it will split the keywords for you based on a regex and return an array.  Loop over the array and you're in business.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.