2013-01-09 39 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