Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts

    display filename in a dir & auto fill it as value an array

    Hi All, nice to meet you

    i Looking for some help here,

    I have config.file where is as file configuration to another file, and inside are:
    PHP Code:
    <?php
    $fn
    [0]["short"] = "file-name.txt"$fn[0]["long"] = "file name";    
    $fn[1]["short"] = "1_file-name.txt"$fn[1]["long"] = "1 file name";    
    ?>
    What that I want to:
    1. $fn[0], $fn[1], etc.., as auto increasing
    2. "file-name.txt", "1_file-name.txt", etc.., as file name from a directory, i want it auto insert.
    3. "file name", "1 file name", etc.., is auto split from "file-name.txt", "1_file-name.txt", etc..,

    I was try to handle it with atomically insert using small php code, but i not sure it's done , because it just echoing and than how to split it?

    here the e.g. code:

    PHP Code:
    $path ".";
    $dh opendir($path);
    //$i=1;
    $i0;
    while ((
    $file readdir($dh)) !== false) {
    if(
    $file != "." && $file != "..") {
      echo 
    "\$fn[$i]['short'] = '$file'; $fn[$i]['long'] = '$file(splited)';<br />"// Test
        
    $i++;

    closedir($dh); 
    because when this code include to config.php file won't work properly
    so Every idea would be welcome and could be help me, Thank You
    Last edited by sir.jones; 01-10-2010 at 06:21 PM.
    Best Regards,

  • #2
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    PHP Code:
    <?php
    echo "\$fn[$i]['short'] = '$file'; \$fn[$i]['long'] = '$file(splited)';<br />";
    ?>

    I'm not too sure what you're trying to do, but I think you may want to escape the $ signs that you don't want to register as a variable. i.e., you want it to echo "$fn[1]['short] = 'file_name';..." and etc.

    To auto inc. an array, simply do the following:

    PHP Code:
    <?php
    $fn 
    = array();
    $fn[] = 'one.file'//$fn[0];
    $fn[] = 'one.file'//$fn[1];
    //and so on...
    ?>
    For the second question, I'm unsure what you are asking for but if you want a script that will search through a directory and return all files, then you could easily find a script like this on the internet.

    Your final question will require a basic string manipulation script (str_replace, or something similar).
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Hi Rebbu, you gave me very nice input...

    in fact i just need to auto dynamically populate a SelectBox element on an HTML webpage with the files found in a certain directory for option value. The values are supposed to be split by dash, underscore and number to provide the option name, e.g.

    Directory with Files > Value SelectBox Options
    filename1.txt > value: filename1.txt, text: Filename 1
    file_name2.txt > value: filename1.txt, text: File Name 2
    file-name3.txt > value: filename1.txt, text: File Name 3

    so i don't want to type file name from directory one by one (manual) anymore & avoid some mistyping (that's my goal) with config.php
    PHP Code:
    $fn[0]["short"] = "file-name.txt"$fn[0]["long"] = "file name";    
    $fn[1]["short"] = "1_file-name.txt"$fn[1]["long"] = "1 file name"
    Using this script:
    PHP Code:
    <?php
    echo "\$fn[$i]['short'] = '$file'; \$fn[$i]['long'] = '$file(splited)';<br />";
    ?>
    because i think it can handling to auto populate value of config.php, but i'm wrong because it's just echoing, that's why i make question here just to looking for another better way.

    Your final question will require a basic string manipulation script (str_replace, or something similar)
    this something stranger to me, can you explain it more please, that maybe can help me.. Many Thank you for your input.

    Regards
    Best Regards,


  •  

    Tags for this Thread

    Posting Permissions

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