【发布时间】:2011-03-15 14:44:52
【问题描述】:
我一直在使用以下内容从特定目录输出导航,但需要稍微调整一下。如何将class="first" 添加到输出的第一个<li> 项目?对于我的一生,我似乎无法弄清楚如何!
<?php
function navigation($path) {
if ($handle = opendir($_SERVER["DOCUMENT_ROOT"].$path)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && $file != 'index.php') {
$label = str_replace('.php', '', $file);
$label = str_replace("-", " ", $label);
$label = ucfirst($label);
$file = str_replace('.php', '/', $file);
$links[] = '<li><a href="' . $path . $file . '" title="' . $label . '">' . $label . '</a></li>' . "\n";
sort($links);
}
}
foreach($links as $link) {
echo ($link);
}
closedir($handle);
}
}
?>
然后我调用 <?php navigation("/directory-name/"); ?> 我希望它出现在页面中的位置。
这目前会输出如下内容:
<ul>
<li><a href="Path to file" title="Label">Label</a></li>
<li><a href="Path to file" title="Label">Label</a></li>
</ul>
【问题讨论】:
标签: php list file directory navigation