Page 1 of 1

Problem with array and function...

Posted: Thu Aug 19, 2004 10:51 am
by Mo
Why cant I get this to work?

Code: Select all

<?php
function Test($myPath) {
	$myDirList = array();
	static $i=0;
	$dh = opendir($myPath);
	while (($dirItem = readdir($dh)) !== false) {
		if ($dirItem != "." && $dirItem != "..") {
			$myAbsolutePath = $myPath . $dirItem;
			if (is_dir($myAbsolutePath)) {
				$myDirList[$i]=$myAbsolutePath;
				//echo "myDirList[$i] = $myAbsolutePath<br>";
				$i++;
			}
		}
	}
	closedir($dh);
}
Test('images/');
echo "myDirList = $myDirList[0]<br>";
?>

Posted: Thu Aug 19, 2004 11:02 am
by Lord Sauron
while (($dirItem = readdir($dh)) !== false) {


What kind of construction is that??? :oops:

Posted: Thu Aug 19, 2004 11:17 am
by Mo
what are you saying? That is not my problem.

My problem is that I cannot get this to work:

echo "myDirList = $myDirList[0]<br>";

Posted: Thu Aug 19, 2004 11:22 am
by feyd
your function doesn't return anything.

Posted: Thu Aug 19, 2004 11:36 am
by Lord Sauron
true Feyd.

But I was just wondering what kind of while-construction that is. I've never seen something like it.

Antonie

Posted: Thu Aug 19, 2004 11:49 am
by Mo
feyd and Lord Sauron
The fact that it doesnt return anything is my problem. try this: remove the two slashes (//) from the following line and try it again.

//echo "myDirList[$i] = $myAbsolutePath<br>";

Posted: Thu Aug 19, 2004 11:50 am
by Lord Sauron
Sorry, like I said. I don't understand your while-loop. Can't help you.

Posted: Thu Aug 19, 2004 11:55 am
by feyd
I don't need to test run the function, it's not returning anything, either through the normal return, or through the variable you pass, or through a global.. so the data created/found inside that function never leaves it. i.e. $mydirList is only local to the function.. adding "return $mydirList;" to the end of the function, and capturing it with "$dirlist = Test('images/');" should make it work... provided there isn't a syntax error..

Posted: Thu Aug 19, 2004 12:19 pm
by Mo
Still having trouble...

What do you mean by capturing it?

Posted: Thu Aug 19, 2004 12:26 pm
by feyd
capturing:

Code: Select all

$dirlist = Test('images/');