Jump to content

Recursive function problem.


ram4nd

Recommended Posts

I have this code:

function get_tutorial_categories($category_id, $seperator, $selected = false, $link = true) {
$CI = get_instance();
static $category, $up_category;
$category = $CI->categories_model->get_category($category_id);
$up_category = $CI->categories_model->get_category($category->parent_id);
if(empty($up_category->parent_id))
	return $category->name;
else
	return $category->name.$seperator.get_tutorial_categories($category->parent_id, $seperator, $selected, $link);
}

 

I need to do:

return get_tutorial_categories($category->parent_id, $seperator, $selected, $link).$category->name.$seperator;

instad of the last line, but $category is the value of the last round of this recursive function.

Link to comment
https://forums.phpfreaks.com/topic/195903-recursive-function-problem/
Share on other sites

Solved it

function get_tutorial_categories($category_id, $seperator) {
$CI = get_instance();
static $category, $up_category, $links_array;
//get category
$category = $CI->categories_model->get_category($category_id);
//get upper level category
$up_category = $CI->categories_model->get_category($category->parent_id);
//push
$links_array[] = $category->name;
//if upper category parent_id is 0, return category name
if(empty($up_category->parent_id)) {
	return array_shift($links_array);
}
//if upper category parent_id isn't 0, return get_main_category($category->parent_id)
else {
	return get_tutorial_categories($category->parent_id, $seperator, $selected, $link).' '.$seperator.' '.array_shift($links_array);
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.