0
我的搜索结果现在正在打印每一个条目,每当有与我的txt文件匹配的字段;它不会打印出过滤结果。我如何更改if语句以便实现缩小功能?谢谢!PHP缩小搜索范围
这里是我的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()'功能来更轻松地将每行读入数组。这也使得它不必像你目前正在做的那样将整个文件读入内存。一次只能使用一行。 –