I have a php function that is returning a list of files from a folder (image files), I would like to know if it possible for me to get a "blank" entry added into the dropdown list (so I can "not select" something in the list), as well as the files it finds in the folder, here is the function.
<?php
$folder = '../Pic/Upload';
echo '<select name="sabpic">'."
".
dropdown(image_filenames($folder), @$_POST['image']).
'</select>'."
".'</br></br>';
function image_filenames($dir)
{
$handle = @opendir($dir)
or die("I cannot open the directory '<b>$dir</b>' for reading.");
$images = array();
while (false !== ($file = readdir($handle)))
{
if (eregi('\.(jpg|gif|png)$', $file))
{
$images[] = $file;
}
}
closedir($handle);
return $images;
}
function dropdown($options_array, $selected = null)
{
global $sabpicname;
$return = null;
foreach($options_array as $option)
{
$return .= '<option value="'.$option.'"'.
(($option == $sabpicname) ? ' selected="selected"' : null ).
'>'.$option.'</option>'."
";
}
return $return;
}
?>
So currently I would get a dropdown with
I would like to have
Thanks for the help.
According to your code just replace $return = null;
with $return = "<option></option>";
function dropdown($options_array, $selected = null)
{
global $sabpicname;
$return = "<option></option>";
foreach($options_array as $option)
{
$return .= '<option value="'.$option.'"'.
(($option == $sabpicname) ? ' selected="selected"' : null ).
'>'.$option.'</option>'."
";
}
return $return;
}
You can add the initial option like this ( I think ) that will initially dislay "Please Select" but is then no longer selectable after a choice has been made
function dropdown($options_array, $selected = null) {
global $sabpicname;
$return = "<option disabled=true hidden='hidden'>Please Select";
foreach($options_array as $option) {
$return .= '<option value="'.$option.'"'.
(($option == $sabpicname) ? ' selected="selected"' : null ).
'>'.$option.'</option>'."
";
}
return $return;
}