Display in the table if search matches in php

后端 未结 1 748
别那么骄傲
别那么骄傲 2020-12-20 08:54

see my Search in php from dat file, This is my code so far:



        
1条回答
  •  暖寄归人
    2020-12-20 09:34

    You would have to open the file using fopen or file_get_contents, then you would explode or split on newlines \n or \r\n (depending on OS and file).

    Then, you could loop through them and explode that string again to see if the first element is what you are looking for. Like so:

    $resLines = explode("\n", $FileContents);
    foreach ($resLines as $line) {
      $resLine = explode("|", $line);
      if ($resLine[0] == $Search) {
        echo "Found! $line";
      }
    }
    

    Update regarding your edit

    It would be something like this:

    $resContents = file_get_contents("order.dat");
    $resLines = explode("\n", $FileContents);
        foreach ($resLines as $line) {
          $resLine = explode("|", $line);
          if ($resLine[0] == $Search) {
            $aFound = $resLine; // setting our array with the found contents
          }
    }
    echo "
    
    Order number Tranasaction id Date Total Price
    " . $aFound[0] . " " . $aFound[1] . " " . $aFound[2] . " " . $aFound[3] . "
    ";

    0 讨论(0)
提交回复
热议问题