Problem with array and function...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Problem with array and function...

Post 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>";
?>
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

while (($dirItem = readdir($dh)) !== false) {


What kind of construction is that??? :oops:
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post 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>";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your function doesn't return anything.
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post 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
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post 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>";
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Sorry, like I said. I don't understand your while-loop. Can't help you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Post by Mo »

Still having trouble...

What do you mean by capturing it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

capturing:

Code: Select all

$dirlist = Test('images/');
Post Reply