Welcome Guest, Not a member yet? Register   Sign In
Passing values to already loaded view
#1

[eluser]behnampmdg3[/eluser]
Hello friends;

I am very new to CI. I am sure there is a "right way" to do this that I don't know of:

I have multiple functions in my controller and each of them send different values to view.

I was wondering how I can send values from different functions of a controller to a view which already has been loaded "WITHOUT RELOADING IT". In other words, I do not want to load a view multiple times, I just want to load it once, and further send values to it from different functions.

Thanks

For example:

Code:
public function __construct()
     {
  //Load header
        $page_info['title'] = "Members";
  $this->load->view('header_view', $page_info);
  }

public function show_members()
     {
  //Load view and send member's info
  $this->load->model('members_model');
  $data = $this->members_model->search();
  $m['members'] = $data;
  $this->load->view('members_view', $m)
}
public function show_bookings()
{
  //Load view and send bookins's info
  $this->load->model('bookings_model');
  $data = $this->bookings_model->search();
  $b['members'] = $data;
  $this->load->view('members_view', $b) //But as you see here, the view has already been loaded. I dont want to re-load it, I just want to send data to it.
}
#2

[eluser]xeroblast[/eluser]
use AJAX..
http://www.w3schools.com/ajax/default.asp
#3

[eluser]solid9[/eluser]
Not tested but try.

Code:
public function __construct()
{
//Load header
    $page_info['title'] = "Members";
$this->load->view('header_view', $page_info);
$this->load->model('members_model');
$this->load->model('bookings_model');
}


pubilc function show_members_bookings()
{
  $this->data['members'] = $this->members_model->search();
  $this->data['bookings'] = $this->bookings_model->search();

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

#4

[eluser]behnampmdg3[/eluser]
Maybe I should have different views for each part of the controller?
Or maybe different models as advised above?

#5

[eluser]solid9[/eluser]
Since you are new to CI.
Try to use one model only, to familiarize yourself.
Once you have confident that you are very familiar how MVC works.
Then after that you can learn MY_Controller and then MY_Model.
#6

[eluser]behnampmdg3[/eluser]
[quote author="solid9" date="1351683805"]Not tested but try.

Code:
public function __construct()
{
//Load header
    $page_info['title'] = "Members";
$this->load->view('header_view', $page_info);
$this->load->model('members_model');
$this->load->model('bookings_model');
}


pubilc function show_members_bookings()
{
  $this->data['members'] = $this->members_model->search();
  $this->data['bookings'] = $this->bookings_model->search();

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

[/quote]

Thank for this but what happens when I want to show only members and not bookings (in other words when I am not calling show_members_bookings)?

I hope you see my point here. A page may show different sets of records from database. For example I show a list of members in a box, a list of bookings somewhere else, a a list of hotels in another div,....but I don't need to load the view each time cause I end up with lots of redundant html.

Thanks

#7

[eluser]Aken[/eluser]
I'd suggest not loading views in your models. Load all of your views in ONE place so you know exactly what is being loaded at what time. Then use your models to send the appropriate data.
#8

[eluser]behnampmdg3[/eluser]
Hello ;

Thank you for your posts. I got what I wanted. Hopefully it is efficient and the correct way of approaching mvc.
Code:
class Members extends CI_Controller {
public $member_id;

public function __construct()
  {
   parent::__construct();
   $data['title'] = "Members Page";
   $this->load->vars($data);
   $this->members();
   $this->places();
   $this->view();
  }
public function index()
  {
  
  }

public function members()
  {
   $this->load->model('members_model');
   $data['members'] = $this->members_model->get_all_members();
   $this->load->vars($data);
  }
  
public function places()
  {
   $this->load->model('places_model');
    $data['places'] = $this->places_model->get_all_places();
    $this->load->vars($data);
    //$this->output->enable_profiler(TRUE);
   }
  public function view()
   {
    $this->load->view('header_view');
    $this->load->view('members_view');
    $this->load->view('footer_view');
   }




Theme © iAndrew 2016 - Forum software by © MyBB