-3

I have PHP code that reads from text files and displays the result where the user enters a word to search for and the system reads the files and displays the result with specified the line number and the file name.

I want to display these results in a table or a form that is well formed.

if its possible, columns must be the name of files and the rows contains the line number.

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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rany Fahed
  • 39
  • 3
  • 11

1 Answers1

0

You can write HTML tags using echo

So, write this before foreach loop.

echo "<table>"

Inside of foreach loop has to be like that

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

Close your table tag after foreach loop

echo "</table>"

And that's it.

hamzaerdem
  • 23
  • 1
  • 1
  • 3