2011-08-19 70 views
-1

可能重复:
Display in the table if search matches in php显示项目,如果搜索匹配

看到我在PHP从dat文件的搜索,这是我到目前为止的代码:

<?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

@Baszz :)我想在表中显示,如果搜索匹配 – John

回答

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; 
     } 
    } 

>

然后,像这里面的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> 

+0

:)在td时没有显示当我使用这个,但它的工作50% – John

+0

@David $结果是空的?您在哪里添加了$结果分配? – Awea

+0

:)你可以在你的答案编辑这个... – John

0

不知道如果我理解你的问题,强硬,我认为你需要看看之后explode功能:

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

然后您打印出结果以及想要的td标签。

+0

:)可以üPLZ编辑我的问题 – John

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