开发者

Display Items in the table if Search matches [duplicate]

This question already has an answer here: Closed 10 years ago.

Possible Duplicate:

Display in the table if search matches in php

see my Search in php from dat file, This is my code so far:

<?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 file contains:-

175|RC456456456|54156456177|177$

176|RC456456177|54156456177|177$

177|RC456456177|54156456465|129$

178|RC456456456|54156456177|177$

now right now if search is found then it says Found matches ... like if i enter 177 the it gives 开发者_Python百科 Found matches: 177|RC456456177|54156456465|129$

now if i enter 002 then it says No matches found

My Question is

I want to Display in this table if search matches :-

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


Not sure if I understood your question, tough I think you need to look after the explode function:

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

Then you print out the results along with wanted td tags.


Well...if you have your search result(s), loop through them and output the table row. Maybe the code snippet below can help you. I use explode to get the values.

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


May be you can do something like this :

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

?>

Then display it inside html like this :

<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜