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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts

    recursive funciton for categories and sub categories?

    Hello.
    I have some code which i created that will get a list of all the main categories and then all the sub categories.
    The data is sorted in an array and the sub categories are stored in an array in the original array.
    let me show you

    PHP Code:
    function getparent($parent)
    {
        
    $tmp = Array();
        
    $getparent "SELECT * FROM static_pages WHERE page_parent = $parent";
        
    $parentquery mysql_query($getparent) or die(mysql_error());
                    
        while(
    $pageresult mysql_fetch_array($parentquery))
        {                        
            
    array_push($tmp$pageresult["page_id"]);
                        
            
    $tmp2 getparent($pageresult["page_id"]);
            if(
    count($tmp2) > 0)
            {
             
    array_push($tmp$tmp2);
            }
        }                    
        return 
    $tmp;
    }            

    print 
    "<pre>";
    print_r(getparent(0));
    print 
    "</pre>"
    any it produces a array like this
    Code:
    Array
    (
        [0] => 1
        [1] => 2
        [2] => Array
            (
                [0] => 3
                [1] => Array
                    (
                        [0] => 4
                    )
    
            )
    
        [3] => 5
        [4] => 6
        [5] => Array
            (
                [0] => 7
                [1] => Array
                    (
                        [0] => 8
                        [1] => Array
                            (
                                [0] => 9
                            )
    
                    )
    
                [2] => 10
            )
    
    )
    Basically i can't see an easy way to navigate to page_id 9. I could go to key-5 and check to see if it an array. if it is then step into it and carry on until i find 9.

    Does anyone know and easier way to find the sub pages (there could be any number of sub sub pages. and also do you know an easier way to store the data.

    Cheers

    MRMAN

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://www.sitepoint.com/article/hie...ata-database/2

    may be a useful starting point for you.
    my mind is on a permanent tangent

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much


  •  

    Posting Permissions

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