Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2013, 10:02 PM   PM User | #1
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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..
rgEffects is offline   Reply With Quote
Old 01-29-2013, 02:24 AM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
do you realize most people wont even read a post title whos your daddy? proper titles get your more help, jsut saying.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
rgEffects (01-29-2013)
Old 01-29-2013, 06:17 AM   PM User | #3
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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..
rgEffects is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:18 PM.


Advertisement
Log in to turn off these ads.