Отображение в таблице, если поиск совпадает с php

см. мой поиск в php из dat-файла. Это мой код:

<?php if (isset($_POST["name"])) { $file = 'order.dat'; $searchfor = $_POST["name"]; // the following line prevents the browser from parsing this as HTML. // get the file contents, assuming the file to be readable (and exist) $contents = file_get_contents($file); // escape special characters in the query $pattern = preg_quote($searchfor, '/'); //$pattern = "/^$pattern/m"; // your string starts with $pattern (177) $pattern = "/^$pattern.*$/m"; // search, and store all matching occurences in $matches if(preg_match_all($pattern, $contents, $matches)){ echo "Found matches:\n"; echo implode("\n", $matches[0]); } else{ echo "No matches found"; } } ?> <h3>Search Order Details</h3> <form method="post" action="search.php" id="searchform"> <input type="text" name="name"> <input type="submit" name="submit" value="Search"> </form> 

Файл order.dat содержит: –

 175|RC456456456|54156456177|177$ 176|RC456456177|54156456177|177$ 177|RC456456177|54156456465|129$ 178|RC456456456|54156456177|177$ 

теперь прямо сейчас, если поиск найден, тогда он говорит, что найденные совпадения … например, если я ввожу 177, он дает Found matches: 177|RC456456177|54156456465|129$

теперь, если я вхожу в 002, тогда он говорит, что совпадений не найдено

Я хочу показать в этой таблице, если поиск соответствует: –

 <table> <tr> <th>Order number </th> <th>Tranasaction id</th> <th>Date </th> <th>Total Price</th> </tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </table> 

Вам нужно будет открыть файл, используя fopen или file_get_contents , затем вы будете explode или split на newlines \n или \r\n (в зависимости от ОС и файла).

Затем вы можете пропустить их и снова explode эту строку, чтобы увидеть, является ли первый элемент тем, что вы ищете. Вот так:

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

Обновление относительно вашего редактирования

Это было бы примерно так:

 $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>";