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
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Reverse Tree Structure - locating parent records

    I've got a table that contains file names and I've set up a query that generates an unordered list. The table has a record id (id) File Name and parentID. This all works perfectly.

    Now I need to generate a serial number that reflects the position of the child in the table structure. Think if it as a file path. If documents was ID 4 and proposal was ID 9 and my-proposal was ID 15 Instead of documents/proposal/my-proposal I would to generate a serial number for myProposal like this: my-proposal SN 4.9.15 This shows me the lineage in the file tree and locates the document perfectly.

    If my-proposal also had three children then they would show up in the directory structure like this:
    documents/proposal/myproposal/
    doc1(ID22)
    doc2(ID23)
    doc3(ID24)

    If you're following me so far I have this code but it is giving me the children of the target and not the parents. IOW, I'm getting 22.23.24.15 instead of 5.9.15
    PHP Code:
    <?php // find Parents

    $pgID $row_pageRS['id']; // current record ID
    $parent $pgID// assuming that pgID is the child
        
    get_parents($parent);
        function 
    get_parents($parent) {
        
            
    $sql "SELECT * FROM files WHERE parentID = ".$parent.""
            
    $run mysql_query($sql);
            
            while (
    $rec mysql_fetch_assoc($run)) { 
                echo 
    $rec['id'], '.'
                
    get_parents($rec['id']);
                }
                
    mysql_free_result($rec);
        }
    echo 
    $pgID;
    ?>
    Any ideas would be appreciated. If I change bcID = to bcID < I get an endless string of 1's


    ** EDITED 4:25 Pacific Time ****
    Just figured out part of the answer. The code above is working correctly. If you change the . in the HTML to <li /> and enclose the script in a <ul></ul> set you'll get a nice list of the files with indents. Changing the $parentID to 0 gives you the whole list so it makes sense that if I substitute the current page ID I'll get all of the children for that page.

    Need another approach. I've been looking at a breadcrumb type of list generator to create my list. There's a start here:
    http://911-need-code-help.blogspot.c...traversal.html

    Haven't been able to adapt it yet. If you have any other ideas please let me know.
    Last edited by rgEffects; 01-29-2013 at 07:13 AM.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    do you realize most people wont even read a post title whos your daddy? proper titles get your more help, jsut saying.

  • Users who have thanked DanInMa for this post:

    rgEffects (01-29-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    I got this solved. It took a bit of searching but I finally got it. Here's the successful code. I hope it benefits someone else. It works well for a breadcrumb trail back up a PHP tree structure.
    PHP Code:
    <?php // Display parent nodes or breadcrumb
    $childID $row_pageRS['id']; 
    $data = array();
    $index = array();
        
    $query mysql_query("SELECT id, parentID FROM files ORDER BY id");
        while (
    $row mysql_fetch_assoc($query)) {
            
    $id $row["id"];
            
    $bcID $row["parentID"] === NULL "NULL" $row["parentID"];
            
    $data[$id] = $row;
            
    $index[$bcID][] = $id;
        }

    function 
    display_parent_nodes($id) {
        global 
    $data;
        
    $current $data[$id];
        
    $bcID $current["parentID"] === NULL "NULL" $current["parentID"];
        
    $parents = array();
            while (isset(
    $data[$parentID])) {
                
    $current $data[$parentID];
                
    $bcID $current["parentID"] === NULL "NULL" $current["parentID"];
                
    $parents[] = $current["id"];
                }
            echo 
    implode("."array_reverse($parents));
        }
        
    display_parent_nodes($childID); 
        if (
    $row_pageRS['parentID'] == '0') {
        echo 
    $row_pageRS['id'];
        }
        else echo 
    '.' $childID;
    ?>
    I'm wondering if I should free the query by modifying these lines of code:
    PHP Code:
            $index[$parentID][] = $id;
        }
              
    mysql_free_result($query);
    function 
    display_parent_nodes($id) { 
    Things seem to be working just fine so far. I'm still not sure when I should be freeing a query result.
    Last edited by rgEffects; 01-29-2013 at 07:16 AM.


  •  

    Posting Permissions

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