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
. '">» ' . $file . "</a></h2>";
}
?>
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
. '">» ' . $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
. '">» ' . $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]
. '">» ' . $files[$count] . "</a></h2>";
}
Do a count of the items in $files
and then check that against the key value of the returned array as you loop through it. If the item reaches the last key? Bold it.
<?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>'
;
}
?>