Determine if this folder is the last level within that folder?

Good day,
I’m working on a script and this script list folder in a html list.

Here is the code;



<html>

<head>

<style type="text/css">

ul {

      list-style-image: url(Icons/box_icon.png);



}



li li {

      list-style-image: url('Icons/open_book_icon.jpg');



}



li li li {

      list-style-image: url('Icons/gallery.gif');

}



</style>

</head>

<body>







<?PHP

function globDir($dir)

{

   $files = glob("$dir/*", GLOB_ONLYDIR);

   

	 if(!empty($files))

   {

      echo "<ul>\
";



      foreach($files as $file)

      {

      echo "<li>";

 



      echo "<b>". basename($file)."</b>\
";



               globDir($file);

         echo "</li>\
";

      }

      echo "</ul>\
";

   }

}

globDir('Photos');

?>



</body>

</html>


example folder
|

  • Folder 1
  • Folder 2
    |
    • Folder2.1
      |
  • Folder3

The deal here is that I could go by CSS level of list but my problem is that sometime there is more subfolder yhat this example.

Is there a way that I can tell the last level folder to be written in bold ?

Thanks !

If I understand correctly, you want to style the parent list items differently from those that have no child list items.

It’s not possible to do that only with CSS.

The closest that you could come to a solution is to use JavaScript, to apply a suitable class name to the leaf list items. The JavaScript Forum will be able to help further with this.

Yes, I know that is why I asked, I have no clues and I have been searching , can you help me start ?

Thanks !

I’ll see if I can get this thread moved to the correct forum.

I don’t want it to be css or javascript, it could be done in php ?

You would need to let us know how you are coding this from PHP

bonk

he did :smiley:

:rolleyes: Trust me to see CSS code and ignore the PHP down below the scrollbar.

You can check if the file has directory contents, and if so, create a class name for the list item:


$dirClass = (glob("$file/*", GLOB_ONLYDIR)) ? ' class="dir"' : '';
echo "<li$dirClass>"; 
echo "<b>". basename($file)."</b>\
"; 
globDir($file);
echo "</li>\
"; 

That way the css can have a default style for the list items, and the ones with a class of “dir” can be styled as normal.


li { font-weight: bold; }
li.dir { font-weight: normal; }

Here we go - play about with this:




> < ?php
	function globDir($dir) 
	{ 
	   $files 	= glob("$dir/*", GLOB_ONLYDIR); 
	   $clr = 'black';
	   
	   if(!empty($files)) 
	   	{ 
		   $lastdir = count($files) -1;
		   
	      echo "<ul>\
"; 
	      foreach($files as $i2 => $file) 
	      {
	      	$clr = ($i2 == $lastdir) ? 'red' : $clr;  
	      	
	      	echo "<li style='color:$clr'>" .$lastdir .' - ' .$clr .'- ' .$i2 .' - '.basename($file)."\
"; 
		      	globDir($file); 
	        echo "</li>\
"; 
	      } 
	      echo "</ul>\
"; 
	   } 
	} 
	globDir('/awww/afiles/na/'); 
? > 



```php




.

Just back from lunch :slight_smile:

Output from the above script:

* 2 - black- 0 - Na_001
      o 0 - red- 0 - cache
            + 1 - black- 0 - resized
            + 1 - red- 1 - thumb
* 2 - black- 1 - Na_002
      o 0 - red- 0 - cache
            + 1 - black- 0 - resized
            + 1 - red- 1 - thumb
* 2 - red- 2 - cache
      o 1 - black- 0 - resized
      o 1 - red- 1 - thumb

Screen dump: http://www.graabr.com/lzZjqI/

.

What I would like is what as a big black line would be not in bold,

Try this:


	function get_dir($dir, $i2=1, $spc='')
	{	
		$files		= scandir($dir);
		$spc     .= ' &nbsp; &nbsp; &nbsp; ';  
		
		foreach($files as $file):
			
			if (('.' != $file) AND ('..' != $file))
			{
				if (is_dir($dir .'/' .$file))
				{
					if ($i2==0)
					{
						echo "<br /><b style='color:#f00'>";
							echo $spc .$file .'';
						echo '</b>';
					}else{
						echo '<br /><b>'. $file .'</b>';
					}//endif	
					get_dir($dir .'/' .$file, 0, $spc);
				}	
			}//endif ..	
		endforeach;
	}//endfunc
	
	get_dir('/awww/_a-2-delete');
?> 


Result:
http://graabr.com/l4AJc0

.

closer !
now, only the 1st folder get black, it is not exactly what I wanted.
I will try to explain better;

All folder that as images files ( or any kind of files exept folder) in them should be red, the rest should be in black.