I'm writing up a search engine with JS and jQuery rather than resulting to my PHP database but i'm having an issue understanding how I will construct the 'query'.
Currently, the following steps are taken to fill the array of objects.
1) PHP backend develops a JSON file with all holidays in a specific database.
2) The Javascript frontend pulls this JSON and adds all of it to an array through a loop.
3) The user has a few boxes where they can search the array with and all the results are added to a 'results' array which is updated accordingly each time.
One issue i'm having is; how do I deal with multiple if clauses? For example, if both search_time and search_state != "all", I would need to narrow the search down to only include objects where search_time AND search_state values are met. Currently the query is an OR query.
I come from a background of SQL so approaching Javascript like search is a bit different for me, any help would be appreciated.
Javascript search below:
for (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 six = 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 three = 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;
}
}