CodeIgniter Forums
Send value to menu view - 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: Send value to menu view (/showthread.php?tid=72812)



Send value to menu view - pippuccio76 - 02-14-2019

Hi , sorry for english , in every controller i have :

Code:
       $this->load->view('templates/header_user',$data);
       $this->load->view('templates/menu_user',$data);
       $this->load->view('controller/view', $data);
       $this->load->view('templates/footer_user',$data);

Because menu_user are used in several controller  and i want send a value  to menu , how can i do this ?


RE: Send value to menu view - php_rocs - 02-14-2019

@pippuccio76,

All you have to do is set the $data value prior to sending it the the view...

PHP Code:
     $data['user_name'] = 'pippuccio76';

       
$this->load->view('templates/header_user',$data);
 
      $this->load->view('templates/menu_user',$data);
 
      $this->load->view('controller/view'$data);
 
      $this->load->view('templates/footer_user',$data); 

Within the view you would reference the variable as... $user_name 

See documentation... https://www.codeigniter.com/user_guide/general/views.html?highlight=view#adding-dynamic-data-to-the-view


RE: Send value to menu view - InsiteFX - 02-14-2019

You can send a variable to the view at any time using:

PHP Code:
$data['menu'] = 'Something';

// makes them global to all views
$this->load->vars($data); 



RE: Send value to menu view - pippuccio76 - 02-14-2019

(02-14-2019, 12:42 PM)InsiteFX Wrote: You can send a variable to the view at any time using:

PHP Code:
$data['menu'] = 'Something';

// makes them global to all views
$this->load->vars($data); 

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


RE: Send value to menu view - Wouter60 - 02-14-2019

Set it once, and store the value in a session variable.
E.g.
PHP Code:
$this->session->user_name 'pippuccio76'

In the menu view:
PHP Code:
<p>Your user name is: <?= $this->session->user_name;?>.</p> 



RE: Send value to menu view - pippuccio76 - 02-15-2019

(02-14-2019, 11:46 PM)Wouter60 Wrote: Set it once, and store the value in a session variable.
E.g.
PHP Code:
$this->session->user_name 'pippuccio76'

In the menu view:
PHP Code:
<p>Your user name is: <?= $this->session->user_name;?>.</p> 

Is It the best pratiche ?


RE: Send value to menu view - pippuccio76 - 02-15-2019

(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.
E.g.
PHP Code:
$this->session->user_name 'pippuccio76'

In the menu view:
PHP Code:
<p>Your user name is: <?= $this->session->user_name;?>.</p> 

Is It the best pratiche ?

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 ?


RE: Send value to menu view - Wouter60 - 02-15-2019

Quote: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...

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.


RE: Send value to menu view - kilishan - 02-15-2019

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.