Send value to menu view |
Hi , sorry for english , in every controller i have :
Code: $this->load->view('templates/header_user',$data); Because menu_user are used in several controller and i want send a value to menu , how can i do this ?
@pippuccio76,
All you have to do is set the $data value prior to sending it the the view... PHP Code: $data['user_name'] = 'pippuccio76'; Within the view you would reference the variable as... $user_name See documentation... https://www.codeigniter.com/user_guide/g...o-the-view
You can send a variable to the view at any time using:
PHP Code: $data['menu'] = 'Something'; What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(02-14-2019, 12:42 PM)InsiteFX Wrote: You can send a variable to the view at any time using: in which place must i set this variable to make her global ? because menu are for many controller and i cannot set it in every controller...
(02-15-2019, 12:29 AM)pippuccio76 Wrote:(02-14-2019, 11:46 PM)Wouter60 Wrote: Set it once, and store the value in a session variable. in my menu i must change value dinamically , set a session on login is not ok . i must set variable directly on view by call a model function that return a value . Can i do this ? Quote:in which place must i set this variable to make her global ? Even global variables only exist until the next url request. So, if you switch from one controller to another, or even from one method to another, by calling a different url, all variables go back to their initial values. If you want to preserve variable values across url requests, use session. Another possibility is to use a library that gets the value from a model. Autoload the library. That makes all functions in it available for every controller and even views. Authentication libraries like Ion_auth work that way. Even in a view, I can call $this->ion_auth->is_logged_in() to check if the current user is logged in. There's no need to pass values from controller to view that way.
You could use a BaseController that all of those controllers extend from. Then you can have a method that grabs the value, or set default $data array in the constructor, etc.
|
Welcome Guest, Not a member yet? Register Sign In |