CodeIgniter Forums
How to pass variable value to another view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to pass variable value to another view? (/showthread.php?tid=37450)



How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]Unknown[/eluser]
Hi Everyone,

I m stuck somewhere passing values between different view.
Let me explain my problem.

I have a controller: category.php
Code:
<?php
class Category extends Controller{
  function Category(){
    parent::Controller();
  }
  function index(){
    $data['catname'] = 'Development';
    $data['page'] = 'home';
    $this->load->view('include/template',$data);
  }
}

In this controller I am passing two value first is category name and second is page name which I will use in my template.
Here is my template code include/template.php
Code:
<?php
$this->load->view('header');
$this->load->view($page);?>

In my header view I am changing the value of catname variable.
header.php
Code:
<?php
$catname = "New Development";
?>

home.php
Code:
<?php
echo $catname;// Printing old value...
?>

Now problem is that when I am trying to print catname using my second view its showing me old value not updated one. So can anyone please suggest me how can I accomplish this.
I have tried GLOBAL varibale too but unable to solve this problem.

I am new to php and codeigniter so please suggest me.

Many thanks in advance.


How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]Eric Barnes[/eluser]
If you are changing the variable in another then you would have to pass it to the sub view.
Code:
$this->load->view($page, $catname); ?>

Although more than likely this would best done in the controller.


How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]Unknown[/eluser]
Hi Eric,

Thanks for reply.

But I am using only one template for whole website and this requirement is only for 3-4 pages and every page have different-2 variable..

Is there any other way to get this.


How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]ranjudsokomora[/eluser]
Alok Rawat,
I would suggest setting a config value at runtime with the original 'catname', and when you load your second view call the config value.

It would look something like this.

controller: category.php
Code:
<?php
class Category extends Controller{
  function Category(){
    parent::Controller();
  }
  function index(){
    $data['catname'] = 'Development';
    $this->config->set_item('originalcatname', 'Development');
    $data['page'] = 'home';
    $this->load->view('include/template',$data);
  }
}

header.php
Code:
<?PHP
$catname = "New Development";

home.php
Code:
<?php
echo $this->config->item('originalcatname'); // Print Stored Value
?>
[/code]


How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]Atharva[/eluser]
Try
Code:
$this->load->vars($array)
The details can be found at here


How to pass variable value to another view? - El Forum - 01-10-2011

[eluser]daydreamer[/eluser]
I have same question a week ago and found the solution at http://ellislab.com/forums/viewthread/88335/#445375