【问题标题】:File search by inputed values按输入值搜索文件
【发布时间】:2017-03-26 14:40:57
【问题描述】:

我在文件搜索方面遇到问题。我有 2 个文本表单,我想在其中输入值以在文件中进行搜索。但不知何故,如果我以其中一种形式编写输入,它也会显示在另一行中(我不想要这个)。此外,我的搜索仅适用于搜索选项之一(其中变量为 $search)。

<?php

function f_display(){

global $file;



?>

<form action=<?php echo $_SERVER['PHP_SELF'] ;?>>
    <input type=text name=search value="<?php echo $_GET['search'] ; ?>">
    <input type=submit value=Message_Search>
    </form>
<form action=<?php echo $_SERVER['PHP_SELF'] ;?>>
    <input type=text name=search value="<?php echo $_GET['search'] ; ?>">
    <input type=submit value=Name_Search>
    </form>

    <?php

    $f    = fopen($file,"r");
    $x     = 'bgcolor="#AAAAAA"';
    $y     = 'bgcolor="#FFFFFF"';
    $search =  $_GET['search'];
    $searchname = $_GET['search'];


    echo "<table width=50% border=1><tr><th>&nbsp;</th><th>vardas</th><th>nikas</th><th>pranesimas</th></tr>";

    if (filesize($file)>0){

        while (!feof ($f)) {
            $string = fgets ($f,1000);
            $part = explode ( "|" , $string );


            if ( @stristr($part[2] , $search) == false and $search) continue ;

            echo "<tr $tr_spec ><td><a href = \"index.php?aaa=2&id=$i\"></a></td><td>" . $part[0] . "</td><td>". $part[1]." </td><td>". $part[2] ."</td></tr>";



            if ( @stristr($part[0] , $searchname) == false and $searchname) continue ;

            echo "<tr $tr_spec ><td><a href = \"index.php?aaa=2&id=$i\"></a></td><td>" . $part[0] . "</td><td>". $part[1]." </td><td>". $part[2] ."</td></tr>";

       } // while
    } // if

    fclose ($f);
    echo "</table>";


?>

文件看起来像:

John|J0N|Hello
Peter|P3tE|Wow
Paul|PO|Nice

也许你可以给我一些东西或告诉我我做错了什么?

【问题讨论】:

    标签: php file search


    【解决方案1】:
    1. 您没有关闭您创建的函数:function f_display(){
    2. 您正在使用已弃用的 HTML,&lt;input type=submit value=Message_Search&gt; 您没有使用引号来给出值,而是给出了直接值。好办法是:&lt;input type="submit" value="Message Search"&gt;
    3. 您在写作时使用了不安全的方法,例如 $_SERVER['PHP_SELF']
    4. 您正在调用 undefined vars $_GET['search'](因为在首次加载时它们尚未定义),它可能会产生错误。 (提示:使用 filter_input())
    5. 您正在使用 HTML5 中不再使用的 width=50%
    6. 您正在使用 explode 方法,但我不确定您为什么不使用 strpos(位于单词的位置,这就是您正在尝试的方法)
    7. 如果你想逐行展开到最后一个,fgets 需要这个数字的第二个参数:4096(从https://secure.php.net/manual/en/function.fgets.php 中提取,我以前从未使用过该方法)
    8. 您在哪里定义 $tr_spec 变量?

    现在我会给你我的 PHP 脚本更正,如果你想使用它,请随意:

      // Check if the form has been sent or if the page was accessed using the GET method.
      if( filter_input( INPUT_SERVER, "REQUEST_METHOD" ) !== "GET" ) {
        die();
      }
    
      $search = filter_input( INPUT_GET, "search" );
    
      //  Check if the searh input has been correctly fill
      if( !isset( $search ) ) {
        die();
      }
    
      // Let's return here to prevent open an empty file
      if( filesize( $file ) <= 0 ) {
        return;
      }
    
      $openedFile = fopen( $file, "r" );
    
      // I was using the here document option, echo <<<END END; but it didn't work due to my text editor...
      echo "
      <table style=\"width: 50%; border-width: 1px;\">
        <thead>
          <tr>
            <th>&nbsp;</th>
            <th>vardas</th>
            <th>nikas</th>
            <th>pranesimas</th>
          </tr>
        </thead>
        <tbody>
      ";
    
      // Let's save in buffer the current line and then navigate through all of them
    
      while( ( $buffer = fgets( $openedFile, 4096 ) ) !== false ) {
    
        // Here we check if the line has the searched word
        if( strpos( $buffer, $search ) !== false ) {
    
          $part = explode( "|", $buffer );
    
          echo "
              <tr>
                <td>" . $part[0] . "</td>
                <td>" . $part[1] . "</td>
                <td>" . $part[2] . "</td>
                <td>" . $part[3] . "</td>
              </tr>
          ";
    
        }//.if
    
      }//.while
    
      echo "
        </tbody>
      </table>
      ";
    

    我用 cmets 解释了一切,但我刚刚更正了结尾脚本问题,你还有一些问题需要解决。我希望这会有所帮助!

    【讨论】:

    • 如果我想分两部分搜索怎么办。我怎么能做到。您的脚本正在整个文件中搜索,我需要按 2parts 搜索
    • 两个不同的部分是什么意思?如果您的意思是 2 个不同的文件只是在第一个文件之前添加一段时间,如果您只想要一个部分更改爆炸或使用 strpos 排除部分,您也可以使用 substr 方法
    • 我的意思是文件有 3 列,每一列都是一部分。我需要在 1 和 3 列中进行搜索。我可以轻松搜索一列,但我无法在两列(part[0] 和 part[2])上进行搜索。
    • 不! strpos() 方法将查找列中的每个单词。如果你只是想测试一列是否正常,首先分解代码,然后对每个部分执行 strpos
    • 也许你可以提供给我,如何处理表格?如何制作 2 个不同的输入,一个用于 part[1],另一个用于 part[2]。稍后进行搜索,这取决于插入的值。
    猜你喜欢
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2021-11-23
    • 2017-04-16
    • 1970-01-01
    • 2013-12-06
    相关资源
    最近更新 更多