用目录填充选择框
Populate selectbox with directories
我有以下代码。我正在寻找评论中的内容,即用目录名称填充选择框。我该怎么做?
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//extract directory names and populate selectbox
}
}
一个快速而肮脏的解决方案,假设结果是您要显示的目录:
echo '<select id="directories" name="directories">';
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo '<option value="' . $result . '">' . $result . '</option>';
}
}
echo '</select>';
也许我没有理解这个问题,但肯定是:
<?php
$selectString = '<select>';
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') {
continue;
}
if (is_dir($path . '/' . $result)) {
$selectString .= "<option>{$result}</option>";
}
}
$selectString .= "</select>"
然后您可以将 $selectString
放在任何需要的地方。
也许最简单:
echo "<select name=\"something\">";
foreach(glob("repository/{$name[0]}/*", GLOB_ONLYDIR) as $dir) {
echo "<option>$dir</option>";
}
echo "</select>";
我有以下代码。我正在寻找评论中的内容,即用目录名称填充选择框。我该怎么做?
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//extract directory names and populate selectbox
}
}
一个快速而肮脏的解决方案,假设结果是您要显示的目录:
echo '<select id="directories" name="directories">';
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo '<option value="' . $result . '">' . $result . '</option>';
}
}
echo '</select>';
也许我没有理解这个问题,但肯定是:
<?php
$selectString = '<select>';
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') {
continue;
}
if (is_dir($path . '/' . $result)) {
$selectString .= "<option>{$result}</option>";
}
}
$selectString .= "</select>"
然后您可以将 $selectString
放在任何需要的地方。
也许最简单:
echo "<select name=\"something\">";
foreach(glob("repository/{$name[0]}/*", GLOB_ONLYDIR) as $dir) {
echo "<option>$dir</option>";
}
echo "</select>";