填充下拉列表显示为空


Populate Dropdown list appears empty

我感谢大家给我的帮助。我现在的下拉框似乎在变化,但它是空白的。我正在使用的当前编码编辑如下:

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select> 
<?php 
$directory = realpath(dirname(FILE)) . '/../users/' . $_SESSION['username']; 
$files = scandir( $directory ) ;
foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
    echo "<option>" . $file . "</option>";
}
}
?>
</select>

当我检查页面上的空下拉元素时,我得到这个:

警告: scandir(/home/revo/public_html/evo/../users/Addiction) [function.scandir]:无法打开目录:第 117 行的/home/revo/public_html/evo/codesaveindex.php 中没有这样的文件或目录

警告:scandir() [function.scandir]:(errno 2):第 117 行的/home/revo/public_html/evo/codesaveindex.php 中没有这样的文件或目录

警告:在第 119 行的/home/revo/public_html/evo/codesaveindex.php 中为 foreach() 提供的参数无效

你的问题是你输出一个<select>标签,然后你开始构建一个带有串联$Selection的 php 变量(这是错误的,因为你从来没有在连接之前初始化过变量)。

看看这是否有效:

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<?php
// match all files that have .txt extension
$file_matcher = realpath(dirname(__FILE__)) . '/../users/' . $_SESSION['username'] . '/*.{txt}';
//Initialize variable $Selection
$Selection = '<select size="1" name="CodeList" id="CodeList" onchange="CodeChange();"><option value="0">(Add New Code)</option>';
foreach( glob($file_matcher, GLOB_BRACE) as $file ) {
    $file_name = basename($file);
    //Concatenate variable $Selection
    $Selection .= "<option value='$file'>$file_name</option>'n";
}
//Finish HTML source concatenation on $Selection
$Selection .= '</select>';
//Output the HTML
echo $Selection;
?>
我认为

这应该可以用您想要的路径替换 DIR

<select> 
<?php
$directory = __DIR__;
$files = scandir( $directory ) ;
foreach( $files as $file )
{
    if ( ! is_dir( $file ) )
    {
        echo "<option>" . $file . "</option>";
    }
}
?>
</select>

你有一个有点复杂的目录路径。

$file_matcher = realpath(dirname(__FILE__)) . '/../users/' . $_SESSION['username'] . '/*.{txt}';

如果目录无效,您将找不到任何文件,也不会有填充的选择。

只是为了测试,尝试一个简单的目录 - 没有任何变量 - 看看它是否有效。我敢打赌,在尼洛特的帮助下,它会的。+1 到尼洛特。