Recursive Functions - ugh
Posted: Tue Jul 29, 2008 12:59 pm
I have a love/hate relationship with recursive functions. I love it when they work, but I hate working out the logic of them. Unfortunately, this one has me stumped and I need some help.
I have a form that allows you to input a new page into my database. The fields are:
name, type, parent, deleted, order
"deleted" and "order" are used to show if a page has been deleted and its order, respectively. Allowed types are "regular", "unique", and "nodelete" (nodelete is only for the index page). If the page type is unique, it has no parent, and doesn't show up in any menus. If the page type is regular, it _must_ have a parent. You can use any page (including unique pages) as a parent. If the parent is a unique page or the index page, each page with that parent will show up on the parent's main menu as menu headings (i.e. Home, FAQ, Members). If the parent is a regular page, it will show up under the menu that the regular page is on. That's the theory anyways. I'm having trouble getting this to work properly.
My mysql query to pull all the pages just takes everything out sorted by order (excluding deleted pages). "Order" is a user input number, so it doesn't necessarily mean anything, and I can't rely on it for true order. Here's the rough pseudo code I've come up with to sort the pages:
I know that this won't work the way I want. It's sort of close though. Here's an example of what I'd like to have happen:
I've been wracking my head for the past two hours (and drawing diagrams, and writing and rewriting code) and still can't come up with anything functional. I'm starting to wonder if a recursive function is the best way to go about this. The reason I started with recursive is because each page could have potentially hundreds of child pages and sub-child pages. It's not likely, but it is possible. Maybe I can structure my query better to pull the pages out slightly more organized? Any ideas on how to fix this function or do this a better way?
Thanks!
I have a form that allows you to input a new page into my database. The fields are:
name, type, parent, deleted, order
"deleted" and "order" are used to show if a page has been deleted and its order, respectively. Allowed types are "regular", "unique", and "nodelete" (nodelete is only for the index page). If the page type is unique, it has no parent, and doesn't show up in any menus. If the page type is regular, it _must_ have a parent. You can use any page (including unique pages) as a parent. If the parent is a unique page or the index page, each page with that parent will show up on the parent's main menu as menu headings (i.e. Home, FAQ, Members). If the parent is a regular page, it will show up under the menu that the regular page is on. That's the theory anyways. I'm having trouble getting this to work properly.
My mysql query to pull all the pages just takes everything out sorted by order (excluding deleted pages). "Order" is a user input number, so it doesn't necessarily mean anything, and I can't rely on it for true order. Here's the rough pseudo code I've come up with to sort the pages:
Code: Select all
function getChild($pages, $originalPages, $originalParents)
{
// $originalPages and $originalParents are the same length, and correspond 1 to 1 for each page's parent.
foreach($pages AS $p)
{
for($i=0; $i < $originalParents.length; $i++)
$sorted = append($originalPages[i]); //append to the $sorted array. Don't remember the actual code to append to arrays.
$finalSort = append(" -" . $p); // children of child pages should appear as " - - -child 3"
if(!empty($sorted))
$finalSort = append(getChilds($sorted, $Opgs, $Oprs));
}
if(empty($sorted));
return null;
else
return $finalSort;
}Code: Select all
$pages = array("index", "parent1", "parent2", "child1", "child2", "child3", "child4", "child5", "child6");
$originalPages = array("index", "parent1", "parent2", "child1", "child2", "child3", "child4", "child5", "child6");
$originalParents = array("", "index", "index", "parent2", "parent1", "parent2", "child2", "parent1", "parent1");
//send through getChild($pages, $originalPages, $originalParent)
$desiredArray = array("index", "parent1", " -child2", " - -child4", " -child5", " -child6", "parent2", " -child1", " -child3");I've been wracking my head for the past two hours (and drawing diagrams, and writing and rewriting code) and still can't come up with anything functional. I'm starting to wonder if a recursive function is the best way to go about this. The reason I started with recursive is because each page could have potentially hundreds of child pages and sub-child pages. It's not likely, but it is possible. Maybe I can structure my query better to pull the pages out slightly more organized? Any ideas on how to fix this function or do this a better way?
Thanks!