-3

我正在尝试编写多级页面菜单导航但无法正常工作,我怀疑这与 RecursiveIteratorIterator 行有关,任何帮助将不胜感激。我正在考虑这样的解决方案,但也许我走得很远。谢谢您的帮助。这是我的代码:

<ul id="nav">
       <?php
 //searchWebsitePagesPath("blog.php");
 ?>
        <li><a href="<?php  echo searchWebsitePagesPath('index.php'); ?>">Home</a></li>
<!--        <li><a href="index.php" id="here">Home</a></li>      -->
        <li><a href="<?php  echo searchWebsitePagesPath('blog.php'); ?>">Journal</a></li>
        <li><a href="<?php  echo searchWebsitePagesPath('gallery.php'); ?>">Gallery</a></li>
        <li><a href="<?php  echo searchWebsitePagesPath('files_upload.php'); ?>">Upload</a></li>
        <li><a href="<?php  echo searchWebsitePagesPath('contact.php'); ?>">contact</a></li>
        <li><a href="<?php  echo searchWebsitePagesPath('register.php'); ?>">Register</a></li>
    </ul>
<?php

    function searchWebsitePagesPath($pageName){
        // echo ("$pageName");
        //    $files = "";
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));

        $my_path="";
        $myArray="";
        $pathReplace="";
        $finalPathReplace="";
        foreach ($files as $file) {

            if($file->getFilename() === $pageName ){
                $pathReplace=str_replace (str_replace('/','\\',$_SERVER['DOCUMENT_ROOT']),'',$file->getRealPath());
                $finalPathReplace = $pathReplace ;
                //           echo $finalPathReplace;
                $myArray=explode("\\",$pathReplace) ;
                $count= count($myArray);
                $reverted = new ArrayIterator(array_reverse($myArray));
                $server_path="" ;
                for($i=1;$i<$count;$i=$i+1 ) {
                    $server_path =$server_path."../";
                }

                $my_path=$server_path.$reverted[$count-1];
            }

        }

        $my_path="\"$my_path\"" ;
        $files = "";
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("$my_path"));
        foreach ($files as $file) {

            if($file->getFilename() === $pageName ){
                $pathReplace=str_replace (str_replace('/','\\',$_SERVER['DOCUMENT_ROOT']),'',$file->getRealPath());
                $finalPathReplace ='/'.str_replace ('\\','/',$pathReplace)  ;
                return $finalPathReplace;
            }

        }

    }

    ?>
4

1 回答 1

0

如果您有这样的资源密集型代码来生成菜单,这将严重影响服务器性能。

于 2012-04-12T04:48:43.717 回答