所以我想到了这个,这是一个新闻剧本。这些文件的名称与27.11.13 A突发新闻!为日期,重新排序将它们全部颠倒,以保持最新的一个。但是,问题是,我如何使最后一个(这是最新的)有一个大胆的标签?(我实际上想给它添加一些效果,所以让它大胆只是一个例子,我只需要说明一下)
<?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 
        . '">» ' . $file . "</a></h2>";
}
?>发布于 2013-11-27 03:10:56
假设最后一个是DOM中自上而下列出的第一个列表,您可以使用CSS:
h2:first-child {
   font-weight: bold;
}或者我可能会做的事情,因为可能有多个新的,是设置一个类为h2为新的:
$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 
        . '">» ' . $file . "</a></h2>";
}然后让CSS来为新消息添加样式:
h2.news.today {
   font-weight: bold;
}请注意,在根据其他条件更改$new变量之前,第二个选项都是粗体。您可能需要按日期或其他方式检查。
编辑:
$count = 0;
foreach ($files as $file) {
    $count++;
    echo '<h2 class="news'.($count === 1 ? ' latest' : '').'"><a href="?module=news&read=' . $file 
        . '">» ' . $file . "</a></h2>";
}
h2.news.latest {
   font-weight: bold;
}还可以使用for循环:
for ($count = 0; $count < count($files); $count++) {
    echo '<h2 class="news'.($count === 0 ? ' latest' : '').'"><a href="?module=news&read=' . $files[$count] 
        . '">» ' . $files[$count] . "</a></h2>";
}发布于 2013-11-27 03:15:50
在$files中对项进行计数,然后在遍历返回数组时对照返回数组的键值进行检查。如果项目到达最后一个键?大胆点。
<?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);
$last_key = count($files - 1);
foreach ($files as $file_key => $file_value)
    $file_final = '<a href="?module=news&read=' . $file . '">» ' . $file . '</a>';
    if ($file_key == $last_key) {
       $file_final = '<b>' . $file_final . '</b>';
    }
    echo '<h2>'
       . $file_final
       . '</h2>'
       ;
}
?>https://stackoverflow.com/questions/20233113
复制相似问题