• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send value to menu view

#1
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 ?
Reply

#2
@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/g...o-the-view
Reply

#3
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); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(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...
Reply

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

#6
(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 ?
Reply

#7
(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 ?
Reply

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.