【问题标题】:Php change css class inside foreachphp在foreach中更改css类
【发布时间】:2016-07-11 20:53:13
【问题描述】:

我构建了一个 webapp 来检查文件夹(监视文件夹)的内容, 现在我正在尝试根据 <tr></tr> 元素的创建时间为它们着色,例如如果文件夹是在一分钟前创建的,则 tr 元素应该是黄色的等等......

代码如下:

<?php

$currentTimeFormat = date("d.m.Y H:i",$currentTime);

echo "current time " . $currentTimeFormat;

if (count($watchFolder) > 0) {

    echo '<table class=\'table table-reponsive table-bordered\'>';

        echo '<tbody>';

        foreach ($watchFolder as $key => $value) {

            $currentWatchPath = $watchFolderPath . $key;

            if(strtotime($currentWatchPath) < strtotime('-1 minute')) {
                $class = "redtest";
            }
            if(strtotime($currentWatchPath) < strtotime('-1 hour')) {
                $class = "bluetest";
            }
            if(strtotime($currentWatchPath) < strtotime('-1 day')) { # Older than one day
                $class = "greentest";
            }

            echo '<thead class=\'thead-inverse\'>
                    <tr>
                        <th scope=\'row\' class='.$class.'>'.$key.'&nbsp;|&nbsp;Last change '.date("d.m.Y H:i", filemtime($currentWatchPath)).'</th>
                    </tr>
                  </thead>';

            foreach ($value as $key => $valueSub) {

                $currentWatchPath2 = $currentWatchPath . '\\' . $key;

                echo '<tr>
                        <td class=\'grey\'>&nbsp;&nbsp;&nbsp;&nbsp;
                        <span class="glyphicon glyphicon-arrow-right"></span>&nbsp;&nbsp;'.$key.'&nbsp;|&nbsp;Last change '.date("d.m.Y H:i", filemtime($currentWatchPath2)).'</td>
                      </tr>';

                foreach ($valueSub as $content) {

                    $currentWatchPath3 = $currentWatchPath2 . '\\' . $content;

                    echo '<tr>
                            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <span class="glyphicon glyphicon-arrow-right"></span>&nbsp;&nbsp;'.$content.'&nbsp;|&nbsp;Last change '.date("d.m.Y H:i", filemtime($currentWatchPath3)).'</td>
                          </tr>';

                }

            }

        }

        echo '</tbody>';

    echo '</table>';

}
?>

$watchfolder 是一个数组,其中包含特定路径的所有文件夹。

我得到了监视文件夹(和子文件夹)及其时间,这是可行的。

但是根据每个文件夹的创建时间为 tr 元素着色不能正常工作,在我的情况下,即使我创建一个新文件夹,所有 tr 元素都是绿色的,它是绿色的,而它应该是红色的。

一些建议会很酷。

因此,如果您认为可以改进我的代码,欢迎您!

【问题讨论】:

    标签: php html arrays time foreach


    【解决方案1】:

    我猜是因为

    strtotime($currentWatchPath)
    

    在您的情况下返回 false,因为您将文件路径作为参数提供,而不是“日期时间”或类似的东西。

    最后一个 if 语句

    if(strtotime($currentWatchPath) < strtotime('-1 day'))
    

    永远是真的,所以你的班级永远是绿色的。

    看看 filemtime($currentWatchPath) 或类似的东西,而不是 strtotime()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多