0

In my html I have a search function that when the submit button is clicked a PHP function is ran that is supposed to filter out rows that do not contain the searched word and only the rows in the HTML table that contained the searched word display on the page. My HTML table is being generated using PHP which is reading books from a .txt file. My code brings back the rows from the text file instead of filtering the table results. Any help please?

HTML

<form action="search.php" method="POST">
    <p>Enter Word to Search</p>
    <p>
        <input type="text" name="search_term"/> 
    <input type="submit" value="Search"/>    
    </form>

PHP

 $search_term = $_POST['search_term'];

            foreach($books as $book){
              $book_formatted = str_replace('|', '', $book);

              $pos = stripos($book_formatted, $search_term);       

            if($pos === false){
                print "Not found";
            }else{
              $book_formatted = substr($book_formatted, 0, $pos);
              $book_formatted = trim($book_formatted);

              $pos2 = stripos($book_formatted, $search_term);

              if($pos2 === false){
                  print "Not found in the list";
              }else{
                  print "Titles that match: ". $book_formatted;
                }
               }
              }

1 Answer 1

2

Without actually seeing what $books or even $book look like, it seems every search would fail because of this:

$book_formatted = substr($book_formatted, 0, $pos);

If your book was "ABC Book" and your search was "Bo", the first stripos would give you a result of 4, which is not false so it moves to your else section. But then you sub-string the book name again, chopping the name down to just "ABC " and perform another search for "Bo" which would then usually fail.

Perhaps you were trying to get the search term and everything after it? in that case just use

$book_formatted = substr($book_formatted, $pos);

which would return "Book". In that case the second search is meaningless, because it would always return 0. Unless there's more that is not provided, it should work if you just format the book name however you want and print it in the first else statement instead of doing a second stripos.

EDIT:

somewhere on your page you probably have a loop printing the table rows:

foreach($books as $book){
    ...
    print "<tr><td>" . $book . "</td></tr>";
    ...
}

basically copy all of your code into that loop around that print line like this

 $search_term = $_POST['search_term'];
 ...
 // this should be the loop on your page that is printing the contents of the table
 foreach($books as $book){

     ...

     $book_formatted = str_replace('|', '', $book);
     $pos = stripos($book_formatted, $search_term);       
     if($search_term == "" || $pos !== false) {
          // this should be whatever line, or group of lines, that is printing the table rows
          print "<tr><td>" . $book_formatted. "</td></tr>";
     }
 }

Its important to note that the search position is only important if $search_term is not empty, this way if you are not doing a search, all rows will show.

Sign up to request clarification or add additional context in comments.

10 Comments

Sorry, books is an array with 20 different books being returned from a text file. They all return in an array called $books. I used your advice of only having "$book_formatted = substr($book_formatted, $pos);" and it partially works. It does bring back the correct string I am looking for (depending on my search) but it is bringing it back from the .txt form rather than filtering my html table. Any further advice?
I assume you have a loop on the page looping through the books and printing the html table rows. you will basically have to merge that with the code above. so inside that loop check if there is a search, and a match, then it will print that row. ill try to add something to the answer.
Yes I do have a loop that is outputting the html table. Thank you
Stephen, I added the following code but it repeats the brought back $book_formatted and puts it underneath each one of my books as a new row. It does not filter. Any help?
so in that loop, do the check to see whether each row should show or not, then inside the if statement, explode the text file and do whatever else you need to do to echo that row.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.