Forum Moderators: coopster

Message Too Old, No Replies

Link Directory using MySQL

         

protiek

3:41 pm on May 8, 2005 (gmt 0)

10+ Year Member



I am creating a link directory using PHP and MySQL, I have followed the Sitepoint Article [sitepoint.com] and modified it for my needs, yet it seems it does not work. I am fairly new to PHP so forgive me for any blaring errors! The wya in which the directory will be requested is like this: script.php?dir=fdjnhh/fdhdfh/fdhdfh

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('&nbsp;&nbsp;',$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

protiek

9:01 pm on May 8, 2005 (gmt 0)

10+ Year Member



Anyone?