Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php创建动态菜单而不使用数据库,只使用目录中的文件夹?_Php_Html_Tree - Fatal编程技术网

使用php创建动态菜单而不使用数据库,只使用目录中的文件夹?

使用php创建动态菜单而不使用数据库,只使用目录中的文件夹?,php,html,tree,Php,Html,Tree,我想感谢任何能够以任何可能的方式帮助我的人,我对php/编码非常陌生,所以我甚至不确定我是否走上了正确的道路 我想知道是否可以只使用php和html创建两步或动态下拉菜单,用目录中的文件夹填充第一个下拉菜单 到目前为止我有 <?php // Set the path of the dir you want displayed... $path="./track"; $handle=opendir($path); while ($file=readdir($handle)) { ec

我想感谢任何能够以任何可能的方式帮助我的人,我对php/编码非常陌生,所以我甚至不确定我是否走上了正确的道路

我想知道是否可以只使用php和html创建两步或动态下拉菜单,用目录中的文件夹填充第一个下拉菜单

到目前为止我有

<?php
// Set the path of the dir you want displayed...
$path="./track";
$handle=opendir($path);
while ($file=readdir($handle))
{
    echo "\t<option value='".$file."'>".$file."</option>\n";
}

使用ajax和文件系统功能。

您的意思是第二个下拉列表取决于第一个下拉列表的值吗?这就是问题所在吗?使用ajax和文件系统函数。是的,我想尝试让第二个下拉列表取决于第一个下拉列表的值。我已经创建了两个下拉菜单。我可以让第一个返回目录中的文件,但是如何让第二个响应呢?例如:选择一个事件:请先选择一个事件:我选择一个事件,比如说全明星。然后它会显示All Star Select a Race:(然后让我从第一个文件夹所在的目录中选择一些内容)您使用的是什么版本的PHP?在新版本的PHP中有更好的解决方案,效率更高。我认为基于web服务器套件版本1.95的PHP 5.2.1谢谢!我非常感谢你的帮助你是一个伟大的人!简单一点:对于第二个下拉菜单,当我选择某个内容时,是否有方法使其自动打开?@user3071439检查更新的答案,这应该可以解决您的问题。再次感谢您的帮助,您是一个真正的救命恩人!事实上,我现在已经尝试了更新的代码,它似乎已经删除了第二个下拉菜单选项,但我会尝试使用它来让它工作!谢谢一群人@用户3071439如果这对您有效,请单击我问题顶部旁边的“复选标记”,将其标记为“已接受”。你也可以点击“向上箭头”。我会投票给你,但我需要更多的代表性是它所说的。。。但是我点击了复选标记!谢谢你,伙计
<?php
/**
 * Check if we have submit the folder 
 * Check if we have submit the file
 */
$post_folder = isset($_POST['folder']) ? $_POST['folder']: null;
$post_file = isset($_POST['file_name']) ? $_POST['file_name'] : null; 

/**
 * Built out the Folder submission form
 */
// Set the path of the dir you want displayed...
$path="./";
echo '<form method="POST" action="', $_SERVER['PHP_SELF'],'">';
echo '<select name="folder">';
foreach (new DirectoryIterator($path) as $asset){
    if($asset->isDot()) continue;
    if($asset->isDir()){
       /**
        * If we've posted which folder we want to view the contents of
        * then automatically make that selected in the dropdown on page load
        */
        $selected = (isset($post_folder) && $post_folder == $asset->getFileName()) ? 'selected' : '';
        echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";    
    }
}
echo '</select>';

if(isset($post_folder)){
    echo '<select name="file_name">';
    $folder_to_iterate = $path.$post_folder;
    foreach (new DirectoryIterator($folder_to_iterate) as $asset){
        if($asset->isFile()){
           /**
            * If we've posted which folder we want to view the contents of
            * then automatically make that selected in the dropdown on page load
            */
            $selected = (isset($post_file) && $post_file == $asset->getFileName()) ? 'selected' : '';
            echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";    
        }
    }
    echo '</select>';
}

$btn_text = isset($post_folder) ? 'Show File Contents' : 'Show Folder Contents';
echo '<input type="submit" value="', $btn_text ,'"/>';
echo '</form>';

if(isset($post_file)){
    $file_to_read = $path.$post_folder.'/'.$post_file;
    echo '<pre>';
    echo htmlentities(file_get_contents($file_to_read));
    echo '</pre>';
}