CodeIgniter Forums
Get select for all controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Get select for all controllers (/thread-80269.html)



Get select for all controllers - Secux - 10-10-2021

where this select must be added to apply to all controllers
PHP Code:
$builder $db->table('users');
$builder->select('title, content, date');
$builder->from('mytable');
$data[‘user’] = $builder->get(); 

I am currently adding it to all functions and controllers.  I want to add it to only one file and it applies to all functions


RE: Get select for all controllers - includebeer - 10-12-2021

What do you mean by that? Do you want to execute this query in every controller? If so, you can add a function to your base controller, or to a custom library. There are several ways to accomplish this. But why not use a model instead of using the query builder like that?

Also, do you want to select from “users” or “mytable”? The way it is written, you have 2 “FROM”. I’m not sure if the second one override the first one, but either way something is wrong with that query.


RE: Get select for all controllers - Secux - 10-12-2021

I gave the code as an example only Smile that is, my code must be put in basecontrolles to work in all controllers?


RE: Get select for all controllers - includebeer - 10-13-2021

It’s still not clear what you try to do. What does it mean “I want to add it to only one file and it applies to all functions”? If you create a function in your base controller and your controllers extends the base controller, then the function will be available from all controller. But you will still need to call it. Or maybe you just want the result of that query available from all controller. Then execute that query in initController of the base controller and store the result in a public or protected variable.


RE: Get select for all controllers - kilishan - 10-13-2021

If you want that code to be executed for every page request, then adding it to the BaseController could work.

Depending on what you want to use it for, you might look into View Cells.