CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Resolved Reverse Tree Structure - locating parent records (http://www.codingforums.com/showthread.php?t=286600)

rgEffects 01-28-2013 10:02 PM

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.

DanInMa 01-29-2013 02:24 AM

do you realize most people wont even read a post title whos your daddy? proper titles get your more help, jsut saying.

rgEffects 01-29-2013 06:17 AM

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.


All times are GMT +1. The time now is 10:19 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.