I want to recursively parse a directory and then assign a random CSS style color to each directory that will correspond to the same color value that can shown for the CSS style for it's contents. A color-coded directory system - I'm then using Iosotope.js to sort it, so I need to clean up and use the directory name for it's class.
I hope my explanation is clear - $thefolders is a list of directory links and $thelist is the files in the directory.
Problem with the below is that it lists a directory for each instance of a file in the directory. So if the are 3 items in the dir, the directory name gets listed 3 times (with correct color correspondence to the containing item).
<?php
$it = new RecursiveDirectoryIterator('audiofiles');
$display = Array ( 'mp3', 'MP3' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
{
$path = $file->getPath();
$folder = ltrim($path, "/audiofiles");
$name = $file->getbasename();
$color = $folder.'#' . strtoupper(dechex(rand(0,10000000)));
$foldercolor = ltrim($color,$folder);
$thefolders .= '<ul><div STYLE = background-color:'.$foldercolor.'"><a class="black" href="#" data-filter=".'.$folder.'">'.$folder.'</a></div> </ul>';
$thelist .= '<div class="'.$folder.'"><li STYLE = "background-color:'.$foldercolor.'" ><a href="'.$file.'">'.rtrim($name,'.mp3').'</a></li></div>';
}
}
?>
<ul id="filters">
<ul><a class="black" href="#" data-filter="*">Show All</a> </ul>
</ul>
<?=$thefolders?>
<br /><br /><br /><br />
<ul class="playlist">
<div id="container">
<?=$thelist?>
</div>
</ul>