>我正在构建一个字典,并希望限制用户可以获得的结果数量。在我的 HTML 页面中,我有一组单选按钮,用于定义用户可以限制搜索的范围。1 = 简单,2 = 简单,3 = meduim,4 = 困难,7 = 全部。这是由附加到条目的数据库值"难度"定义的。我正在尝试使用以下代码限制搜索,但它没有返回任何结果。$search_limit 是无线电值。
if(strcmp($search_limit, '1'))
{
$sql .= ') and (Difficulty is null or Difficulty = 1) and rownum<=500' . $search_order;
}
elseif (strcmp($search_limit, '2'))
{
$sql .= ') and (Difficulty >= 1 or Difficulty <= 3) and rownum<=500' . $search_order;
}
elseif (strcmp($search_limit, '3'))
{
$sql .= ') and (Difficulty >= 4 or Difficulty <= 6) and rownum<=500' . $search_order;
}
else
{
$sql .= ') and (Difficulty <= 7) or rownum<=500' . $search_order;
}
我遇到的问题是(难度>= x 或难度 <= x)。如果我将搜索限制默认为 else 语句,它会正确返回难度为 7(最大值)及以下的所有内容。同样,如果我将 else 语句中的 7 替换为 7 以下的任何其他数字,它也可以正常工作。我只是无法使定义的搜索起作用。
搜索范围时,应使用 x >= y AND x <= z
...否则,如果您使用OR
您将返回所有内容。
所以改变...
Difficulty >= 1 or Difficulty <= 3
自。。。
Difficulty >= 1 and Difficulty <= 3
此外,您的else
具有不同的逻辑,因为当所有其他逻辑都and rownum<=500
时,它or rownum<=500
难度>= 1 或难度 <= 3
这始终是正确的(当难度为空时除外):每个数字都大于 1 或小于 3。
你一定是故意的
Difficulty >= 1 AND Difficulty <= 3