Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Directory Question...

    Hi all,
    I am setting up a directory similar to Yahoo. I am having problems displaying the parent categories though.

    Also this forum uses the same concept:
    CodingForums.com > :: Server side development > PHP > Directory Question...

    I want to have it set up like:

    Parent > Sub 1 > Sub 2

    But I can only get it to show either:

    Parent > Sub 1

    Sub 1 > Sub 2

    Also a category's parent with the value of 0 means that their is no parent category.

    PHP Code:
    //Check to see if category has a parent category
    function hasParent($parent){
        if(
    $parent>0){
            
    //do{
                
    $q2=mysql_query("SELECT * FROM categories WHERE id=$parent")or die(mysql_error());
                
    $r2=mysql_fetch_array($q2);
                
    $newparent=$r2['parent'];
                
    $parentname=$r2['name'];        
                
    $newpath="$parentname > ";
                
    $path="$newpath$path";
            
    //}while($newparent>=0);
            
    return $path;
        }
        else{
            
    $path="";
            return 
    $path;
        }

    Any help would be awesome. Thanks in advance!
    Last edited by dizz; 03-23-2004 at 03:13 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    look like you have your WHILE in the wrong place. try this:

    Code:
    function hasParent($parent){
      $newparent=$parent;
      while($newparent>=0)
       {
        if($parent>0){ 
    
                $q2=mysql_query("SELECT * FROM categories WHERE id=$parent")or die(mysql_error()); 
                $r2=mysql_fetch_array($q2); 
                $newparent=$r2['parent']; 
                $parentname=$r2['name'];         
                $newpath="$parentname > "; 
                $path="$newpath$path"; 
                return $path; 
        } 
        else
       { 
            $path=""; 
            return $path; 
        } 
      }
    }
    not tested yet, but i think it should be ok....
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk


  •  

    Posting Permissions

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