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
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);
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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