我正努力使我的搜索功能正常工作。它确实起作用,但是结果不正确。例如,我搜索“数据”一词,并显示“计算机程序员”。它假定显示“数据分析器”。
if (isset($_POST['submit-search'])) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
// $sql = 'SELECT * FROM professional, job, location WHERE J.JOB_ID = P.PROFESSIONAL_ID AND P.PROFESSIONAL_ID = L.LOCATION_ID AND PROFESSIONAL_NAME LIKE '%$search%'';
$sql = "SELECT * FROM professional, job, location WHERE JOB_NAME LIKE '%$search%' AND PROFESSIONAL_ID=JOB_ID AND JOB_ID = LOCATION_ID " ;
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$new_result = $row["JOB_NAME"];
function str_contains($phrase, $word) {
return strpos($phrase, $word) !== false;
}
if (str_contains($new_result,'computer') || str_contains($new_result, 'programmer') || str_contains($new_result, 'computer programmer')) {
echo "
<div class= 'expertise'>
<h2><i class='fa fa-briefcase' aria-hidden='true'></i><a href='computer_programmer.html'> Computer Programmer</a></h2>
</div>";
} else if (str_contains($new_result, 'data') || str_contains($new_result, 'analyst') || str_contains($new_result, 'data analyst')) {
echo "
<div class= 'expertise'>
<h2><i class='fa fa-briefcase' aria-hidden='true'></i><a href='data_analyst.html'> Data Analyst</a></h2>
</div>";
} else {
echo 'There are no results matching your search!';
}
}
答案 0 :(得分:1)
您的if
陈述当前不正确。当前,您的表达式将始终求值为true
,因为非空字符串被认为是真实的。因此,您的if语句看起来更像:
if ($result == '%computer%' || true || true) {
上面的语句将始终在if
内运行代码,因为表达式将始终为true(与X ∨ T ≡ T
相同)。
相反,您需要重新检查$ result的相等性:
if ($result == '%computer%' || $result == '%programmer%' || $result == '%computer programmer%') {
// do "Computer Programer" code...
} else if ($result =='%data%' || $result == '%analyst%' || $result == '%data analyst%') {
// do "Data Analyst" code...
} else {
// do "no results code"...
}
但是,要使其正常工作,您需要确保$result
是您认为的样子,而不是mysqli_result
对象
编辑:
要检查字符串是否包含computer
或analyst
等...您不能像在此处那样使用SQL通配符。相反,您可以使用PHP方法strpos
。我建议您构建一个辅助函数,该函数将使您可以在if语句中整齐地执行此操作:
function str_contains($phrase, $word) {
return strpos(strtolower($phrase), $word) !== false;
}
// then in your if statement:
if (str_contains($result,'computer') || str_contains($result, 'programmer') || str_contains($result, 'computer programmer')) {
// do "Computer Programer" code...
} else if (str_contains($result, 'data') || str_contains($result, 'analyst') || str_contains($result, 'data analyst')) {
// do "Data Analyst" code...
} else {
// do "no results code"...
}
答案 1 :(得分:1)
您在代码中使用了$ result错误 见
if (isset($_POST['submit-search'])) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
// $sql = 'SELECT * FROM professional, job, location WHERE J.JOB_ID = P.PROFESSIONAL_ID AND P.PROFESSIONAL_ID = L.LOCATION_ID AND PROFESSIONAL_NAME LIKE '%$search%'';
$sql = "SELECT * FROM professional, job, location WHERE JOB_NAME LIKE '%$search%' AND PROFESSIONAL_ID=JOB_ID AND JOB_ID = LOCATION_ID " ;
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result)){
if ($row['JOB_NAME'] == '%computer%' || '%programmer%' || '%computer programmer%')
{
echo "
<div class= 'expertise'>
<h2><i class='fa fa-briefcase' aria-hidden='true'></i><a href='computer_programmer.html'> Computer Programmer</a></h2>
</div>";
}
else if ($row['JOB_NAME'] =='%data%' || '%analyst%' || '%data analyst%'){
echo "
<div class= 'expertise'>
<h2><i class='fa fa-briefcase' aria-hidden='true'></i><a href='data_analyst.html'> Data Analyst</a></h2>
</div>";
}
}
} else {
echo 'There are no results matching your search!';
}
希望这对您有帮助