PHP Newbie - Question

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default PHP Newbie - Question

    Hello,
    I am working with a script that a friend of mine wrote. The script
    traverses a directory and outputs the files and folders to a table.
    What I'd like to do is to have the folders expand and collapse with
    little plus/minus signs beside them. Can anyone point me in the right
    direction? An example of the output can be seen here (in the yellow
    box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]

    The script is as follows:

    <?php

    // Put this file in a directory. And then create a subdirectory in that
    directory called "Useful Links".
    // then put whatever you want into the Useful Links directory. Files,
    folders, whatever. Then access
    // this program in a web browser. It shoudl search the "Useful Links"
    directory and output what it finds.
    // I did this a sort of challenge to myself, so if you don't use it
    don't worry.
    // if you like the idea, I can change the bulleted list output to
    something more visually appealing (like tables).

    // this function is used to return all characters in a haystack that
    occur before the needle
    function strrrchr($haystack,$needle)
    {
    return substr($haystack,0,strpos($haystack,$needle));
    }

    // this function tries to find all occurences of the word "AND" and
    replace with with "&"
    function replace_and($str){
    return str_replace(array("AND", "And", "and"), "&", $str);
    }

    // this is a recursive function to traverse a directory structure and
    return a one-dimensional array
    function get_dir_array($dir) {
    STATIC $dir_array;
    $current_dir = "";
    if(!($dh = @opendir($dir))){
    echo "<!-- Directory not found $dir -->";
    return false;
    }

    while($fname = readdir($dh)) {
    if($fname!='.' && $fname!='..') {
    if(is_dir("$dir/$fname")) {
    get_dir_array("$dir/$fname");
    } else {
    if($current_dir == ""){
    $current_dir = $dir;
    }
    $dir_array[$current_dir][]=$current_dir."/".$fname;
    }
    }
    }

    closedir($dh);

    return $dir_array;
    }

    function cmp($a, $b) {
    $a = strrrchr(basename($a), ".");
    $b = strrrchr(basename($b), ".");

    return strnatcasecmp($a, $b);
    }


    // this function takes a one-dimensional array and converts it into a
    multi-dimensinal array using "/" as a delimmiter in the array key
    function convert_array($old_array){
    $new_array = array();
    if(is_array($old_array)){
    foreach($old_array AS $key=>$filename_array){
    if(is_array($filename_array)){
    usort($filename_array, "cmp");
    foreach($filename_array AS $filename){
    $explode_array = explode("/", $key);
    $temp_array = '$new_array';
    foreach($explode_array AS $new_key=>$new_val){
    $temp_array .= '[\''. $new_val .'\']';
    }
    $temp_array .= '[]=\''.$filename.'\';'."\n";
    eval($temp_array);
    }
    }
    }
    }

    return $new_array;
    }

    // this function takes a converted array and builds a formated
    bulleted list
    function display_links($links_array){
    STATIC $i = 0;
    $buffer = "";
    if(is_array($links_array)){

    $buffer .= "\n<table border=0 cellspacing=0 cellpadding=2>";
    foreach($links_array AS $title=>$link){

    if(!is_numeric($title) && $i > 0){
    $buffer .= "\n <tr>\n\t<td colspan=2
    NOWRAP>".replace_and($title)."</td>\n </tr>";
    }
    if(!is_array($link)){
    $buffer .= "\n <tr>\n\t<td NOWRAP>&nbsp;&nbsp;<a target='_blank'
    href='".str_replace(" ", "%20",
    $link)."'>".replace_and(strrrchr(basename($link), "."))."</a></td>\n
    </tr>";
    }else{
    $i++;
    $buffer .= "\n <tr>\n\t<td>\n<table border=0 cellspacing=0
    cellpadding=2>\n
    <tr>\n\t<td>&nbsp;</td>\n\t<td>".display_links($link)."</td>\n
    </tr>\n</table>\n\t</td>\n </tr>";
    }
    }
    $buffer .= "\n</table>";
    }
    $i++;
    return $buffer;
    }

    // directory containing the "Useful Links", assumes the location is
    relative to the current script.
    $start_directory = "Useful Links";

    // now get an unformatted list of links, and then convert it into a
    multi-dimensional array
    $useful_links_array = convert_array(get_dir_array($start_directory));

    // not take that list and change it into something to display on the
    screen
    $display_links = display_links($useful_links_array);

    ?>


    <?php
    // now "echo" that list of links to the screen
    echo $display_links;
    ?>


    --
    To reply, please remove your robe.

    Ryan Sullivan Guest

  2. Similar Questions and Discussions

    1. A newbie with a newbie question
      Good afternoon everyone, My Name is Dusty I am new to this forum and pretty new to Acrobat. I have Acrobat 9 running on an IMAC running 10.5.2 I...
    2. newbie question,,,
      I converted an AVI to FLV in the encoder. The resulsting file only opens a blank flash 8 player. I can't even get it to play within the flash app....
    3. Newbie Question: Biz Card Template Question
      Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I...
    4. Pen Tool Use Question. (Embarrassingly Newbie Question)
      I'm currently using Flash MX and whenever I choose the Pen Tool instead of the pen nib with the small "x" beside it that supposed to show up on...
    5. Newbie OO question
      In article <EbkRa.331382$fC.2436421@news.easynews.com>, "Ed W" <dodgynewsgroups@ewildgoose.demon.co.uk> wrote: perldoc perltoot it's all you...
  3. #2

    Default PHP Newbie - Question

    Hello,
    I am working with a script that a friend of mine wrote. The script
    traverses a directory and outputs the files and folders to a table.
    What I'd like to do is to have the folders expand and collapse with
    little plus/minus signs beside them. Can anyone point me in the right
    direction? An example of the output can be seen here (in the yellow
    box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]

    The script is as follows:

    <?php

    // Put this file in a directory. And then create a subdirectory in that
    directory called "Useful Links".
    // then put whatever you want into the Useful Links directory. Files,
    folders, whatever. Then access
    // this program in a web browser. It shoudl search the "Useful Links"
    directory and output what it finds.
    // I did this a sort of challenge to myself, so if you don't use it
    don't worry.
    // if you like the idea, I can change the bulleted list output to
    something more visually appealing (like tables).

    // this function is used to return all characters in a haystack that
    occur before the needle
    function strrrchr($haystack,$needle)
    {
    return substr($haystack,0,strpos($haystack,$needle));
    }

    // this function tries to find all occurences of the word "AND" and
    replace with with "&"
    function replace_and($str){
    return str_replace(array("AND", "And", "and"), "&", $str);
    }

    // this is a recursive function to traverse a directory structure and
    return a one-dimensional array
    function get_dir_array($dir) {
    STATIC $dir_array;
    $current_dir = "";
    if(!($dh = @opendir($dir))){
    echo "<!-- Directory not found $dir -->";
    return false;
    }

    while($fname = readdir($dh)) {
    if($fname!='.' && $fname!='..') {
    if(is_dir("$dir/$fname")) {
    get_dir_array("$dir/$fname");
    } else {
    if($current_dir == ""){
    $current_dir = $dir;
    }
    $dir_array[$current_dir][]=$current_dir."/".$fname;
    }
    }
    }

    closedir($dh);

    return $dir_array;
    }

    function cmp($a, $b) {
    $a = strrrchr(basename($a), ".");
    $b = strrrchr(basename($b), ".");

    return strnatcasecmp($a, $b);
    }


    // this function takes a one-dimensional array and converts it into a
    multi-dimensinal array using "/" as a delimmiter in the array key
    function convert_array($old_array){
    $new_array = array();
    if(is_array($old_array)){
    foreach($old_array AS $key=>$filename_array){
    if(is_array($filename_array)){
    usort($filename_array, "cmp");
    foreach($filename_array AS $filename){
    $explode_array = explode("/", $key);
    $temp_array = '$new_array';
    foreach($explode_array AS $new_key=>$new_val){
    $temp_array .= '[\''. $new_val .'\']';
    }
    $temp_array .= '[]=\''.$filename.'\';'."\n";
    eval($temp_array);
    }
    }
    }
    }

    return $new_array;
    }

    // this function takes a converted array and builds a formated
    bulleted list
    function display_links($links_array){
    STATIC $i = 0;
    $buffer = "";
    if(is_array($links_array)){

    $buffer .= "\n<table border=0 cellspacing=0 cellpadding=2>";
    foreach($links_array AS $title=>$link){

    if(!is_numeric($title) && $i > 0){
    $buffer .= "\n <tr>\n\t<td colspan=2
    NOWRAP>".replace_and($title)."</td>\n </tr>";
    }
    if(!is_array($link)){
    $buffer .= "\n <tr>\n\t<td NOWRAP>&nbsp;&nbsp;<a target='_blank'
    href='".str_replace(" ", "%20",
    $link)."'>".replace_and(strrrchr(basename($link), "."))."</a></td>\n
    </tr>";
    }else{
    $i++;
    $buffer .= "\n <tr>\n\t<td>\n<table border=0 cellspacing=0
    cellpadding=2>\n
    <tr>\n\t<td>&nbsp;</td>\n\t<td>".display_links($link)."</td>\n
    </tr>\n</table>\n\t</td>\n </tr>";
    }
    }
    $buffer .= "\n</table>";
    }
    $i++;
    return $buffer;
    }

    // directory containing the "Useful Links", assumes the location is
    relative to the current script.
    $start_directory = "Useful Links";

    // now get an unformatted list of links, and then convert it into a
    multi-dimensional array
    $useful_links_array = convert_array(get_dir_array($start_directory));

    // not take that list and change it into something to display on the
    screen
    $display_links = display_links($useful_links_array);

    ?>


    <?php
    // now "echo" that list of links to the screen
    echo $display_links;
    ?>


    --
    To reply, please remove your robe.

    Ryan Sullivan Guest

  4. #3

    Default Re: PHP Newbie - Question

    An noise sounding like Ryan Sullivan said:
    > Hello,
    > I am working with a script that a friend of mine wrote. The script
    > traverses a directory and outputs the files and folders to a table.
    > What I'd like to do is to have the folders expand and collapse with
    > little plus/minus signs beside them. Can anyone point me in the right
    > direction? An example of the output can be seen here (in the yellow
    > box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]
    >
    Eh, you'd probably want to look at generating the whole directory tree and
    then displaying it dynamically with javascript used to expand/contract
    sections of it.

    David
    David Gillen Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139