SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link Directory using MySQL

    I am creating a link directory using PHP and MySQL, I have followed the Sitepoint Article 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:

    PHP Code:
    <?

    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);

    ?>
    I have tested the first block of code that handles the query string, that works fine if you are wondering.

    If any further explanation is required just shout, any help or guidance you guys could give me would be greatly appreciated!

    Regards,
    -Tom Griffin

  2. #2
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •