0

我需要从一个目录创建一个 php 菜单,我有这个文件夹:Publications。我想将里面的项目列为动态菜单。项目如下所示: newsletter_2013_1.pdf newsletter_2013_2.pdf newsletter_2014_1.pdf newsletter_2014_2.pdf

我需要创建的菜单必须是这样 的:请选择年份:-> 2013 -> Issue# 1 / Issue# 2 / Issue # 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 我能够创建我的菜单,但现在对于每个问题 # 都会在我的树上创建一个新分支,我希望所有问题都在同一个编号下

4

3 回答 3

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。

于 2013-04-08T17:06:58.227 回答
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>&hellip;%d more file%s/director%s</i></li>",$Difference,$Difference==1?"":"s",$Difference==1?"y":"ies") ;
    printf("</ul>\n") ;
}
?>
于 2013-04-08T17:18:47.217 回答
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>';    
于 2013-04-09T13:47:37.787 回答