jquery - Javascript Search Engine AND clause -
i'm writing search engine js , jquery rather resulting php database i'm having issue understanding how construct 'query'.
currently, following steps taken fill array of objects.
1) php backend develops json file holidays in specific database.
2) javascript frontend pulls json , adds of array through loop.
3) user has few boxes can search array , results added 'results' array updated accordingly each time.
one issue i'm having is; how deal multiple if clauses? example, if both search_time , search_state != "all", need narrow search down include objects search_time , search_state values met. query or query.
i come background of sql approaching javascript search bit different me, appreciated.
javascript search below:
(var i=0; (i <= holidays.length) && (found < limit); i++) { var h = holidays[i]; console.log(h); complete = false; while (!complete && (h != undefined)) { if (search_terms != "" && search_terms != undefined) { if (like(h.title, search_terms) || like(h.state, search_terms) || like(h.country, search_terms) || like(h.location, search_terms)) { results[found] = h; found += 1; complete = true; } } if (search_country != "all") { if (h.country != undefined) { if (like(h.country, "australia") && !complete) { results[found] = h; found += 1; complete = true; } } } if (search_state != "all") { if (like(h.state, search_state) && !complete) { results[found] = h; found += 1; complete = true; } } if (search_time != "all") { var cyear = new date().getfullyear(); var nyear = cyear + 1; if (search_time == 'n-year' && !complete) { if (h.startsyd != undefined) { if (new date(h.startsyd).getfullyear() >= nyear) { results[found] = h; found += 1; complete = true; } } else if (h.melbstart != undefined) { if (new date(h.melbstart).getfullyear() >= nyear) { results[found] = h; found += 1; complete = true; } } } else if (search_time == 'c-year' && !complete) { if (h.startsyd != undefined) { if (new date(h.startsyd).getfullyear() >= cyear && new date(h.startsyd).getfullyear() < nyear) { results[found] = h; found += 1; complete = true; } } else if (h.melbstart != undefined) { if (new date(h.melbstart).getfullyear() >= cyear && new date(h.melbend).getfullyear() < nyear) { results[found] = h; found += 1; complete = true; } } } else if (search_time == '6-months' && !complete) { var 6 = new date().setmonth(this.getmonth() + 6); if (h.startsyd != undefined) { if (new date(h.startsyd <= six)) { results[found] = h; found += 1; complete = true; } } else if (h.melbstart != undefined) { if (new date(h.melbstart <= six)) { results[found] = h; found += 1; complete = true; } } } else if (search_time == '3-months' && !complete) { var 3 = new date().setmonth(this.getmonth() + 3); if (h.startsyd != undefined) { if (new date(h.startsyd <= three)) { results[found] = h; found += 1; complete = true; } } else if (h.melbstart != undefined) { if (new date(h.melbstart <= three)) { results[found] = h; found += 1; complete = true; } } } } complete = true; } }
this way allows to:
1) add more field comparisons (read items marked number 4.a , 4.b, can add 4.c, 4.d etc.., trying keep things clear).
2) allows select in , or or modes (with sensitivity)
3) save lot of time , code using jquery.
4) strstr function provided, read scripts sections comments. original function (link) returns string, make modification returning null instead of "no match" string.
// 1. lets setup database, can pull php or other else.. // var holidays = [ { title : "aaskjqkwqiuwqi" , state : "florida" }, { title : "aaaaksjak222jski" , state : "california" }, { title : "1281827888282" , state : "california" }, { title : "aksjakjkas88112" , state : "florida" } ]; // 2. lets define inputs // var i_find = "88"; // find var i_state = "all"; // find // 3. define internal conditions. // var result_type = 'and'; // set to: , or or var at_least = 2; // when or, @ least n items have match.. var results = []; // 4. each holiday entries count how many conditions match, // , depending on search modality (and or or) put results // $(holidays).each(function(index,obj){ var matches = []; // 4.a find title , if success or not matches.push({ res: strstr(obj.title, i_find) ? true : false }); // 4.b find state, , again, if success or not if("all" != i_state) matches.push({ res: strstr(obj.state, i_state) ? true : false }); // 4.c more search attributes ? add them here // 5. process results // count how many questions (4a,4b..) success: var n_matches = 0; $(matches).each(function(i,o){ if(true == o.res) n_matches++; }); // 5.b return results depending on search model: , or or, // if(('and' == result_type) && (n_matches == matches.length)) results.push(obj); if(('or' == result_type) && (n_matches > at_least)) results.push(obj); }); // 6. have results, if 'results' has entries. console.log('search type: '+result_type); results.length ? console.log('we have match for:' +i_find+' please examine results array') : console.log('no match'); if(results.length) console.log(results); // 7. have nice day. :)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <script> /* taken from: https://stackoverflow.com/a/9123997/937815 */ function strstr (haystack, needle) { var = 0, templength = 0, temp = []; (;;) { if (haystack[i] === undefined || needle == null) { return null; } //if char doesn't match reset else if (haystack[i] !== needle[templength]) { temp = []; templength = 0; } //the char matches let's store it. else if (haystack[i] === needle[templength]) { temp[templength] = haystack[i]; if (needle[templength + 1] === undefined) { return temp; } templength++; } i++; } }; </script>
Comments
Post a Comment