Skip to content Skip to sidebar Skip to footer

Search Words In Any Order Using JS

I have code to search the typed words from the text box, Get the typed word, search it in the web sql database and show the result in the HTML page. In that i can search the word e

Solution 1:

So you want an AND search using each of the words entered, rather than the exact string? Howabout something like this:

var yourSearchText = 'World Hello';
var searchTerms = yourSearchText.split(' ');
var searchTermBits = [];
for(var i=0; i<searchTerms.length;i++) {
        searchTermBits.push("word LIKE %"+searchTerms[i]+"%");
}

var result = searchTermBits.join(" AND ");
var query = 'SELECT * FROM TABLE WHERE '+result;

this will give you a query like:

SELECT * FROM table WHERE word LIKE '%world%' AND word LIKE '%hello%'

change the AND to an OR if you want to match any of the search terms rather than all.


Post a Comment for "Search Words In Any Order Using JS"