Welcome Guest, Not a member yet? Register   Sign In
Pass data to nested view many time within one variable ?
#1

Best solution to pass data to nested view ? 
I research some guide about this, almost they do like this :

Class A extends CI_Controller
{
   $this->data['title'] = "WEBSITE";
   $view['view_1'] = $this->load->view('view_1', '', TRUE);
   $view['view_2'] = $this->load->view('view_2', '', TRUE);
   $this->data['view'] = $view;

   $this->load->view('main_view', $this->data);
}

In main_view :
<?=view['view_1']?>
<?=view['view_2']?>

In view_1
<?=$title?>

It will get error :
Message:  Undefined variable: title
 Because view_1 is not passed data. So, i edit like this:
$view['view_1'] = $this->load->view('view_1', $this->data, TRUE);

My question :
- First : if i pass "$this->data" two times in two views like this => make my website slower ?
- Second : have any solution for this case ($this->data have a lot of data may be to use in all views)
Reply
#2

PHP Code:
$this->load->vars($data);
$this->load->view('view_name'); 

Make all variables global to all views.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks for your reply ! 
My code : 
Code:
$this->data['title'] = "WEBSITE";
$view['view_1'] = $this->load->view('view_1', '', TRUE);
$view['view_2'] = $this->load->view('view_2', '', TRUE);
$this->data['view'] = $view;
$this->load->vars($this->data);
$this->load->view('main_view');

But, i still get error when i load main_view : Undefined variable: title
Reply
#4

(06-15-2016, 08:53 PM)ryantranvn Wrote: Thanks for your reply ! 
My code : 
Code:
$this->data['title'] = "WEBSITE";
$view['view_1'] = $this->load->view('view_1', '', TRUE);
$view['view_2'] = $this->load->view('view_2', '', TRUE);
$this->data['view'] = $view;
$this->load->vars($this->data);
$this->load->view('main_view');

But, i still get error when i load main_view : Undefined variable: title

In main view:
PHP Code:
<?= $this->load->view('view1'''TRUE?>
<?= $this
->load->view('view2'''TRUE?>

Code to load main view:
PHP Code:
$this->data['title'] = "WEBSITE";
$this->load->vars($this->data);
$this->load->view('main_view'); 
Reply
#5

Many thanks pdthinh!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB