Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have written a function with while loop for getting recursive parents of category from DB in PHP, it fulfilled my purpose. But I want to know (actually curious to know) that how can I do this by recursive function? If yes then how? All it is doing is that it returns an array of parent categories by using a categoroy_id. Please let me know if something is unclear.

public function get_recursive_parents($category_id){
        $categories = array();
        $res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
        $cat_id = $res['parent_id'];
        $categories[] = $res;
        while($cat_id){
            $res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array();
            $categories[] = $res;
            $cat_id = $res['parent_id'];
        }
        return $categories;
    }
share|improve this question
1  
Can you clarify what exactly this is doing. While $cat_id is true, continue the loop? Wouldn't that be infinite? –  user1104854 Feb 13 '13 at 12:35
 
@user1104854 Nope, the loop will exit, when $res['parent_id'] is false, 0 or empty. –  Dainis Abols Feb 13 '13 at 12:37
1  
Oh, my fault. I didn't notice $cat_id was being re-set in the loop. I'll try to re-write it recursively, but honestly, there's really no point if this works. In my opinion, recursion has no benefits here. –  user1104854 Feb 13 '13 at 12:38
 
I know, it's just out of curiosity but you should know that a loop actually performs better than recursion and should be preferred if possible. –  fab Feb 13 '13 at 12:50
1  
Firstly, any loop structure can be written as a loop or recursively. Secondly, plain loops almost always outperform recursion in languages like PHP (functional languages on the other hand will be better with a recursive option). Finally, unless you have a specific performance issue with this function (which seems unlikely), you should write in the manner that is easiest to read rather than worrying about performance. Don't sacrifice maintainability for performance unless the speed difference is truly significant and makes a noticeable difference to the end user. –  SDC Feb 13 '13 at 13:32
show 1 more comment

3 Answers

up vote 0 down vote accepted

I can't tested the code, so I'm not sure if it's correct:

<?php

public function get_recursive_parents($category_id) {
    $categories = array();
    __get_recursive_parents($res['parent_id'], $categories);

    return $categories;
}

public function __get_recursive_parents($cat_id, &$output){
   $res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array();
   if ($res['parent_id']) {
       $output[] = $res;
       __get_recursive_parents($res['parent_id'], $output);
   }
   else {
       $output[] = $res;
   }
}
share|improve this answer
 
Yah I just wanted the logic and your logic seems correct, actually magic is happening at &$output, passing with reference, Thanks. –  Hafiz Feb 16 '13 at 11:36
add comment

To convert this to a recursive function it should work something like this (untested, but the logic should be similar).

public function get_recursive_parents($category_id){
        $categories = array();
        $res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
        $cat_id = $res['parent_id'];
        $categories[] = $res;
        while($cat_id) {
            $categories[] = get_recursive_parents($cat_id);
        }
        return $categories;
    }
share|improve this answer
 
Shouldn't the $categories array be a variable declared outside of the function? Won't it be re-set every time the function is called (via recursion)? So essentially only the last iteration would be in the $categories array –  user1104854 Feb 13 '13 at 12:44
 
No, the final result will be a multi-level array with all children of the first category. –  mcryan Feb 13 '13 at 12:56
add comment

Database queries in a recursive function? Your DBA might take you out the back and shoot you.

share|improve this answer
 
This will not be run many time, and I understand that at place where it is running , it will not be a big problem, as it is admin site.However, comments shouldn't be posted as answer of question –  Hafiz Feb 13 '13 at 14:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.