如果在php中搜索匹配,则在表中显示

如果在php中搜索匹配,则在表中显示,php,Php,请参阅我从dat文件在php中的搜索,这是我迄今为止的代码: <?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 t

请参阅我从dat文件在php中的搜索,这是我迄今为止的代码:

<?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>
现在,如果搜索被找到,它会说找到了匹配项。。。就像我输入177它给出的
找到匹配项:177 | RC4564177 | 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>

订单号
转换id
日期
总价

您必须使用
fopen
file\u获取内容
打开文件,然后在换行符
\n
\r\n
上进行
分解
拆分(取决于操作系统和文件)

然后,您可以循环遍历它们并再次分解该字符串,以查看第一个元素是否是您要查找的元素。像这样:

$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>";
$resContents=文件获取内容(“order.dat”);
$resLines=explode(“\n”,$FileContents);
foreach($resline作为$line){
$resLine=分解(“|”,$line);
如果($resLine[0]==$Search){
$aFound=$resLine;//使用找到的内容设置数组
}
}
回声“
订单号
转换id
日期
总价
" . $A基金会[0]。"
" . $A基金会[1]。"
" . $A基金会[2]。"
" . $A基金会[3]。"
";

不确定您的问题到底是什么?@David我也不知道您的问题是什么。@Pekka请看,如果搜索匹配,我想将我的数据放在表中。。。比如,如果我输入177,它会给出找到的匹配项:177 | RC456456177 | 54156465 | 129$,现在我想把这些细节放在表中。。。表示如果搜索匹配,我想在表中显示…@Devator:)。。然后,在我想在表中显示详细信息之后,否则现在找不到匹配项,会出现匹配项警报,但我想在表中显示177 | rc456177 | 54156465 | 129$。@Pekka:)现在再次编辑问题以便更好地理解:)我不明白你在说什么。。。你可以在任何编辑器中修改我的代码,但仍然不能显示在表中
$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>";