简体   繁体   English

如何使用PHP(没有MySQL)带回html表格中的过滤行

[英]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. 在我的html中,我有一个搜索功能,当单击“提交”按钮时,运行了一个PHP功能,该功能应筛选出不包含搜索到的单词的行,而仅过滤HTML表中包含搜索到的单词的行,页。 My HTML table is being generated using PHP which is reading books from a .txt file. 我的HTML表是使用PHP生成的,该PHP正在从.txt文件中读取书籍。 My code brings back the rows from the text file instead of filtering the table results. 我的代码从文本文件中带回行,而不是过滤表结果。 Any help please? 有什么帮助吗?

HTML 的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 的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: 如果没有真正看到$books甚至$book样子,似乎每个搜索都会因此而失败:

$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. 如果您的书是“ ABC书”,而您的搜索是“ Bo”,则第一个条纹会给您结果4,这不是错误的,因此移至您的else部分。 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. 但是,然后您再次对书名进行子字符串化,将名称砍成“ ABC”,然后再次搜索“ Bo”,这通常会失败。

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. 在这种情况下,第二次搜索是无意义的,因为它总是返回0。除非未提供更多内容,否则只要您按自己的意愿格式化书名并在第一个else语句中打印而不是第二次打印就可以了脱衣舞。

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. 重要的是要注意,搜索位置仅在$ search_term不为空时才重要,这样,如果不进行搜索,将显示所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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