Welcome Guest, Not a member yet? Register   Sign In
How cani pass data from one view to other
#1

[eluser]evilphp[/eluser]
hi i am new to mvc and currently learning it i want to send data from one page to other in .net i use response.redirect() or set the postback url of the button. but i don't know how to do the same in codeigniter nd i also want to join the data of two views and display it on other view.please guide me.
Thank you
#2

[eluser]Nalorin[/eluser]
I'm not sure what exactly you're trying to get at, exactly, but with MVC, all the data is prepared before the view is loaded.

The controller takes the input from the user (either via the URL, or via post data in a form), and performs the domain logic to load the proper views and insert the data into the views like so:

Code:
$data['favorite_movie'] = 'Ghostbusters';
$data['best_friends'] = array('Mickey','Minnie','Donald','Daffy');

$this->load->model('user_model');
$data['logged_in'] = $this->user_model->is_logged_in($user_info);

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

You can also load views from within a view. I use this method on my site, following the pattern on Jeffrey Way's tutorial on http://net.tutsplus.com.

Here's a brief summary of how to do that:

application/controllers/site.php:
Code:
class Site extends Controller
{
  function index()
  {
    $this->load->model('my_model1');

    $data['content'] = 'my_other_page';
    $data['my_model_data'] = $this->my_model1->get_data();
    $data['calendar_data'] = array (
      'date' => date('Y-m-d'),
      'todays_events' => array ('soccer @ 6:00','dance @ 9:00')
    );
    
    $this->load->view('includes/template',$data);
  }
}

application/views/includes/template.php
Code:
<?php
$data['my_model_data'] = $my_model_data;
$other_data['calendar_data'] = $calendar_data;
?>
<html>
<head><title>My CodeIgniter Page! :D</title>
  <link href='/css/my_style.css' />
</head>
<body>

  <div id='my_content'>
    &lt;?php $this->load->view($content,$data); ?&gt;
  </div>

  <div id='my_calendar'>
    &lt;?php $this->load->view('calendar',$calendar_data); ?&gt;
  </div>

  <div id='static_data'>
    &lt;?php $this->load->view('includes/static_data'); ?&gt;
  </div>

&lt;/body&gt;
&lt;/html&gt;

And then you can use your imagination on what to do with calendar.php, {$content}.php and includes/static_data.php, but they would be located in the views folder (with static_data.php being in the includes folder therein).

Just remember that passing data to a view is easiest (I don't know if it's possible otherwise) if you pass it as an associative array, because the array keys become the variable names inside the called view. So:
Code:
$data['my_key_name'] = 'my_value';
$this->load->view('view_name',$data);
in the constructor, or in a view, is available as:
Code:
&lt;?php echo $my_key_name; ?&gt;
in the view (if called from the constructor), or sub-view (if called from a view).
#3

[eluser]evilphp[/eluser]
Thanks i got it ........................Smile :-)




Theme © iAndrew 2016 - Forum software by © MyBB