• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load view into a variable?

#1
I want to load view into a variable but also want to pass some dynamic data to the view? 
please help

This code works:
$result["view"] = $this->load->view('template/test', '', TRUE);

This returns output directly to the browser:
$result["view"] = $this->load->view('template/test', array("something"=>"something"), TRUE);

https://codeigniter.com/user_guide/general/views.html
Reply

#2
Can you load your snippet of code?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#3
hi just create an an array called 'anything' like this
$page_data['myvariable']='something'; // the myvariable(or whatever you write) will become a variable once passed to the view, then in the second parameter pass it there like this
$result["view"] = $this->load->view('template/test', '$page_data'); // here you only pass the array name not the whole "$page_data['myvariable']" remember it dosen't have to be called myvariable it can be anything you like also page_data can be anything you like.
Reply

#4
Hi,

You are correctly using the 3rd term set to true in the load view function. This returns the view as a string for your variable.

PHP Code:
$result["view"] = $this->load->view('template/test'''TRUE); 

The second variable which you have left as an empty string is where you would pass the data to the partial view.

PHP Code:
$page_data = array('name' => 'mydata');
$result["view"] = $this->load->view('template/test'$page_dataTRUE); 

You can then access any of the array keys in your partial view as you would normally using, in the example above,

PHP Code:
My name is <?php echo $name?>

Hope that helps,

Paul.
Reply

#5
(03-05-2016, 03:25 AM)PaulD Wrote: Hi,

You are correctly using the 3rd term set to true in the load view function. This returns the view as a string for your variable.

PHP Code:
$result["view"] = $this->load->view('template/test'''TRUE); 

The second variable which you have left as an empty string is where you would pass the data to the partial view.

PHP Code:
$page_data = array('name' => 'mydata');
$result["view"] = $this->load->view('template/test'$page_dataTRUE); 

You can then access any of the array keys in your partial view as you would normally using, in the example above,

PHP Code:
My name is <?php echo $name?>

Hope that helps,

Paul.


Hi Paul,

can I perform jquery operation in test and set the value in view that value is not returned in $result["view"]

view Code

My Name is <?php echo $name ?>
<div id="address"></div>
<script>
$(function(){
    $('#address').html('this is my name print');
});
</script>

The $result["view"] will have contains the address div value. but I'm getting an empty value. 

Can you guide me on that?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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