【问题标题】:PHP narrow down searchPHP缩小搜索范围
【发布时间】:2014-02-11 01:04:41
【问题描述】:

只要我的 txt 文件有匹配的字段,我的搜索结果就会打印出每一个条目;它没有打印出过滤后的结果。如何更改我的 if 语句以实现缩小功能?谢谢!

这是我的 handle_search.php:

 $delimiter = ' | ';
 if(isset($_POST['submit'])){

 $seasonsr = $_POST["seasonsr"];
 $numbersr = $_POST["numbersr"];
 $titlesr = $_POST["titlesr"];
 $directorsr = $_POST["directorsr"];
 $datesr = $_POST["datesr"];

 $file = fopen("park.txt", "r");      

    if (!$file) {
    die("There was a problem opening the park.txt file");
    }

$search = file("park.txt");
$isfound = false;

foreach($search as $find){

    $match = false;
    $explode = explode($delimiter, $find);
    if ($seasonsr == $explode [0] && $_POST['seasonsr'] != "") {
        print $explode [0]. "  " . $explode[1]. "  " . $explode[2]. "  " . $explode[3]. 
"  " .     $explode[4];
        print ("<br/>");
        $isfound = true;
    $match = true;
    }

    if ($numbersr == $explode [1] && $_POST['numbersr'] != "" && !$match) {
        print $explode [0]. "  " . $explode[1]. "  " . $explode[2]. "  " . $explode[3]. 
"  " . $explode[4];
        print ("<br/>");
        $isfound = true;
        $match = true;
    }

    if ($titlesr == $explode [2] && $_POST['titlesr'] != "" && !$match) {
        print $explode [0]. "  " . $explode[1]. "  " . $explode[2]. "  " . $explode[3]. 
"  " . $explode[4];
        print ("<br/>");
        $isfound = true;
        $match = true;
    }


    if ($directorsr == $explode [3] && $_POST['directorsr'] != "" && !$match) {
        print $explode [0]. "  " . $explode[1]. "  " . $explode[2]. "  " . $explode[3]. 
"  " . $explode[4];
        print ("<br/>");
        $isfound = true;
        $match = true;
    }

$itemdate= $explode[4];
    //print("<p>search: $datesr item: $itemdate match: </p>");
    if (trim($datesr) == trim($explode [4]) && $_POST['datesr'] != "" && !$match) {
        print $explode [0]. "  " . $explode[1]. "  " . $explode[2]. "  " . $explode[3]. 
"  " . $explode[4];
        print ("<br/>");
        $isfound = true;
        $match = true;
    }

}
if(!$isfound){
    echo ("Sorry! No search found.");
}
}
fclose($file);
}

【问题讨论】:

  • 所以你有一个管道分隔的文件?我强烈考虑使用fgetcsv() 功能更轻松地将每一行读入数组。这也使得您不必像当前那样将整个文件读入内存。一次只处理一行。

标签: php file search


【解决方案1】:

几个提示:

  • 您应该使用 fgetcsv,因为您有一个分隔文件。
  • 我还会考虑将您的匹配值放入一个数组中以便于比较。不需要做所有那些疯狂的条件。
  • 将您的输出生成放在一个函数或类似的可重用代码部分中,而不是一遍又一遍地编写同一行。
  • 您可能应该进行某种 POST 数据验证,以确保您获取的数据符合预期格式(这在下面的示例中未显示)。

把它们放在一起可能看起来像这样:

$delimiter = ' | ';
// array of POST field names you are interested in
$match_fields = array(
    'seasonr',
    'numbersr',
    'titlesr',
    'directorsr',
    'datesr'
);
// array to store POSTed values
$match_values = array();

if(isset($_POST['submit'])){
    // load POSTed values into match array
    foreach($match_fields as $i => $field) {
        $match_values[$i] = null;
        if(!empty($_POST[$field])) {
            $match_values[$i] = $_POST[$field];
        }
    }

    // open file to read
    $file = fopen("park.txt", "r");      

    if (!$file) {
        die("There was a problem opening the park.txt file");
    }

    $isfound = false;
    // $match = false; -- not used as this seems same as $isfound

    // go through file one line at a time
    while($line = fgetcsv($file, 0, $delimiter)) {
        for($i = 0; $i < count($line); $i++) {
            if($line[i] === $match_values[$i]) {
                $isfound = true;
                print_line($line);
                break;
            }
        }
    }

    fclose($file);

    if (false === $isfound) {
        echo 'No results found';
    }
} else {
    // no POST was made do something else
}

function print_line($line) {
    $string = '';
    foreach($line as $item) {
        $string .= $item . ' ';
    }
    rtrim($string);
    $string .= '<br/>';
    echo $string;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多