我需要从目录创建一个php菜单,我有这个文件夹:Publications。 我想将内部项目列为动态菜单。 这些项目如下所示: 的 newsletter_2013_1.pdf newsletter_2013_2.pdf newsletter_2014_1.pdf newsletter_2014_2.pdf
我需要创建的菜单必须是这样的: 请选择一年: - > 2013 - >问题#1 /问题#2 /问题#3
我会感激任何帮助我都能得到感谢!
更新 到目前为止我有这个代码:
$paths = new DirectoryIterator('/usr/apps/webdata/backend/assets/newsletters/temp');
echo '<div id=cssmenu>';
echo '<ul>';
echo '<li class=has-sub last><a href=#><span>Please select a Year</span></a>';
echo '<ul>';
$list = array();
foreach($paths as $file)
{
if($file->isDot())
continue;
$string = $file;
if(preg_match_all("#(\d{1,})#", $string, $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$issue = $match[0];
while (list($key, $value) = each($match))
{
//YEAR
if (strlen ($value) == 4)
{
$uyear = $value;
//echo "Year: $uyear \n";
//echo "<li class='has-sub'><a href='#'><span>".$uyear." </span></a>";
}
// ISSUE#
elseif (strlen ($value) == 1)
{
$uissue = $value;
//echo "Issue: $uissue \n";
//echo "<li class=last><a href=# onclick=myPDF('".$file."')> Issue# ".$uissue."</a></li>";
}
}
}
}
if(!isset($list[$uyear]))
{
$list[$uyear] = array();
echo "<li class='has-sub'><a href='#'><span>".$uyear." </span></a>";
}
echo '<ul>';
if(!in_array($uissue, $list[$uyear]))
{
$list[$uyear][] = $uissue;
//for($i=0;$i<4; $i++)
//{
echo "<li class=last><a href=# onclick=myPDF('".$file."')> Issue# ".$uissue."</a></li>";
//echo '<h2 style= margin-top: 20px;>Preview Issue# '.$uissue.' from '.$uyear.' </h2>';
//}
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
echo '</li>';
echo '</ul>';
echo '</div>';
但它不能按我需要的方式工作
更新2 我能够创建我的菜单,但现在每个问题#都在我的树上创建一个新的分支,我希望所有问题都在相同的数字下
答案 0 :(得分:0)
使用PHP Glob从目录中获取文件。然后使用foreach回显出一组链接...
我用它将图像动态加载到图库中。
<?php
$counter = 0; // Set counter to 0
foreach (glob("images/photo-strip/thumb/*.jpg") as $pathToThumb) { // Grab files from the thumbnail path and save them to variable
$th_filename = basename($pathToThumb); // Strip the thumbnail filename from the path
$filename = str_replace('th_', '', $th_filename); // Strip th_ from the filename and save it to a different variable
$pathToFull = 'images/photo-strip/' . $filename; // Rebuild the path to the full-size image
echo ("<section class=\"photo-box\"><a href=\"$pathToFull\"><img src=\"$pathToThumb\" /></a></section>"); // Echo the photobox
$counter++; // Increment counter by 1 until no file exists
}
?>
你的就像......
foreach (glob("path/2012/*.html") as $pathTo2012) {
$filename = basename($pathTo2012);
echo "<a href=\"$pathTo2012\">$filename</a>";
回复您的修改:
<?php
$dir = "directory/";
$year = "2009";
echo "<ul>";
foreach ($year as $label ) {
if (($year >= 2009) && ($year <= date(Y))) {
echo "<li>$label";
echo buildLinks;
echo "</li>";
}
}
$year++; //increment year by 1
function buildLinks () {
echo "<ul>";
foreach (glob($dir . $year . "/*.html") as $filepath) {
$filename = basename($filepath);
echo "<li><a href=\"$filepath\">$filename</a></li>";
}
echo "</ul>";
}
echo "</ul>";
?>
请注意,这是未经测试的,并不适合您的情况。可能需要进行一些修改。不幸的是,我目前还没有测试PHP的情况。
答案 1 :(得分:0)
这可能是一种矫枉过正,但我希望它有所帮助:
if(!is_readable($DirName)) { # Make sure the dir is readable before going on
printf("You do not have permission to read:") ;
printf("<blockquote><code>\n%s\n</code></blockquote>",$DirName) ;
}
else if(!is_dir($DirName)) { # Make sure it is a directory
printf("The requested item is not a directory:") ;
printf("<blockquote><code>\n%s\n</code></blockquote>",$DirName) ;
}
else if(($FileList = scandir($DirName))==FALSE) { # Are we able to scan the directory
printf("<h2>Cannot get listing</h2>\n") ;
printf("Cannot get list of the contents of the directory:") ;
printf("<blockquote><code>\n%s\n</code></blockquote>",$DirName) ;
}
else {
$FileCount = sizeof($FileList) ;
printf("Current working directory is:<blockquote><pre>%s</pre></blockquote>\n",posix_getcwd()) ;
printf("Searching <blockquote><pre>%s</pre></blockquote>\n",$DirName) ;
printf("<ul>\n") ;
for($Count=0;$Count<$FileCount && $Count<$CountLimit;$Count++) {
printf("<li>%s</li>\n",$FileList[$Count]) ;
}
# The number of files shown was limited by $CountLimit, if there are more files beyond what was shown above then show how many
$Difference = $FileCount-$CountLimit ;
if($Difference>0)
printf("<li><i>…%d more file%s/director%s</i></li>",$Difference,$Difference==1?"":"s",$Difference==1?"y":"ies") ;
printf("</ul>\n") ;
}
?>
答案 2 :(得分:0)
所以在我发现问题后,我能够生成一个带有我的值的CSS菜单,这是最终的代码:
echo '<div id=cssmenu>';
// This If is to check that the array is not empty
if(count($menu) > 0) {
echo '<ul>';
echo ' <li class=has-sub last><a href=#><span>Please Select a Year:</span></a>';
echo ' <ul>';
foreach($menu as $uyear => $docs)
{
echo "<li class='has-sub'><a href='#'><span>".$uyear." </span></a>";
echo ' <ul>';
foreach($docs as $filename)
{
$a= "Newsletter_$uyear";
$b="_$filename.pdf";
$myissue = $a.$b;
echo "<li class=last><a href=# onclick=myPDF('".$myissue."')> Issue# ".$filename."</a></li>";
}
echo ' </ul>';
echo ' </li>';
}
echo ' </ul>';
echo ' </li>';
echo '</ul>';
}
echo '</div>';