Trying to make recursive function : no data return - 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: Trying to make recursive function : no data return (/showthread.php?tid=85126) |
Trying to make recursive function : no data return - sTis - 12-04-2022 Hello, I'm trying to make a function that get data recursively to get parents of an entity. And also parents of the parents. If an entity is at the root is parent is set to null. My database : Code: -- My controller (Edited 19/12) : PHP Code: function getAllEntities() { My model (Edited 19/12) : PHP Code: <?php $data is populated with the right data as you can see (var_dump in model) but, return $data doesn't work !! Code: C:\wamp64\www\qqov2\app\Models\TestsModel.php:18: Not any data return to my controller. Var_dump in my controller return NULL : Code: C:\wamp64\www\qqov2\app\Controllers\Tests.php:41:null I'm probably missing something but can see what it is !!!! Can someone help me please ? RE: Trying to make recursive function : no data return - bassmandc - 12-10-2022 If you're able to verify that $data is populated with the right data before it hits the return function, then I don't think it's the return function that isn't working. I think the problem lies with this line: $test = $testsMdl->getParentsRecursive($id, array()); I might be missing something, but I don't understand what "array()" is doing in that call. Try this: $test = $testsMdl->getParentsRecursive($id); Does it return your data? RE: Trying to make recursive function : no data return - sTis - 12-19-2022 Hi, Thank for your answer. Sorry for the long time before my return. I've edited my code (see in my first post). I've try to eliminate the array as you suggested. But, the array() is useful for compiling data before sending it. I removed the array parameter in the call to my model's function in the controller. As soon as I call the function getParentsRecursive itself the return doesn't work anymore. Every suggestion welcome, RE: Trying to make recursive function : no data return - bassmandc - 12-19-2022 Go ahead and restore the "array()" definition in your call then. Here's my next suggestion: In your Controller, change the line: Code: $testsMdl = model('testsModel'); to either: Code: $testsMdl = model(testsModel::class); or Code: $testsMdl = new model('testsModel'); RE: Trying to make recursive function : no data return - badger - 12-19-2022 Don't know how many records you have in your database but in a similar situation, rather than hitting the db with umpteen queries, i extract all the entries and then do a recursive loop in php Bill RE: Trying to make recursive function : no data return - InsiteFX - 12-20-2022 This explains on how to write recursive php methods. PHP Recursive Functions: How to Write Them, and Why They’re Useful RE: Trying to make recursive function : no data return - sTis - 12-20-2022 Thanks all for your answers, @bassmanc : I've tried yours propositions. It's not the solution. In fact my model call works : I've got results in my recursive function in app/models/testsModel.php. @badger : I used such a solution to make it work. But I really want to understand why my results can't go out of my model !!!! @InsideFX : thanks for this article. Any idea on what my result can't be return ? RE: Trying to make recursive function : no data return - InsiteFX - 12-21-2022 If return $data; not working then there is something wrong with your if statement. Try retruning the data ant the end and see if it returns without the if statement. RE: Trying to make recursive function : no data return - sTis - 12-25-2022 What's stange is that $data in else statement are right (see below). In my example, I have 3 entities to return. $data is correctly populated. everything seems to work fine except the return function. PHP Code: $data in Else loop PHP Code: $data dump outside of if loop In my example, I have three passages in the recursive function. And you can see above that $data is correctly fed at each pass. But getParentsRecursive() still return nothing. RE: Trying to make recursive function : no data return - InsiteFX - 12-26-2022 Please see this post that I answered here for a Category Menu. Forums - Convert array structure for ul() codeigniter helper |