Hello --

I am having a trouble with my code, which reads a document directory recursively. My idea is that I call a function rmenu() from mainfunction.php file and get an array of directory information and parse it into a table.

I wrote a function, rmenu()

function rmenu($dir, $j) {
	global $docRoot, $urlDir, $articles, $docPath;
	$content="";
	$menuContent="";
	if ($handle=@opendir($dir)) {
		while (($file = readdir($handle)) !== false) {
			if (is_dir($dir."/".$file)) {
				if ($file != "." && $file != "..") {
				$dA[]=$file;
				}
		    }
		    else {
				$fA[]=$file;
		    }
	    }
		closedir($handle);
    }
    for ($i=0;$i<sizeof($fA);$i++) {
	if (is_file($dir."/".$fA[$i])) {
			// debugging... echo "Fileanme : {$fA[$i]} === under Dir: $dir <br>";
		}
	}
	$j++;
    for ($i=0;$i<sizeof($dA);$i++) {
    	rmenu("$dir"."/"."$dA[$i]", $j);
    }

return $fA;

}

While this function reads directories and files ok, return values keep changing (first it returns files in parent directory; and returns files in its subdirectories; then their subdirectories; and so on...).

As a result of this, I only get the last array of $fA.

I want to gather them into one array and return it to a php file; and render the directory within a table. "Gather them into one array" is my problem....

Oh, if I make echo statements here and there, I get all the information. But, this would not help me since I need to parse the information into a table in a php file (index.php). That is, the echo statements will show before the page design...

Any help would be appreciated...

Thanks.

    The reason you are missing parts of the return is that you are discarding it.

    When you call you rmenu function, you need to have some variable to store it's result in.

    like

    $res = rmenu("$dir"."/"."$dA[$i]", $j);
    

    I didn't go too in depth with your code to figure out how it works and what you are doing, but that's the reason you are losing stuff.

    If you want it to return the whole array of files, you'll need to append or do something in order to build your array before you return it with the return $fA line.

    I hope this makes sense or helps or something...

    Dave 🙂

      Exactly! That was my problem... which I don't know how to do it.

      I call the rmenu function as you suggested here.

      $res = rmenu("$dir"."/"."$dA[$i]", $j);
      

      The function takes this alright. But, only returns the last return values. Apparently, the output of the rmenu function discards the previous return values as it calls itself (recursively) several times.

      And as you suggested, I WANT to collect all the returns into an array; and have the function spit it back to me. But, I don't know how!?!?

      Even if I put at the end of the script something like:

      $allArray[]=$fA;
      

      It wouldn't hold the previous return values, since the function starts itself again (by recalling itself). Therefore, the result only would look like

      $res[][$i] -> file values. 
      

      That is, it has two-dimensional array with the first [] empty; and it has only the last value that was sent by the rmenu function. Using $allArray.=$fA also result in the same...

      Maybe, I will just use echo with table format in this rmenu function; then call it from an appropriate place (between <td> rmenu($dir, $j)</td> place) in the index.php...

      BUT, definitely I would like to know how to hold the values of recursive function result...

      And I really thank you, David. You helped me think clearly!

        Write a Reply...