Drop down list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • -saturn-
    Member
    • Jan 2001
    • 93

    Drop down list

    What I want to do is simple, but since I'm just starting out in PHP, I'd like your help.

    I want to read a directory for available files and then get the filenames and put them in a drop down list.

    Whatever the user will select will be the value of the variable $selected.

    I already found the code to read directories:

    Code:
    function list_files() {
    
    global $the_path;
    
            $handle = dir($the_path);
            print "\n<b>Uploaded files:</b><br>";
            while ($file = $handle->read()) {
                    if (($file != ".") && ($file != "..")) {
                            print "\n" . $file . "<br>";
               }
            }
            print "<hr>";
    }
    My guess is that the whole trick would be to use something like
    Code:
    $cnt=++;
    <option value="$cnt">$file</option>";
    inside the loop, where $cnt is a counter used to add a number to each new option for the list.

    The result should be a form with the list and a "Select" button beside it.

    Can someone help me put it together?

    Thanks in advance.
    Last edited by -saturn-; Sun 1 Apr '01, 9:18am.
  • -saturn-
    Member
    • Jan 2001
    • 93

    #2
    For those interested, here's the solution:

    PHP Code:
    <?php


    function list_files($the_path) {

            
    $cnt 0;
            
    $handle dir($the_path);
            while (
    $file $handle->read()) {
                    if ((
    $file != ".") && ($file != "..")) {
                            
    $cnt++;
                            
    $options .= "<option value='$cnt'>$file</option>n";
               }
            }
            return 
    $options;
    }
    ?>

    <form name="files" action="">
    <select name=selected>
    <? echo list_files("/www/stuff/"); ?>
    </select>
    </form>

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...