如果“搜索”匹配,则显示表中的项目


Display Items in the table if Search matches

可能重复:
如果在php 中搜索匹配,则显示在表中

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

<?php   
if (isset($_POST["name"]))
{
    $file = 'order.dat';
    $searchfor = $_POST["name"];

$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^$pattern.*$/m";

if(preg_match_all($pattern, $contents, $matches))
{
     $result = explode('|', $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$

现在,如果搜索被找到,它会说找到匹配。。。就像我输入177Found matches: 177|RC456456177|54156456465|129$

现在,如果我输入002,它会显示没有找到匹配的

My Question is

如果搜索匹配,我想在此表中显示:-

 <table>
                    <tr>
                    <th>Order number </th>
                    <th>Tranasaction id</th>
                    <th>Date </th>
                    <th>Total Price</th>
                    </tr>
        <td><?=$result[0]?></td>
        <td><?=$result[1]?></td>
        <td><?=$result[2]?></td>
        <td><?=$result[3]?></td>

            </table>

不确定我是否理解你的问题,很难,我认为你需要照顾爆炸函数:

$results = explode("|", $matches[0]);

然后打印出结果以及所需的td标记。

嗯。。。如果您有搜索结果,请循环遍历它们并输出表行。也许下面的代码片段可以帮助你。我使用爆炸来获取值。

foreach($results as $result){
  var data = explode("|", $result);
  echo "<tr><td>".$result[0]."</td><td>".$result[1]."</td><td>".$result[2]."</td></tr>";
}

也许你可以做这样的事情:

<?php   
    if (isset($_POST["name"])){
        $file = 'order.dat';
        $searchfor = $_POST["name"];
        $contents = file_get_contents($file);
        $pattern = preg_quote($searchfor, '/');
        $pattern = "/^$pattern.*$/m";
        if(preg_match_all($pattern, $contents, $matches)){
           $result = implode("'n", $matches[0]);
           $result = explode('|', $result);
        }
        else{
           $result = false;
        }
    }

?>

然后在html中显示如下:

<table>
    <tr>
        <th>Order number </th>
        <th>Tranasaction id</th>
        <th>Date </th>
        <th>Total Price</th>
    </tr>
    <tr>
        <td><?=$result[0]?></td>
        <td><?=$result[1]?></td>
        <td><?=$result[2]?></td>
        <td><?=$result[3]?></td>
    </tr>
</table>