1

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 Answers2

0

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>";
}
fanfavorite
  • 5,128
  • 1
  • 31
  • 58
  • h2 .news:first-child { font-size: 17px; } how do i make this work? i want to specify

    with class="news" so it wont bother the other tags in the page

    – user2579331 Nov 27 '13 at 03:22
  • like, TAG, CLASS:firs-child how do i set it properly? sorry – user2579331 Nov 27 '13 at 03:22
  • If the class is on the tag, its TAG.CLASS:first-child { font-size: 17px; }. Make sure you don't have a space. If this is in your HTML, make sure you have or have an external CSS file, use the link tag. – fanfavorite Nov 27 '13 at 03:25
  • $title this is calling, and this is the css: h2.news:first-child { font-size: 17px; } but it wont change it, why? – user2579331 Nov 27 '13 at 03:27
  • Would have to see an example. That should work unless you are using an old browser. What browser and version are you using? My guess it is how you implemented it though. – fanfavorite Nov 27 '13 at 03:30
  • i tried removing the :first-child to see if its the problem, it is. the font changes with h2.news{..} only, it wont recognize the first child request – user2579331 Nov 27 '13 at 03:30
  • You must be using an older browser then. See for browser support: http://stackoverflow.com/questions/7938521/browser-support-for-css-first-child-and-last-child – fanfavorite Nov 27 '13 at 03:32
  • Why wouldn't you add a class for new elements instead anyways? If you have getting the date, you could check if its after the current date. – fanfavorite Nov 27 '13 at 03:33
  • how can i check that? sorry for the idiotic questions. it seems like first-child is the problem here.. – user2579331 Nov 27 '13 at 03:37
  • Hard to tell without more code, but date("d.m.y") would get you the current date in the format of DD.MM.YY like you have in your description above. – fanfavorite Nov 27 '13 at 03:39
  • yeah but i'm not trying to do "today's news" , just tag the latest one, make it abit more noticeable – user2579331 Nov 27 '13 at 03:41
  • I have updated my answer to get the date from the file variable, assuming its always [DD.MM.YY] at start of the file. – fanfavorite Nov 27 '13 at 13:48
  • Check out the Edit version at the bottom. – fanfavorite Nov 27 '13 at 17:00
0

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 . '">&raquo; ' . $file . '</a>';
    if ($file_key == $last_key) {
       $file_final = '<b>' . $file_final . '</b>';
    }
    echo '<h2>'
       . $file_final
       . '</h2>'
       ;
}
?>
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103