我用JS和jQuery编写了一个搜索引擎而不是我的PHP数据库,但是我在理解如何构建查询时遇到了问题。
目前,采取以下步骤来填充对象数组。
1)PHP后端开发了一个JSON文件,其中包含特定数据库中的所有假期。
2)Javascript前端提取此JSON并通过循环将所有内容添加到数组中。
3)用户有几个框可以搜索阵列,并将所有结果添加到“结果”中。每次都相应更新的数组。
我遇到的一个问题是;我该如何处理多个if子句? 例如,如果search_time和search_state!=" all",我需要将搜索范围缩小到仅包含满足search_time和search_state值的对象。目前,查询是OR查询。
我来自SQL的背景,所以接近像搜索这样的Javascript对我来说有点不同,任何帮助都会受到赞赏。
下面的Javascript搜索:
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;
}
}
答案 0 :(得分:0)
这种方式可以让你:
1)添加更多的字段比较(阅读标有数字4.a和4.b的项目,你可以添加4.c,4.d等...,试图保持清晰)。
2)允许您选择AND或OR模式(带灵敏度)
3)使用jQuery为您节省大量时间和代码。
4)提供strstr函数,读取脚本部分以获取注释。原始函数(链接)返回一个字符串,我通过返回null而不是“No match”字符串对其进行修改。
// 1. lets setup a database, you can pull it from PHP or some other else..
//
var holidays = [
{ title : "aaskjqkwqiuwqi" , state : "florida" },
{ title : "aaaaksjak222jski" , state : "california" },
{ title : "1281827888282" , state : "california" },
{ title : "aksjakjkas88112" , state : "florida" }
];
// 2. lets define some inputs
//
var i_find = "88"; // what to find
var i_state = "all"; // what to find
// 3. define some internal conditions.
//
var result_type = 'AND'; // set to: AND or OR
var at_least = 2; // when OR, at least N items to have a match..
var results = [];
// 4. for each holiday entries we will count how many conditions match,
// and depending on the search modality (AND or OR) then we put results
//
$(holidays).each(function(index,obj){
var matches = [];
// 4.a find by title and say if it success or not
matches.push({ res: strstr(obj.title, i_find) ? true : false });
// 4.b find by state, and again, say if it 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
// we will 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 the search model: AND 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. we have results, if 'results' has entries.
console.log('Search Type: '+result_type);
results.length ? console.log('WE HAVE A MATCH FOR:'
+i_find+' please examine results array')
: console.log('NO MATCH');
if(results.length) console.log(results);
// 7. have a 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 i = 0,
tempLength = 0,
temp = [];
for (;;) {
if (haystack[i] === undefined || needle == null) {
return null;
}
//if the char doesn't match then reset
else if (haystack[i] !== needle[tempLength]) {
temp = [];
tempLength = 0;
}
//the char matches so let's store it.
else if (haystack[i] === needle[tempLength]) {
temp[tempLength] = haystack[i];
if (needle[tempLength + 1] === undefined) {
return temp;
}
tempLength++;
}
i++;
}
};
</script>