Jump to content

Search By Key Word.


lilmer

Recommended Posts

I've got this data.

- - - - - - - - - - - - -
| ID | content           |
- - - - - - - - - - - - -
| 1  | Hand Some         |
- - - - - - - - - - - - -
| 2  | Big Apple         |
- - - - - - - - - - - - -
| 3  | Green Day         |
- - - - - - - - - - - - -

If I search data by "Some Hand", then it must show "Hand Some" cause it is on the list.  I've try LIKE "%variable%" but it since it still only search for specific keyword that will match the data on a field.
 

Can you please give an idea on what is the best way to filter data with my example

Link to comment
https://forums.phpfreaks.com/topic/284346-search-by-key-word/
Share on other sites

You can use Full Text searching - preferred method. This involves more than I would be willing to try and explain in a forum post. there are plenty of tutorials out there you can look up. Or, you can roll your own. Take the input and split it by spaces, then use those values to build the query.

 

 

$searchString = $_POST['search_string'];
$searchWords = array_filter(explode(' ', $searchString));
$WHERE_PARTS = array();
foreach($searchWords as $word)
{
    $WHERE_PARTS[] = "content LIKE '%{$word}%'";
}
 
$query = "SELECT * FROM table_name WHERE  " . implode (' AND ', $WHERE_PARTS);
Link to comment
https://forums.phpfreaks.com/topic/284346-search-by-key-word/#findComment-1460462
Share on other sites

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.