Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something terribly wrong.. but I can't understand what

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Could someone have a look at this code and try to figure out what's wrong??
(the problem occurs when you've clicked a sub folder and clicks on the same sub_folder. For some reason the variable "pre" isn't passed on the currently "open" directory in the structure)

<?
$root = &quot;D:/musicfolder/&quot;;
$img = &quot;<img src='/images/folder.gif'>&quot;;
$dir = $_GET['dir'];
/*
dir looks like this dir[]=sub_folder1&dir[]=sub_folder2 etc.
*/
function sub_dir($path)
{
if(!file_exists($path))
return 0;
$arr = array();
if ($handle = opendir($path))
{
while (false !== ($found_dir = readdir($handle)))
{
if ($found_dir !== &quot;.&quot; && $found_dir !== &quot;..&quot; && is_dir($path.$found_dir))
{
$arr[count($arr)] = $found_dir;
}
}
}
return $arr;
}

function dir_structure($path,$count,$pre)
{
global $img;
global $dir;
for($i=0;$i<$count;$i++)
$add = $add.&quot;&nbsp;&nbsp;&quot;;
$a_dir = sub_dir($path);
for($i=0;$i<count($a_dir);$i++)
{
if($a_dir[$i] == $dir[$count])
{
echo $add.$img.&quot;<a href='?dir[]=&quot;.$a_dir[$i].&quot;'>&quot;.$a_dir[$i].&quot;</a><br>&quot;;
dir_structure($path.$a_dir[$i].&quot;/&quot;,($count+1),$pre.&quot;dir[]=&quot;.$a_dir[$i].&quot;&&quot;);
}
else
{
echo $add.$img.&quot;<a href='?&quot;.$pre.&quot;dir[]=&quot;.$a_dir[$i].&quot;'>&quot;.$a_dir[$i].&quot;</a><br>&quot;;
}
}
}
dir_structure($root,0,&quot;&quot;);
?>

My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Do this:

Code:
<?
$img = &quot;<img src='/images/folder.gif'>&quot;;
$dir = $_GET['dir'];
if (strlen($root) == 0) {$root = 'E:\\';}
/*
    dir looks like this dir[]=sub_folder1&dir[]=sub_folder2 etc.
*/
function sub_dir($path)
{
    if(!file_exists($path)) {return 0;}
    $arr = array();
    if ($handle = opendir($path))
    {
        while (false !== ($found_dir = readdir($handle)))
        {
            if ($found_dir !== &quot;.&quot; && $found_dir !== &quot;..&quot; && is_dir($path.$found_dir))
            {
                $arr[count($arr)] = $found_dir;
            }
        }
    }
    return $arr;
}

function dir_structure($path,$count,$pre)
{
    global $img;
    global $dir;
    for($i=0; $i<=$count; $i++) {$add .= &quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;;}
    $a_dir = sub_dir($path);
    for($i=0; $i<sizeof($a_dir); $i++)
    {
        if($a_dir[$i] == $dir[$count])
        {
            print($add . $img . &quot;<a href='?dir[]=&quot; . str_replace(&quot;/&quot;, &quot;\\&quot;, $a_dir[$i]) . &quot;'>&quot; . $a_dir[$i] . &quot;</a><br>&quot;);
            dir_structure($path.$a_dir[$i] . &quot;/&quot;, ($count+1), $pre . &quot;dir[]=&quot; . str_replace(&quot;/&quot;, &quot;\\&quot;, $a_dir[$i]) . &quot;&&quot;);
        }
        else {print($add . $img . &quot;<a href='?&quot; . $pre . &quot;dir[]=&quot; . str_replace(&quot;/&quot;, &quot;\\&quot;, $a_dir[$i]) . &quot;'>&quot; . $a_dir[$i] . &quot;</a><br>&quot;);}
    }
}
dir_structure($root,0,&quot;&quot;);
?>

Good luck :)

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top