Giedrius Giedrius - 6 months ago 18
HTML Question

How to highlight current link in foreach loop

I have a script that lists all the directories as links.

<?php
$dirs = array_filter(glob('../*'), 'is_dir');
?>

<ul style="float:left;">
<?php
foreach ($dirs as $nav) {
echo "<li><a href='$nav'>".basename($nav)."</a></li>";
}
?>
</ul>


I want to highlight the current directory, or give the current link a class or id. I understand that i need if statement to accomplish this like
if(currentLink=thisLink) { // add span class somehow} else {// continue looping}
, but I am not completely sure how to do this.

What would be the correct way to implement this ?

Answer

to find out the current directory name you need:

basename(getcwd())

in foreach loop use this:

if(basename($nav) == basename(getcwd()))
{
// add span class
}