Calling files into a selectable list problem. Please help!

Discussion in 'PHP' started by DK42, Jul 10, 2009.

  1. #1
    I have a PHP function that calls for the file names in a directory. That all works perfectly fine. The code is below:
                        <?php
     
                    $string="";
                    $fileCount=0;
                    $filePath=$PATH.(TEMPLATEPATH . '/'); # Specify the path you want to look in. 
                    $dir = opendir($filePath); # Open the path
                    while ($file = readdir($dir)) { 
                      if (eregi("\.php",$file)) { # Look at only files with a .php extension
                        $string .= "$file,<br />";
                        $fileCount++;
                      }
                    }
                    if ($fileCount > 0) {
                      echo sprintf($string,$fileCount);
                    
                    
                    }
                    ?>
    Code (text):
    Below that, I have some code that pulls in an array to use as a select list (this is for a wordpress theme options page) which is this:
        <?php foreach ($file['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
    Code (text):
    I have been trying all day without any success to bring the two together. I want a user to be able to drop files into a folder and then have those files listed in a drop down menu so they can select one.

    I can get the two features working side by side, but cannot bring them together. Im tearing my hair out here with this and I havent got much hair to start with!

    Could anyone possibly help, im really stuck on this one.

    Thanks if anyone can help! Feel free to say if I havent explained myself very well.:):):confused:
     
    DK42, Jul 10, 2009 IP
  2. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $directory = "path to dir";

    $handler = opendir($directory);
    while ($file = readdir($handler)) {
    if ($file != '.' && $file != '..')
    $results[] = $file;
    }
    closedir($handler);

    echo "<select>";
    foreach ($results as $option) {
    echo "<option>".$option."</option>";
    }
    echo "</select>";
     
    Goramba, Jul 11, 2009 IP
    DK42 likes this.
  3. DK42

    DK42 Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Goramba you are a legend! Thank you very very very much!

    Sorry for the late reply, have been away, am really happy you showed me that, works really well. Thank you very much!
     
    DK42, Jul 14, 2009 IP
  4. DK42

    DK42 Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    OK, am actually stuck again!:(

    That code works just fine, brings up a cool selectable list of the files I need, but I have a problem. I cannot get it to write to the database.

    Im reworking a Wordpress theme and it already has a select list bit that works, its just you have to add the option names in the code, where as I want to search for files.

    On the working select it has this between the select links, but I cant figure out how to bring it into the code and make it work

    <select style="width:250px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
    	<?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
    Code (markup):
    If anyone can help I would be really really pleased! Thanks!:)
     
    DK42, Jul 14, 2009 IP
  5. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm confused, the only thing that bit of code does is display the $option (your new code is for the file list) and if ID matches the $option then it's automatically selected, or if $value['std'] matches $option it is selected as default. The only important part is the <select> portion. Change what I posted from
    echo "<select>";

    to

    echo '<select style="width:250px;" name="'.$value['id'].'" id="'. $value['id'].'">';
     
    Goramba, Jul 14, 2009 IP
  6. DK42

    DK42 Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks Goramba! I got it to work now. What was confusing me was that it wasnt displaying the selected variable in the list when you come back to the page (if that makes sense!)

    I managed to alter some of the other settings as well and its working wonderfully now! Believe me this has been hard work for me so thank you very very much! You have helped a lot! Have a great day!
     
    DK42, Jul 14, 2009 IP