Jump to content

Search By Key Word.


lilmer
Go to solution Solved by Psycho,

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.