[英]PHP: How to list files of a folder in a dropdown and then select one of them according to mysql entry
I'm trying to create a dropdown listing the content of a specific folder. 我正在尝试创建列出特定文件夹内容的下拉列表。 For that I'm using this code which works fine:
为此我使用这个代码工作正常:
<select name="level">
<?php
$dirname = "../images/page_images/";
$dirhandle = opendir($dirname);
while($file = readdir($dirhandle))
{
if ($file != "." && $file != "..")
{
if (is_file($dirname.$file))
{
echo "<option value='" . $file .">" . $file . "</option>";
}
else
{
echo "mappe: " . $file . "<br>";
}
}
}
?>
</select>
Now I want the dropdown bar to check a mysql entry and match it so that the file written in the database is the one that is selected. 现在我希望下拉栏检查一个mysql条目并匹配它,以便在数据库中写入的文件是所选的文件。 For that I think my option value should luke something like this: ($pageImage is the value loaded from mysql)
为此,我认为我的选项值应该是这样的:($ pageImage是从mysql加载的值)
<option <?php echo ($pageImage) == $file ? "selected" : "" ?> value="$file">$file</option>
My question is, how do I merge these two scripts together? 我的问题是,如何将这两个脚本合并在一起?
Try this one easy way to list file name from a folder. 尝试这一种简单的方法来列出文件夹中的文件名。
<select name="level">
<?php
foreach (glob("../images/page_images/*.{jpg,gif}") as $filename) {
echo "<option value='" . $filename .">" . $filename . "</option>";
}
?>
</select>
echo "<option value='{$file}'" . ($pageImage == $file ? " selected" : "") . ">{$file}</option>";
or 要么
echo '<option value="' . $file . '"' . ($pageImage == $file ? " selected" : "") . '>' . $file . '</option>';
Docs: PHP String Operators 文档: PHP字符串运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.