Im trying to make a search engine on key up to return results from DB but also if phrase has been typed to pull results from object not from DB
For example : when you type "BMW 300" will pull data from DB and will save phrase and results in object , and object will look like this
object {
b: (4) […]
0: Object { name: "BMW"}
1: Object { name: "BMW 300"}
2: Object { name: "BMW XS"}
3: Object { name: "BMW Z 500"}
bm: (4) […]
0: Object { name: "BMW"}
1: Object { name: "BMW 300"}
2: Object { name: "BMW XS"}
3: Object { name: "BMW Z 500"}
bmw: (4) […]
0: Object { name: "BMW"}
1: Object { name: "BMW 300"}
2: Object { name: "BMW XS"}
3: Object { name: "BMW Z 500"}
"bmw 3": (1) […]
0: Object { name: "BMW 300"}
"bmw 30": (1) […]
0: Object { name: "BMW 300"}
"bmw 300": (1) […]
0: Object { name: "BMW 300"}
}
if you delete 300 phrase is become "BMW" now should pull results from object because phrase "BMW" has been typed .
I code both logic and they work perfect , but the problem is how to swap when to read from object and when to read from DB ?
// create object
var hold_results = {};
$('input[name="search"]').on('keyup', function(){
var phrase = jQuery.trim($(this).val());
if (phrase.length > 0) {
$.ajax({
type: "POST",
url: 'index.php?route=product/newsearch',
data: 'phrase='+phrase,
success: function(data) {
if (data.length != 0) {
// push phrase and currect results in object
hold_results[phrase] = data;
$.each(data , function(key, value){
if (value != '') {
html = '<li><a href="'+value.href+'">'+value.name+'</a></li>';
$('#search-results').append(html);
}
});
}
}
});
}
// should pull results from object here if phrase exist
$.each(hold_results , function(key , value){
if (key == phrase) {
$.each(value, function(k , v){
html = '<li><a href="'+v.href+'">'+v.name+'</a></li>';
$('#search-results').append(html);
})
}
});
});