SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Modified Preorder Tree Traversal question

    I have a question on this code.
    http://www.sitepoint.com/article/hie...ata-database/2

    how would you get only the parents to show? no children. and I know that all of them are a child of 'food' but if you count the very first category "food" as NULL and only get 'fruit' and 'meat' as parents how would you do that?

    making the category 'food' null is not a problem. I just reverse the array and pop it of the end.

    any ideas on how to do this?
    success is not by chance, it is by choice.

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not exactly sure what you mean by setting food to null and getting its parents. "Food" by definition doesn't have any parents, its the parent of all the other nodes.

    For example, to get the parents of "Cherry" you would run this ...

    SELECT * FROM tree WHERE lft < 4;

    To find the parents of "Banana" you would run this ...

    SELECT * FROM tree WHERE lft < 8 AND rgt > 9;

    As you can see from the numbering convention, the direct line of parents to the top of the tree will always have a lft value less than the value of the node you are looking at, and a rgt value greater than that node as well.

    Does this help?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me explain. I don't have it set a food being the main parent. I use this filed to be a "default" parameter so I can store things under it. I do not want to list this field on the site. so I just popup it off the end.

    now, if the code you start be doing a new tree which gets the "food" category. this sets the vlaues for lft and rgt. now, as you go through the code you get all that is under that. this is working grreat and doing exactly as it is told. but I do not want to list all of the children, I only want to list all of the parents. so in that link I want to list (show) fruit and meat. and stop after that.

    but here is where the problem comes in. how do I know if it has children and if it does not to show them?

    I do not want to start at the bottom and work up, I am at the top already and I do not want to show the bottom, unless it is a parent. does that make sense?

    to make it more clear, say I have these categories.
    Code:
    main
        cat1
        cat2
            sub1
            sub2
        cat3
        cat4
            sub3
            sub4
    I only want to show all the cat's, NO subs. with that code as it is it will show everything in order.
    success is not by chance, it is by choice.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    define "immediate children of X" as "descendants of X whose parent is X":

    SELECT * FROM tree WHERE
    (lft BETWEEN $X->lft AND $X->rgt)
    AND (parent = $X->title)

  5. #5
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to modify this function to show only parents
    PHP Code:
    <?php
    function display_tree($root) {
       
    // retrieve the left and right value of the $root node
       
    $result mysql_query('SELECT lft, rgt FROM tree '.
                              
    'WHERE title="'.$root.'";');
       
    $row mysql_fetch_array($result);

       
    // start with an empty $right stack
       
    $right = array();

       
    // now, retrieve all descendants of the $root node
       
    $result mysql_query('SELECT title, lft, rgt FROM tree '.
                              
    'WHERE lft BETWEEN '.$row['lft'].' AND '.
                              
    $row['rgt'].' ORDER BY lft ASC;');

       
    // display each row
       
    while ($row mysql_fetch_array($result)) {
           
    // only check stack if there is one
           
    if (count($right)>0) {
               
    // check if we should remove a node from the stack
               
    while ($right[count($right)-1]<$row['rgt']) {
                   
    array_pop($right);
               }
           }

           
    // display indented node title
           
    echo str_repeat('  ',count($right)).$row['title']."\n";

           
    // add this node to the stack
           
    $right[] = $row['rgt'];
       }
    }
    ?>
    the query has to stay the same as it will start with the 'food' category which is the main category. saying 'and parent = $x->title' will throw the query off
    success is not by chance, it is by choice.


Bookmarks

Posting Permissions

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