Problem with function design

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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Problem with function design

Post by SBro »

I have the following function

Code: Select all

function loop_dir($dirname, $recursive = FALSE) {
	global $loop_dir;
	$handle = opendir($dirname);
	if ($handle) {
		while (false != ($file = readdir($handle))) {
			if ($file != '.' && $file != '..') {
				$file_ext = get_file_extension($file);
				if ($file_ext == $file) {
					$loop_dir[] = $dirname.$file;
					if ($recursive) {
						loop_dir($dirname.$file.'/');
					}
				}
			}
		}
		return $loop_dir;
	}
	else {
		error::log(ERR_NOTICE, 'Unable to open directory '. $dirname . '.', ERR_SHOW);
	}
}
This function resides in one file, I then use that function in another file by using something similar to:

Code: Select all

$array = loop_dir('directory/');
$loop_dir = array();

$array2 = loop_dir('directory2/');
$loop_dir = array();
This all works fine, however, I'm guessing my design/code is not the best, as I have to clear the array before running the function again. If I don't clear the array, then the next instance of the function simply adds to the previous. So my question is, how can I better code my function to allow it to be used multiple times, but without having to clear it each time? thanks.

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

don't use globals.

Return an array from the function, when you recurse, add the returned data to the current array that will be returned when that function ends. You have to be careful with recursion though, as you can quickly run out of memory because of all the jumps in and out of the function.

I prefer to write the function in a single go. This is done by getting the directory list of an initial directory, then iterating over the results looking into each directory contained within it. If I must, I'll post some code, but I'd like you to try making it (if you want to) first. ;)
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

As it stands at the moment, if I don't use globals, and leave the code exactly as is except comment out the global $loop_dir line, then I get the error "Undefined variable $loop_dir" when I run my page. I'm not sure I completely understand what you think I should do feyd. I sort of understand what it is you reccomend, but am not sure how to implement it. However I'm not looking for a free ride, so if you can explain it any better (as opposed to posting code) that would be appreciated thanks :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. to start, create a function that takes a directory as an argument. Upon calling, store the passed directory make it the initial element in an array. Using a for loop, you can now iterate over that array, doing a check if the current entry is a directory, if so, loop over it. Each entry in that directory found, add to the original array. The limit control of the for loop (second expression) must use an updatable flag of sorts.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Ok well unfortunately I haven't been able to successfully do it using your methodology Feyd. I did up the following code, trying take on board what you suggested:

Code: Select all

function new_loop($folder) {
	$new_array[] = $folder;
	
	$count = 1;
	// how should this work ??
        while ($count ??) {
		foreach ($new_array as $dir) {
			if (is_dir($dir)) {
				$dir_array[] = $dir;
				echo 'Dir: '.$dir.'<br>';
				$handle = opendir($dir);
				if ($handle) {
					while (false !== ($file = readdir($handle))) {
						$new_array[] = $file;
						$count++; // ?
					}
				}
			}
		}
	}
	return $dir_array;
}
I return $dir_array because I only want the directories, not individual files. I know i should use my $count variable somehow, but am unsure how to go about it? ie. what I should be comparing it to...I've tried using the size of the $new_array etc, but it seems just to create a never ending loop
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the while and foreach can be swapped out for a single for() if done correctly. readdir() only returns the filename, not a full path, so you need to add $dir to it.
Post Reply