将foreach的值返回到表中

时间:2018-10-24 07:17:12

标签: php foreach html-table

我有一个包含不同文件的文件夹和3个文件夹(test1,test2和test3) 在这3个文件夹中,有3个HTML文件,其标题为(index1.html,index2.html,index3.html)

我想将foreach的结果显示到“标题网站”中,但我不知道

我包括我的脚本和图像,以更好地理解。

enter image description here

<body>

  <div id="container">

    <h1>Home Page</h1>

    <table class="table table-hover ">
      <thead>
        <tr>
          <th>Filename</th>
          <th>Date</th>
          <th>Title Web Site</th>
        </tr>
      </thead>
      <tbody>
      <?php

        // Opens directory
        $myDirectory=opendir(".");

        // Gets each entry
        while($entryName=readdir($myDirectory)) {
          $dirArray[]=$entryName;
        }

        // Finds extensions of files
        function findexts ($filename) {
          $filename=strtolower($filename);
          $exts=explode(" ", $filename);
          $n=count($exts)-1;
          $exts=$exts[$n];
          return $exts;
        }

        // Closes directory
        closedir($myDirectory);

        // Counts elements in array
        $indexCount=count($dirArray);


        $fileList = glob('test?/*.html');
        $array = array();
        foreach($fileList as $file_title){
            $html = file_get_contents($file_title);
                  preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
               array_push($array, $matches[1]);  
        }
          var_dump($array);



        // Loops through the array of files
        for($index=0; $index < $indexCount; $index++) {

          // Allows ./?hidden to show hidden files
          if($_SERVER['QUERY_STRING']=="hidden")
          {$hide="";
          $ahref="./";
          $atext="Hide";}
          else
          {$hide=".";
          $ahref="./?hidden"; 
          $atext="Show";}
          if(substr("$dirArray[$index]", 0, 1) != $hide) {

          // Gets File Names
          $name=$dirArray[$index];
          $namehref=$dirArray[$index];          

          // Gets Date Modified Data
          $modtime=date("Y-m-d H:i", filemtime($dirArray[$index]));          

          // Separates directories
          if(is_dir($dirArray[$index])) {
            $class="dir";

          } else {
            $class="file";
          }

          print("
          <tr class='$class'>
            <td><a href='./$namehref'>$name</a></td>
            <td><a href='./$namehref'>$modtime</a></td>
          </tr>");

          }

        }       
      ?>
      </tbody>
    </table>

  </div>

</body>

0 个答案:

没有答案