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. For that I think my option value should luke something like this: ($pageImage is the value loaded from 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.