Forum Moderators: coopster

Message Too Old, No Replies

Populate Drop Down List

from folder names in a directory

         

SantaMonica

11:44 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



I'm trying to populate a drop down list with the names of folders in a directory. If it matters, it's for a personal image uploading script. The form itself is in a password protected section, so security isn't as important as it would be if it were in a public section.

Here's my existing form:


<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><select>
(Here is where the folder names will be)
</select></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>

If, for example, I have three folders titled "avatars", "banners", and "cats", I'll need the options to look like this:


<option value="avatars">avatars</option>
<option value="banners">banners</option>
<option value="cats">cats</option>

It needs to be dynamic, so coding it in static HTML isn't an option.

And then, I'll need to set the "value" to a variable...which I'm not 100% certain how to do.

Thanks in advance for any assistance. :)

andrewsmd

1:32 pm on Oct 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



to populate your form do something like this. Let's assume your folders are in c:\some folder (but that could be anything including a network path).
Alright first
function populateDropdown(){
$path = glob("c:\\some folder\\*"); //the double \\ is not a typo you
//have to escape it
//if the path cannot be found
if(!($path)){
echo("The path cannot be found");
return;
}//if

//the path is valid
else{
foreach($path as $i){

echo("<option value=$i>$i</option>");

}//foreach
}//else
}//populateDropdown
now in your php file where the html table is put this call in
<select>
<?php populateDropdown(); ?>
</select>
Make sure you have that function somewhere in that file or in the php file you call. Just a note if there are files in that directory i.e. if avatars, banners, and cats, are there but so is test.txt that will be put in the dropdown also. to work around that you need to us the is_dir function. Let me know if you need anything else. I didn't test this so there may be a typo but I know the logic is right I deal with directories in PHP every single day.

andrewsmd

1:38 pm on Oct 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To get the value of the dropdown I'm assuming you know that you have to have some kind of button. I'm going to assume you used a submit button with the name "submit" you also need to give your select box a name im going to use <select name = "dropdown">

//if they clicked the submit button
if(isset($_POST['submit'])){

$dropdownValue = $_POST['dropdown'];

//if the user selected avatars then the
//value of $dropdownValue will be == avatars etc.

//find the value of the dropdown
if($dropdownValue == "avatars"){

echo("You selected avatars");

}//if avatars

elseif($dropdownValue == "banners"){

echo("You selected banners");

}//elseif banners

elseif($dropdownValue == "cats"){

echo("You selected cats");

}//elseif cats
}if isset

SantaMonica

3:46 pm on Oct 3, 2008 (gmt 0)

10+ Year Member



Awesome! Thanks! It works great.

Just one more thing...is it possible to clean up the values so that it's just a single word instead of the entire path?

It's not overly important so if it's too difficult, don't bother with it. It will just make it look nicer.

andrewsmd

4:07 pm on Oct 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure is put this function in your code also I'm going to give you a couple formatting tips because if you echo like this when you go to view source it will be all on one line
//this function takes in a path and retrieves the filename
//based on the last text split in a path
function getFileName($path){

//an array that is created from the individual folders
$arrName = explode("\\", $path);

//get the filename by grabbing the last thing in the array
$fileName = end($arrName);
return $fileName;

}//getFileName
Then in your foreach do this
foreach($path as $i){

//the \t will tab it over.
//if you want 2 then put \t\t
//the \n prints a carriage return so it is not all on one line in your html

echo("\t<option value=".getFileName($i).">".getFileName($i)."</option>\n");

}//foreach