2016-03-24 14 views
1

フォルダー(ウォッチフォルダー)の内容を確認するwebappを作成しました。 今、私は<tr></tr>の要素を作成時間に基づいて色付けしようとしています。 tr要素は黄色であるべきのように...foreach内でphp change css class

コードは次のようになります。

<?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は、特定のパスのすべてのフォルダを保持する配列です。

watchfolders(とサブフォルダ)とその時刻を取得できます。

しかし、各フォルダの作成時間に基づいてtr要素を着色することは適切に機能しません。私の場合、新しいフォルダを作成してもすべてのtr要素は緑色に着色されます。

いくつかのアドバイスはクールです。

あなたが私のコードを改善できると思うなら、あなたは大歓迎です!

答えて

0

私はあなたの場合はfalseを理由

strtotime($currentWatchPath) 

戻りますが、引数ではなく、「日時」またはそのような何かのようにファイルパスを与えているので、それは、だと思います。

そして、if文の最後

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

常にtrueになりますので、あなたのクラスは常に緑色になります。

strtotime()の代わりにfilemtime($ currentWatchPath)などを見てください