1

The logic required in this case might be simple or not, but I couldn't figure it out. So, I am asking for help.

After a query, I get an array that looks a bit like this:

Array=>
     [0]=>
         ['name'] = item1
         ['id'] = 1
         ['parent_id'] = 0
     [1]=>
         ['name'] = item2
         ['id'] = 2
         ['parent_id'] = 1
     [2]=>
         ['name'] = item3
         ['id'] = 3
         ['parent_id'] = 5

Now, I need to make paths for each and every item in this list. The paths would look something like: /item1 for item1 and something like /item1/item2 for item 2.

Note: The items are not necessarily in order. A parent item might come after it's child...

So, basically, I need a loop (probably more than 1), that when it encounters an item, it writes down the item name preceded by a slash. Then, it looks at the parent_id and writes down the parent_id's name preceded by a slash.

Then it looks at the parent's parent_id and writes down that name preceded by a slash. It continues to do this until it encounters a parent_id of 0. At which point, it assigns a value to an array, so something like paths['item2'] = "/item1/item2" and moves on to the next id and repeats!

Thanks for all your help, have a good day!

edit: Fixed the id of item3, all the items are meant to have different id's. I was asked to improve the question: The final output array should look a bit like this:

Array=>
     ["item1"]="/item1"
     ["item2"]="/item1/item2"
     ["item3"]="/item5/item3"    

The final output would be a html select form with each item as an option, and I need to have it's path associated somehow, either with a hidden field or just through Ajax or something.

edit: I fixed the problem. I just thought I'd write out the solution here in case someone else stumbles across this. Note: Still, not exactly sure how it works, but it works! It might be inefficient, I don't know.

function getCollPath($proj_list, $length){
$total_path = "";
$paths = array();
for ($j = 0; $j < $length + 1; $j++){
    if (isset($proj_list[$j])){
        $id = $j;
        $name = $proj_list[$j]['name'];
        $total_path = getItemPath($proj_list, $id, NULL);
        $paths[$name] = $total_path;
    }
}
return $paths;
}     

function getItemPath($proj_list, $current_id, $path){

$current_parent_id = $proj_list[$current_id]['parent_id'];
$current_name = $proj_list[$current_id]['name'];
$current_path = "/".$current_name;
if ($current_parent_id == 0){
      if (isset($path)){
        return $current_path.$path;
      }
      else{
        return $current_path;
      }
}
else{
    if (!isset($path)){
        $path = $current_path;
    }
    return getItemPath($proj_list, $current_parent_id, $path);
}
}
5
  • 2
    Why do item2 and item3 share the same id? Commented Dec 15, 2011 at 22:15
  • what would the final output be? Commented Dec 15, 2011 at 22:23
  • @webbiedave oops that's just a typo. All the id's are meant to be unique. Commented Dec 16, 2011 at 3:24
  • @Jon The final output as in the array? That would look like paths=>["item1"]=>"/item1",["item2"]=>"/item1/item2",["item3"]=>"/item5/item3"` and so on. Or, are you talking about the actual display? In which case, I need to make a html select form with each item as an option, and I need to have it's path associated somehow, either with a hidden field or just through Ajax or something. Commented Dec 16, 2011 at 3:29
  • @zermy Okay, please post that in the question to improve it. Commented Dec 16, 2011 at 3:56

1 Answer 1

1

A recursive function. It looks out for array element with id = child.parent_id. Then it calls self with the current parent_id as parameter until a element with parent_id = "" or "0" is reached. It should return a segment of the breadcrumb to the parent call, so the original call gets the whole route

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.