Welcome Guest, Not a member yet? Register   Sign In
RECURSIVE FUNCTION NOT WORKING IN CI
#1

can someone help me make recursive function work in CI.Below is my code i tried but nothing is shown on the browser.When i try to use the code as helper it produces infinite loop.

public function printMenu($id=''){

$this->db->select('*');
$this->db->from('drop_menu');
$this->db->where('id', $id);
$query = $this->db->get();

foreach($query->result_array() as $row){

echo "<li><a href=\"#\">".$row['name'] ."</a>";

if(hasChildren($id)){
echo "<ul>";

//Call the function again with menu's id as the parameter
printMenu($id);

echo "</ul>";
}// end of if loop

echo "</li>";

}


}

public function hasChildren($menuId =''){

$query = $this->db->get_where('drop_menu',array('id'=>$menuId));

return $query->row_array();

if($this->db->num_rows() > 0){
return true;
}else{
return false;
}
}
Reply
#2

Is this an object? If so things like
PHP Code:
if(hasChildren($id)){ 
would need to be using $this, like
PHP Code:
if($this->hasChildren($id)){ 

and
PHP Code:
//Call the function again with menu's id as the parameter
printMenu($id); 
should be
PHP Code:
$this->printMenu($id); 

As far as getting it to work as a helper, did you load the CI instance into your helper and use it to access CI's internals, like the $this->db object?
Reply
#3

For starters, at this line:
Code:
printMenu($id);
You haven't changed the value of $id, yet, so the only way this function would ever do anything but loop infinitely would be for a menu with no children.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB