简体   繁体   English

PHP的高级搜索功能

[英]Advanced Search Function with PHP

currently I have a form that allows users to fill in 3 input fields. 目前,我有一个表格,允许用户填写3个输入字段。 3 of them are text fields (title, author and ISBN) and 1 of it is a select option (categories). 其中3个是文本字段(标题,作者和ISBN),其中1个是选择选项(类别)。

What I would like to achieve is to allow users to fill in any number of the 4 fields, and return the respected values. 我想要实现的是允许用户填写4个字段中的任意一个,并返回受尊重的值。 This means that if users fills in 2 input fields, there will be 2 conditions to check in the backend. 这意味着,如果用户填写2个输入字段,则将有2个条件要检查后端。 3 inputs filled means 3 conditions. 3输入满足3个条件。

What I have currenly is this (an example, not the actual code itself): 我目前正对此(示例,而不是实际的代码本身):

    if($title == $allMyArray["title"]){
        array_push($returningResult, $allMyArray);
    }else if($author == $allMyArray["author"]){
        array_push($returningResult, $allMyArray);
    }else if($ISBN == $allMyArray["ISBN"]){
        array_push($returningResult, $allMyArray);
    }else if($categoreis== $allMyArray["categories"]){
        array_push($returningResult, $allMyArray);
    }else{
        echo "nothing";
    }

This set of code works when I only fill in one specific field. 当我仅填写一个特定字段时,这组代码有效。 For example if I fill in only the author input, and leave the other 3 options blank, I will be returned wwith the values I want. 例如,如果我仅填写作者输入,而其他3个选项留空,则将返回我想要的值。 However, if I attempt to fill in 2 or more fields at once, the returned value is incorrect. 但是,如果我尝试一次填写2个或更多字段,则返回的值不正确。

So how else can I come up with an if else statement that will check which fields are filled, and set the conditions correctly based on the inputted fields? 那么,我还能如何提出一个if else语句来检查填充了哪些字段,并根据输入的字段正确设置条件?

Thank you all for your help in advanced! 谢谢大家对高级的帮助! Much appreciated! 非常感激! :) :)

Changing to below code will work. 更改为以下代码将起作用。 The problem is when you use ELSE you are limiting your conditional statements to one result only. 问题是当您使用ELSE时,您会将条件语句限制为仅一个结果。

Optionally, you can use switch/case statement if you find IF dirty. (可选)如果发现IF不干净,则可以使用switch / case语句。

But it may produce duplicates, so you need to work this out. 但是它可能会产生重复,因此您需要解决这个问题。 (i dont know your code, it is just an assumption) (我不知道您的代码,这只是一个假设)

$atLeast1Result = false;
if($title == $allMyArray["title"]){
    array_push($returningResult, $allMyArray);
    $atLeast1Result = true;
}
if($author == $allMyArray["author"]){
    array_push($returningResult, $allMyArray);
    $atLeast1Result = true;
} 
if($ISBN == $allMyArray["ISBN"]){
    array_push($returningResult, $allMyArray);
    $atLeast1Result = true;
}
if($categoreis== $allMyArray["categories"]){
    array_push($returningResult, $allMyArray);
    $atLeast1Result = true;
}
if(!$atLeast1Result ) {
    echo "nothing";
} else {
    $returningResult = array_unique($returningResult); // this might now work on all versions, as i dont know what this array is.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM