显示表格时有条件地跳过行

时间:2018-10-27 16:13:10

标签: php html-table

每次都有一个只包含标题的index.html文件时,我都有一个文件夹。

我现在正在尝试确保如果文件夹为空,那么我们不会在板上显示它,但是我不知道该怎么做...

我还将代码的一部分发送给您,以便您更好地理解我的要求:

$title     = array();
$link_html = array();

// Find title and file inside the folder
$fileList = glob('tpe**/index.html');
foreach($fileList as $file_Path) {
    $html = file_get_contents($file_Path);
    preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
    array_push($title, $matches[1]);
    array_push($link_html, $file_Path);  
} 

$file_empty = glob('tpe**');
foreach($file_empty as $filePath) {
    if (count(glob($filePath . '/*')) == 0 ) {
        /* Gérer condition dossier vide */
    }
}

// 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];

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

        // Display all information
        $dirs1    = array_filter(glob('*'),'is_file');
        $compteur = count($dirs1)+2;

        if ($index>=$compteur) {
            $ind = $index - $compteur;
            $path = $link_html[$ind];
            print("
                <tr>
                <td><a href='./$path'>$name</a></td>
                <td><a href='./$path'>$modtime</a></td>
                <td><a href='./$path'>$title[$ind]</a></td>
                </tr>" 
            );
        }
    }
}       

在此图像上,tpe07文件夹为空,因此目标是不显示空文件夹:

enter image description here

1 个答案:

答案 0 :(得分:0)

我用下面的这棵树做了一个测试。在这里,.tpe05是一个隐藏目录,tpe07是空的。

.
├── index.php
├── tpe00
│   └── index.html
├── tpe01
│   └── index.html
├── tpe02
│   └── index.html
├── tpe03
│   └── index.html
├── tpe04
│   └── index.html
├── .tpe05
│   └── index.html
├── tpe06
│   └── index.html
├── tpe07
├── tpe08
│   └── index.html
└── tpe09
    └── index.html

让我们查看脚本:

<?php

$title     = array();
$link_html = array();

// Code added to make this test page working
// You can remove it since dirArray and indexCount
// seem to be already set in your code.
date_default_timezone_set('Europe/Paris');
$dirArray = array();
for ($i=0; $i <= 9 ; $i++)
    array_push($dirArray,($i==5?".":null)."tpe0" . $i);
$indexCount = sizeof($dirArray);
// End of added code

// Getting *all* the files including the hidden ones.
// Getting also the hidden files.
// Filling title and link_html arrays for each file
// With <key,value> pairs.
// Examples:
// $title["tpe01"] = "Je suis index 01"
// $link_html["tpe01"] = "tpe01/index.html"

$fileList = glob('{,.}tpe*/index.html', GLOB_BRACE);
foreach($fileList as $file_Path) {
    $html = file_get_contents($file_Path);
    preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
    $dirname = basename(dirname($file_Path));
    $title[$dirname] = $matches[1];
    $link_html[$dirname] = $file_Path;
} 

// Code added to make this test page working (HTML table beginning)
// You can remove it since it seems you already begun the table.
echo "<table>";
// End of added code

// Loops through the array of files
for($index=0; $index < $indexCount; $index++) {
    // Allows ./?hidden to show hidden files

    $name=$dirArray[$index];
    $hasDirPrefix = (substr($name, 0, 1) == ".");

    if ($hasDirPrefix && !$showHiddenFile) continue;
    if (!isset($title[$name])) continue;

    // Gets File Names


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


    $path = $link_html[$name]; // . ($showHiddenFile?"?hidden":null);
    print("
                <tr>
                <td><a href='./$path'>$name</a></td>
                <td><a href='./$path'>$modtime</a></td>
                <td><a href='./$path'>$title[$name]</a></td>
                </tr>" 
    );


}
// Code added to make this test page working (HTML table end)
// You can remove it since it seems you may have end the table
// further.
echo "</table>";
// End of added code
?>

在这里,我使用了continue。根据{{​​3}}:

  

continue 用于循环结构中,以跳过当前循环的其余部分,并在条件评估中继续执行 ,然后< strong>下一次迭代的开始。

我还使用PHP documentation来显示标题和链接。变得容易起来。看一下标题数组的内容:

Array
(
    [tpe00] => Je suis index 0
    [tpe01] => Je suis index 1
    [tpe02] => Je suis index 2
    [tpe03] => Je suis index 3
    [tpe04] => Je suis index 4
    [tpe06] => Je suis index 6
    [tpe08] => Je suis index 8
    [tpe09] => Je suis index 9
    [.tpe05] => Je suis index 5
)

要获取tpe04目录的标题,只需编写title["tpe04"]。您不必介意订单项,这在这里是一件好事:在$fileList中,".tpe05"是最后一项,请执行print_r($fileList);来查看它。