如何使用PHP在表中显示结果

问题描述 投票:-3回答:1

我有PHP代码从文本文件中读取并显示结果,用户输入要搜索的单词,系统读取文件并显示结果,并指定行号和文件名。

我想在表格或形式良好的表格中显示这些结果。

如果可能,列必须是文件名,行包含行号。

Code:

<?php


if(isset($_POST["search"]))
    {
        $search =$_POST['name'];
        echo "the word  $search exist: <br><br>";
        foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
        {
            $line = 1; 
            $myFileLink = fopen($txts, 'r');
            while(!feof($myFileLink)) 
            {
            $myFileContents = fgets($myFileLink);
            if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
            {

                    foreach($matches[1] as $match)
                    {


                         echo "line [$line] file  ";
                    }
                    echo basename ($txts) . "<br>".PHP_EOL;

            }
            ++$line;
            }
        fclose($myFileLink);
        }
    }
?>


<html>
    <head>
    </head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
          #form {
        background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        margin: auto;
        width: 200px;
        height: 200px;
        position: absolute;


        font-family: Tahoma, Geneva, sans-serif;
        font-size: 14px;
        font-style: italic;
        line-height: 24px;
        font-weight: bold;
        color: #09C;
        text-decoration: none;
        border-radius: 10px;
        padding: 10px;
        border: 1px solid #999;
        border: inset 1px solid #333;
        -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
      }

    </style
    <body>
    <div id = "form">
        <form action="index.php" method="post">
          <h1 align =center > Search Form </h1>
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p align =center ><input type ="Submit" name ="search" value= "Search" /></p>
        </form>
    </div>
    </body>
</html>
php html readfile tabular
1个回答
0
投票

您可以使用echo编写HTML标记

所以,在foreach循环之前写这个。

echo "<table>"

foreach循环内部必须是这样的

echo "<tr><td>".$line. "</td></tr>

在foreach循环后关闭表标记

echo "</table>"

就是这样。

© www.soinside.com 2019 - 2024. All rights reserved.