0

My search results are now printing out each and every entry whenever there is a matched field with my txt file; it's not printing out the filtered result. How can I change my if statements so that I can achieve the narrowing down function? Thank you!

here's my 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); } 
1
  • So you have a pipe delimited file? I would strongly consider using fgetcsv() functionality to more easily read each line into an array. That also makes it to where you don't have to read the whole file into memory like you are currently doing. Just work with one line at a time. Commented Feb 11, 2014 at 1:11

1 Answer 1

1

A couple of tips:

  • You should work with fgetcsv since you have a delimited file.
  • I would also look at placing your matching values into an array for easy comparison. No need to do all those crazy conditionals.
  • Place your output generation in a function or similar reusable section of code rather than write the same line over and over.
  • You probably should have some sort of POST data validation to make sure you are getting data in expected formats (this is not shown in example below).

Putting it all together might look like this:

$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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.