Jump to content

Rafriend

New Members
  • Posts

    6
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Rafriend's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. thanks thorpe! This little project has been driving me crazy, As it's my first attempt at writing a recursion based script I find my brain going to mush when I start thinking through the processes only to get lost on a bermuda triangle of thought... I'll take a close look at your script and hope that it can shine some light on the matter. If I get anywhere I'll post it. Robert
  2. if anyone's interested in trying it with the above diagram's data, here's an array declaration for it: [code]$inputLines = array('Food', '  Fruit', '    Red', '      Cherry', '    Yellow', '      Banana', '  Meat', '    Beef', '    Pork'); [/code]
  3. well, after another long day of trying I still haven't cracked this I've got as far as getting Children processed properly but can't figure out what I have to do for getting Siblings into the equation: Also, I realise that the code on the site point forum will only work with 1 parent top node, So Ive changed my array to contain 1 top level element that I call in order to start the process... So far: //haven't included the hasKids() function below - this just compares 2 lines in the array and returns true or false if the following line is indented more than the first (therefore "hasKids") [code]    function rebuild_tree($index, $left) {                  global $inputLines; global $tree_Array;                  $right = $left+1;          while ($inputLines[$index]) {                  if(hasKids($index)) {         $right = rebuild_tree($index+1, $right);         }                  // write the data         $tree_Array[] = "$inputLines[$index] lft:$left rgt:$right";                  // return the right value of this node + 1         return $right+1;                  $index++;     }     }          $inputLines = array('All', '  Games', '  Health', '    Alternative Medicine', '    Beauty & Style', '    Dental', '      Oral Hygene', '  Home', 'Other');         rebuild_tree(0, 1);     print_r($tree_Array);[/code] If anyone has any pointers I would love to hear them... so close but yet so far!... [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] btw, this is a diagram of the the result I want to achieve: [img src=\"http://i2.sitepoint.com/graphics/sitepoint_numbering.gif\" border=\"0\" alt=\"IPB Image\" /]
  4. Thanks Ober, wasn't sure where to post it initially
  5. Hi Ober! Thanks for the fast response! Sorry if I haven't been clear, I'm trying to achieve a Modified Preorder Tree Traversal output. The best explanation I've found of it is here: [a href=\"http://www.sitepoint.com/article/hierarchical-data-database/2\" target=\"_blank\"]http://www.sitepoint.com/article/hierarchi...data-database/2[/a] The Lft/Rgt will contain the data that will allow me to achieve the 'Nested Sets' model (see above link)
  6. Hi all! I hope someone out there can help! [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] I've been trying all day to write some code that will take an array of categories and 'echo' out a representation of a nested sets table... I haven't managed it... my array is as follows (changed spaces for undersores so forum will display): [code]$array = array('Food, 'Fun Stuff', 'Health' , '__Dental', '____Oral Hygene', 'Other');[/code] so this gives me: [0] => Food [1] => Fun Stuff [2] => Health [3] => __Dental [4] => ____Oral Hygene [5] => Other I want to be able to write the output as: | Category Name | Lft | Rgt | xxxxxxxx xx xx ......(yes these should be inline) I've been referring to [a href=\"http://www.sitepoint.com/article/hierarchical-data-database/3\" target=\"_blank\"]this code[/a] from an article on sitepoint. [code]<?php function rebuild_tree($parent, $left) {    // the right value of this node is the left value + 1    $right = $left+1;    // get all children of this node    $result = mysql_query('SELECT title FROM tree '.                           'WHERE parent="'.$parent.'";');    while ($row = mysql_fetch_array($result)) {        // recursive execution of this function for each        // child of this node        // $right is the current right value, which is        // incremented by the rebuild_tree function        $right = rebuild_tree($row['title'], $right);    }    // we've got the left value, and now that we've processed    // the children of this node we also know the right value    mysql_query('UPDATE tree SET lft='.$left.', rgt='.                 $right.' WHERE title="'.$parent.'";');    // return the right value of this node + 1    return $right+1; } ?>[/code] This achieves these results with a database (adjacency list) as the input but I can't seem to get my head around how I would use an array like mine as an input instead. I've already written a small function that checks whether the 'next' element in the array has more spaces than the current so therefore 'hasChild' but that's as far as i've got... All attempts at writing the recursion has lead to bloated code and frustrating dead ends... Any info, advice, questions or discussion would be appreciated! Thanks Robert
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.