see my Search in php from dat file, This is my code so far:
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>";