2013-01-09 40 views
1

我想用DateTime來確定新發布的漫畫應該保持突出顯示多長時間。所以,我的目標是讓最新的漫畫「突出顯示」2天,然後回到正常(灰色,和其他一樣)。突出顯示最新帖子,直到某個日期

注意:這是我以前問過的問題。我正在重申它,因爲前面的問題對於人們來說變得相當混亂。所以我重新編寫了代碼,簡化了我的問題,並重新提出了問題。

enter image description here

我的邏輯:

Loop through all comics { 
    if comic date >= current date, display that comic with highlight CSS tag, 

    else display it with normal CSS tag. 
} 

我的代碼:我不知道爲什麼這是不工作...它甚至沒有展示最新的漫畫(其日期>當前日期)。

 $desc = (isset($_GET['description']) ? ($_GET['description']) : null); 


    $row = $catResult->fetch_assoc(); 

    $current_date = new DateTime; 
    echo "Current Date: " . $current_date->format('Y-m-d H:i:s'); 

    //$comic_date->modify('3 day'); 

    //DISPLAY IMAGES TO CORRECT PAGE FROM DATABASE 

      echo '<ul>'; 
     $imageCounter = 0; 
     while (($imageCounter < $imagesPerPage) && ($row = $catResult->fetch_assoc())) { 

      $comic_date = new DateTime($row['date']); 

      $class = ($comic_date >= $current_date) ? 'newcomics' : 'comics'; 
       echo '<li>';     
        echo  '<span class="' . $class . '"><a href=".?action=viewimage&site='.$site. '&id=' . $row['id'] .'" title="' . $row['description'] . '" alt="' . $row['title'] . '"> 
           <img src="./scripts/thumber.php?img=.' . $thumbpath.$row['thumb'] . '&mw=220&mh=220"/></a> 
           <br /><br /> ' . $row['description'] . $row['date'] . '</span>';         
        $imageCounter++; 
       echo '</li>'; 


      } 
     echo '</ul>'; 

有什麼想法?

回答

2

您的三元條件設置爲條件爲真,您只輸出<span class="newcomics">而沒有其他東西(不是錨標籤等)。

我建議你做這樣使它更具可讀性:

$class = ($row['date'] >= $current_date) ? 'newcomic' : 'comic' 

echo '<span class="' . $class . '"><a href=".?action=viewimage&site='.$site. '&id=' . $row['id'] .'" title="' . $row['description'] . '" alt="' . $row['title'] . '"><img src="./scripts/thumber.php?img=.' . $thumbpath.$row['thumb'] . '&mw=220&mh=220"/></a> <br /><br /> ' . $row['description'] . $row['date'] . '</span>'; 
+0

我已經更新上面的代碼 – Growler

+1

你仍然有問題?如果是這樣,你還可以向我們展示生成的HTML(右鍵 - >查看源代碼) – xbonez

+0

似乎要解決......但只有當我把'$ comic_date = new DateTime($ row ['date']) ;」在while循環中,並使用DateTime進行比較...我想知道爲什麼我不能只比較$ row ['date']?如下所示:$ class =($ row ['date']> = $ current_date)? 'newcomics':'漫畫'; ....爲什麼當它們都回顯爲相同的格式時,它必須是新的DateTime? – Growler