简体   繁体   中英

how to bring back filtered rows in an html table using PHP (no MySQL)

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;
                }
               }
              }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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