Display in the table if search matches in php

后端 未结 1 744
别那么骄傲
别那么骄傲 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 "
    <table>
        <tr>
            <th>Order number </th>
            <th>Tranasaction id</th>
            <th>Date </th>
            <th>Total Price</th>
        </tr>
    
        <tr>
            <td>" . $aFound[0] . "</td>
            <td>" . $aFound[1] . "</td>
            <td>" . $aFound[2] . "</td>
            <td>" . $aFound[3] . "</td>
        </tr>
    
    
    </table>";
    
    0 讨论(0)
提交回复
热议问题