SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying of files in a directory in a select menu

    Hi all,

    I am trying to create a snippet of developing a viewing files in a select/list menu being dynamically created so it displays all files in a certain directory

    only problem is that when i do this i get this back for some reason

    <br/> contents of the directory:rank<br/> <select name="cnt">
    <option value="">Please a file</option><option value=1">1</option><option value="">Please a file</option><option value=1">1</option><option value="">Please a file</option><option value=1">1</option> </select>


    now why am i getting 1 for value and name of the value??

    can anyone help me out here please,

    Here is the code i am using

    PHP Code:
        $dir="rank";
        $odir=opendir($dir);//will open the directory
        echo "<br/> contents of the directory:".$dir."<br/>";
        ?>
        <select name="cnt">
        <?
        
    while($contents=readdir($odir))//read the contents of the directory
        
    {
            if(
    $contents="." || $contents="..")
            {
                echo 
    "<option value=\"\">Choose a file</option>";    
                echo 
    "<option value=".$contents."\">".$contents."</option>";
            }
            
        }
    ?>
        </select>
        <?
        closedir
    ($odir);//closes the directory that is open
    Thanks,William

    What am i doing wrong???

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    personally I would use glob() rather than opendir.
    PHP Code:
    <?php
    $dir 
    '.';
    $option '';
    $files glob($dir.'/*');
    foreach(
    $files as $key=>$data) {
        
    $option .= '<option>'$data .'</option>';
    }

    ?>
    <select name="test">
        <?php echo $option?>
    </select>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    The quick answer is that you're using = where you should be using == in your if statement.

    Off Topic:

    I did have a long, rambling answer prepared as to why the value of $contents echoes as 1 but I closed the browser tab and lost it all.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •