从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$
现在现在如果找到搜索然后它说找到匹配...就像我输入177给它
Found 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>
答案 0 :(得分:0)
可能你可以这样做:
<?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;
}
}
&GT;
然后在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>
答案 1 :(得分:0)
答案 2 :(得分:0)
嗯......如果你有你的搜索结果,循环它们并输出表格行。也许下面的代码片段可以帮助您。我使用explode来获取值。
foreach($results as $result){
var data = explode("|", $result);
echo "<tr><td>".$result[0]."</td><td>".$result[1]."</td><td>".$result[2]."</td></tr>";
}