Jump to content

[SOLVED] populating drop down menu with directory list, navigation


iamcaper

Recommended Posts

Hey Gang,

 

Thanks to those of you who've tried to help so far.

 

I have the following code that populates a drop down with a directory list of files of a specified type:

 

<?php

//Images to ignore, File name ONLY, no extension, seperate by a comma, no spaces

//Example: $ignoreimages = "BlahImage,ImageMeh";

$ignoreimages = "";

 

 

$direc = "path";

$dh = opendir($direc);

while (false !== ($filename = readdir($dh))) {

$files[] = $filename;

}

 

$output .= "<form name='FileList' method='post' action=''>

  <select name='select'>";

 

//Accepted File Types To display

$acceptedfiles = array(1 => "jpg", 2 => "png", 3 => "gif", 4 => "tif");

 

//Build the array that holds the files to exclude.

$ignore = explode(",", $ignoreimages);

 

$FileCount = count($files);

for($i=0; $i <= $FileCount; $i++){

$file1 = explode(".", $files[$i]);

$blah = count(file1);

$extension = $file1[$blah];

$filename = $file1[$blah -1];

if(in_array($extension, $acceptedfiles)) {

if(!in_array($filename, $ignore)){

$output .= "<option value='". $files[$i] ."'>". $files[$i] ."</option>";

}

}

}

//Close the file out

$output .= "</select>

</form>";

 

//output the HTML

echo $output;

?>

 

I'd like to expand this so that it's a chained select which is automatically populated by directory contents.  For example, the main directory could be:

 

option1

option2

option3

 

If you select "option2" it will give you options in the next drop down menu, which has been populated by a folder within the "option2" folder. 

 

Does this make sense or am I not explaining it properly?

 

Eventually, I want to be able to have the selected file (within the final drop down) open in the interface I'm developing.

Link to comment
Share on other sites

Yes, it makes sense. But, you need to make a decision.

 

Either 1) You need to have the page refresh every time the user changes the folder so the page can query the contents of that folder or 2) You need to query all of the possible folders and get all of the possible files. then dump the results into a javascript array. Then you could have the contents of the 2nd select list change dynamically w/o a page refresh.

 

Well, you could also use AJAX to repoulate the select list w/o a refresh. But, I have found problems with these types of implementations due to a slight delay.

Link to comment
Share on other sites

I guess that considering the sheer amount of files that I'll potentially be faced with, it would be best to refresh.  The size would probably be better handled by a database but I want to do it this way because the files will be constantly changin.

 

So, to make a decision, I'll go with 1 and do a refresh. 

 

 

Link to comment
Share on other sites

Can anybody else step in here to offer assistance?

 

I'd like to have drop down's, the same as in Xin Yang's Chained Select, but I would like for the menu's to be automatically filled by file lists from specified directories.  If you would like more info on what I'm trying to accomplish please feel free to message me.

 

Thanks

Link to comment
Share on other sites

Here is a working example. Just change the $parent_directory value to the relative path where the folders reside.

 

<?php 

$parent_directory = '.';
$file_types = 'jpg,png,gif,tif';

//===================================================//
// FUNCTION: directoryToArray                        //
//                                                   //
// Parameters:                                       //
//  - $root: The directory to process                //
//  - $to_return: f=files, d=directories, b=both     //
//  - $file_types: the extensions of file types to   //
//                 to return if files selected       //
//===================================================//
function directoryToArray($root, $to_return='b', $file_types=false) {
  $array_items = array();
  if ($file_types) { $file_types=explode(',',$file_types); }
  if ($handle = opendir($root)) {
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {

        $add_item = false;
        $type = (is_dir($root. "/" . $file))?'d':'f';
        $name = preg_replace("/\/\//si", "/", $file);

        if ($type=='d' && ($to_return=='b' || $to_return=='d') ) {
          $add_item = true;
        }

        if ($type=='f' && ($to_return=='b' || $to_return=='f') ) {
          $ext = end(explode('.',$name));
          if ( !$file_types || in_array($ext, $file_types) ) {
            $add_item = true;
          }
        }

        if ($add_item) {
          $array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root);
        }
      }
    } // End While
    closedir($handle);
  } // End If
  return $array_items;
}



if (isset($_POST[pickfile])) {

  // User has selected a file take whatever action you want based
  // upon the values for folder and file

} else {

    echo '
<html>
<head>
  <script type="text/javascript">
    function changeFolder(folder) {
      document.pickFile.submit();
    }
  </script>
</head>

<body>';


echo "<form name=\"pickFile\" method=\"POST\">\n";

$directoryList = directoryToArray($parent_directory,'d');

echo "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n";
foreach ($directoryList as $folder) {
  $selected = ($_POST[folder]==$folder[name])? 'selected' : '';
  echo "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n"; 
}
echo '</select><br><br>';

$working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name];

$fileList = directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types);

echo "<select name=\"file\">\n";
foreach ($fileList as $file) {
  echo "<option value=\"$file[name]\">$file[name]</option>\n"; 
}
echo '</select><br><br>';

echo "<button type=\"submit\" name=\"pickfile\">Submit</button>\n";

echo "</form>\n";
echo "</body>\n";
echo "</html>\n";

}
?>

Link to comment
Share on other sites

Now I'm on the right track, thanks mjdomato!

 

Now I just have to figure out how to get subsequent folders/files displayed in additional menu's, all based on what you select in the previous menu.  hehe

 

You rock!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.