[英]Give unique class to list item in php array
I have created an array that displays the all the files within several sub-directories of one single directory (A directory tree essentially). 我创建了一个数组,用于显示一个目录(本质上是目录树)的几个子目录中的所有文件。 The files are all links that open the file within that directory when clicked.
文件是单击后在该目录中打开文件的所有链接。
The issue I am having is that I want to be able to style my results, however all sub-directories and file names have the same class (as they are generated using the same code). 我遇到的问题是我希望能够设置结果样式,但是所有子目录和文件名都具有相同的类(因为它们是使用相同的代码生成的)。
Here is my php: 这是我的PHP:
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol class="song">';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li class="title">';
if(is_dir($dir.'/'.$ff)){
echo $ff;
listFolderFiles($dir.'/'.$ff);
}else{
echo '<a href="'.$dir.'/'.$ff.'" target="_blank">'.$ff.'</a>';
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('Current Songs');
?>
And the html generated is this: 和生成的HTML是这样的:
<ol class="song">
<li class="title">
Sub Directory Folder 1
<ol class="song">
<li class="title">
<a href="Current Songs/Song1.mp3"></a></li>
<li class="title">
<a href="Current Songs/Song1.pdf"></a></li>
</ol>
</li>
<li class="title">
Sub Directory Folder 2
<ol class="song">
<li class="title">
<a href="Current Songs/Song2.mp3"></a></li>
<li class="title">
<a href="Current Songs/Song2.pdf"></a></li>
</ol class="song">
</li>
</ol>
</li>
As you can see, each entry is made up of 2 ordered lists, however they both have the same class, as do all the list items, which makes it near impossible to do any styling to. 如您所见,每个条目都由2个有序列表组成,但是它们与所有列表项一样都具有相同的类,因此几乎不可能对其进行任何样式设置。
What I want is the following html markup to be generated: 我想要的是将生成以下html标记:
<ol class="song">
<li class="title">
Sub Directory Folder 1
</li> <!--END TITLE HERE-->
<ol>
<li>
<a href="Current Songs/Song1.mp3"></a></li>
<li>
<a href="Current Songs/Song1.pdf"></a></li>
</ol>
</ol> <!--END SONG OL HERE-->
Can anyone please give some advice on what php changes I need to make to my code in order to output the above. 任何人都可以对我需要对我的代码进行哪些PHP更改以输出上述内容提供一些建议。
you can pass the class name also as a prameter 您也可以将类名作为参数传递
function listFolderFiles($dir,$myclass){
$ffs = scandir($dir);
echo '<ol class="song'.$myclass.'">';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li class="title'.$myclass.'">';
if(is_dir($dir.'/'.$ff)){
echo $ff;
listFolderFiles($dir.'/'.$ff);
}else{
echo '<a href="'.$dir.'/'.$ff.'" target="_blank">'.$ff.'</a>';
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('Current Songs','_myclass');
listFolderFiles('Current Songs','');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.