Search code examples
phpfunctioncodeignitercountcodeigniter-4

Recursive Function Not Working in CodeIgniter 4


I'm trying to list categories with the recursive function in CI 4, but I couldn't. The code is below and the error message is below 👇

public function categoryList($list, $parent = 0)
{

       $model = new BirimlerModel();
  $list = $model->findAll();
      
     echo '<ul>';
     foreach ($list as $category){
         if ($category['birim_ust'] == $parent){
             echo'<li>' . $category['birim_adi'];
                 echo categoryList($list, $category['birim_id']);
             echo '</li>';
         }
     }
     echo '</ul>';
  echo categoryList($list);
}

Error message: 👇

ArgumentCountError Too few arguments to function App\Controllers\Yonetim\Birimler::categoryList(), 0 passed in C:\xampp\htdocs\dentisthys\system\CodeIgniter.php on line 918 and at least 1 expected


Solution

  • The following line is the faulty line of your code. You are not passing the second argument here at the last line of your code.

     echo categoryList($list);
    

    From the above line of your code

    echo categoryList($list, $category['birim_id']);
    

    I understand there should be another argument.