Forum Moderators: coopster
The reason I am not doing it like it is done in the article is because I want the ability to open and close directories instead of having the whole tree displayed, so I am running through the whole directory tree, letting PHP decide whether or not the branch should be displayed based on the query string, inefficient I know, when I run this code it locks up my computer and my CPU is at 100%, here it is:
<?
mysql_connect("", "", "") or die (mysql_error());
mysql_select_db("");
if($_GET['dir'] == NULL){
//if there is no path selected, set it to NULL
$path[] = "";
} else {
//remove empty dirs and sanitise
$pathArray = explode("/", $_GET['dir']);
foreach($pathArray as $pathElement){
if(trim($pathElement) == ""){
//do nothing
} else {
$path[] = mysql_real_escape_string($pathElement);
if($path[0]!= ""){
array_unshift($path, "");
}
}
}
}
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query("SELECT `name` FROM `dirs` WHERE `parent` = '" . $parent . "' ");
// display each child
while ($row = mysql_fetch_array($result)) {
if($row['parent'] == ""){
//print it because it is a root folder
print $row['name'] . "<br/>";
//let's display it's children
display_children($row['id'], $level+1);
} else {
//this is not a root folder, check whether it should be displayed
if($row['name'] == $path[$level]){
print str_repeat(' ',$level) . $row['name'] . "<br/>";
//let's display it's children
display_children($row['id'], $level+1);
}
}
}
}
display_children("",0);
?>
If any further explanation is required just shout, any help or guidance you guys could give me would be greatly appreciated!
Regards,
-Tom Griffin