Welcome Guest, Not a member yet? Register   Sign In
Get select for all controllers
#1

(This post was last modified: 10-10-2021, 09:37 PM by Secux.)

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
Reply
#2

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

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

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#5

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB