Giedrius Giedrius - 1 year ago 57
HTML Question

How to highlight current link in foreach loop

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

$dirs = array_filter(glob('../*'), 'is_dir');

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

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 Source

to find out the current directory name you need:


in foreach loop use this:

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