RECURSIVE FUNCTION NOT WORKING IN CI - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: RECURSIVE FUNCTION NOT WORKING IN CI (/showthread.php?tid=62232) |
RECURSIVE FUNCTION NOT WORKING IN CI - kwangu - 06-22-2015 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; } } RE: RECURSIVE FUNCTION NOT WORKING IN CI - CroNiX - 06-22-2015 Is this an object? If so things like PHP Code: if(hasChildren($id)){ PHP Code: if($this->hasChildren($id)){ and PHP Code: //Call the function again with menu's id as the parameter 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? RE: RECURSIVE FUNCTION NOT WORKING IN CI - mwhitney - 06-26-2015 For starters, at this line: Code: printMenu($id); |