dounie0889 2013-11-27 03:03
浏览 8

制作最新消息<b>旧

So I came up with this, which is a news script. The files has names with [27.11.13] A breaking news! as dates, and rsort will sort them all reversed to keep the latest one up. BUT, the question is, how can i also make the last one (which is the newest) have a bold tag? (I actually want to add some effects to it, so making it bold is just an example and i just need the directions)

<?php
$files = array();
if($handle = opendir( 'includes/news' )) {
    while( $file = readdir( $handle )) {
        if ($file != '.' && $file != '..') {
            // let's check for txt extension
            $extension = substr($file, -3);
            // filename without '.txt'
            $filename = substr($file, 0, -4);
            if ($extension == 'txt')
                $files[] = $filename; // or $filename
        }
    }
    closedir($handle);
}
rsort($files);
foreach ($files as $file)
    echo '<h2><a href="?module=news&read=' . $file 
        . '">&raquo; ' . $file . "</a></h2>";
}
?>
  • 写回答

2条回答 默认 最新

  • doutan6286 2013-11-27 03:10
    关注

    Assuming the last one is the first listed in the DOM from top to bottom, you could use CSS:

    h2:first-child {
       font-weight: bold;
    }
    

    Or what I would probably do since there could be multiple new ones is set a class to h2 for the new:

    $todaysDate = date("d.m.y");
    foreach ($files as $file) {
        $fileDate = substr($file,1,8);
        if ($todaysDate == $fileDate) {
           $today = true;
        }
        echo '<h2 class="news'.($today ? ' today' : '').'"><a href="?module=news&read=' . $file 
            . '">&raquo; ' . $file . "</a></h2>";
    }
    

    Then have CSS to style the new news:

    h2.news.today {
       font-weight: bold;
    }
    

    Please note that the second option will have all as bold until you change the $new variable based on other conditions. You may want to check by date or something else.

    EDIT:

    $count = 0;
    foreach ($files as $file) {
        $count++;
        echo '<h2 class="news'.($count === 1 ? ' latest' : '').'"><a href="?module=news&read=' . $file 
            . '">&raquo; ' . $file . "</a></h2>";
    }
    
    h2.news.latest {
       font-weight: bold;
    }
    

    You could also use a for loop:

    for ($count = 0; $count < count($files); $count++) {
        echo '<h2 class="news'.($count === 0 ? ' latest' : '').'"><a href="?module=news&read=' . $files[$count] 
            . '">&raquo; ' . $files[$count] . "</a></h2>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c