2 Codeigniter Controller Querys on One View
How do you put two different function outputs from the same controller into the same view(page)? For example, I have a function going to the "article" div, and another function that I am trying to use within the "aside" div. (using HTML 5 nomenclature here)
I have went as far as using the actual db query in the (view)aside div and it still only displays the 1st article function.
I have changed the $data('result') variable to separate variables and that makes no difference.
I am showing a todo list of jobs I have to do in the article div, then showing the titles of the completed todo's in the aside div.
I suppose I am making a mess of this explanation. Would you use a function from a different controller?
This is the first code in the controller
function index()
{
$this->load->model('work_m');
$data = array();
$config['base_url'] = base_url() . 'index.php/work/index/';
$config['total_rows'] = $this->db->count_all('work');
$config['per_page'] = '10';
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['result'] = $this->work_m->get_records($config['per_page'], $this->uri->
segment(3));
$tmpl = array('table_open' =>
'<table border="0" cellpadding="0" cellspacing="0">', 'heading_row_start' =>
'<tr class="heading">', 'heading_row_end' => '</tr>', 'heading_cell_start' =>
'<th>', 'heading_cell_end' => '</th>', 'row_start' => '<tr>', 'row_end' =>
'</tr>', 'cell_start' => '<td>', 'cell_end' => '</td>', 'row_alt_start' =>
'<tr class="alt">', 'row_alt_end' => '</tr>', 'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>', 'table_close' => '</table>');
$this->table->set_template($tmpl);
$this->table->set_caption("Work Items");
//-- Header Row
$this->table->set_heading('ID', 'Date', 'Title', 'Done', 'Item');
//-- Content Rows
$data['title'] = 'Page Display';
$this->load->view('work_links', $data);
Note it points to work_links, a view
the next function is this
function done()
{
$data = array();
if ($query = $this->work_m->dead_work()) {
$data['dead'] = $query;
}
$tmpl = array('table_open' =>
'<table border="0" cellpadding="0" cellspacing="0">', 'heading_row_start' =>
'<tr class="heading">', 'heading_row_end' => '</tr>', 'heading_cell_start' =>
'<th>', 'heading_cell_end' => '</th>', 'row_start' => '<tr>', 'row_end' =>
'</tr>', 'cell_start' => '<td>', 'cell_end' =&开发者_Python百科gt; '</td>', 'row_alt_start' =>
'<tr class="alt">', 'row_alt_end' => '</tr>', 'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>', 'table_close' => '</table>');
$this->table->set_template($tmpl);
$this->load->view('work_links', $data);
}
The models behind them are basic calls to the database
Now this code in the view goes with the first function above(in the article div) and works perfectly
foreach($result as $row)
{
$this->table->add_row(
anchor("work/fill_form/$row->id", $row->id),
$row->date,
$row->title,
$row->compl,
$this->typography->auto_typography($row->item)
);
}
$table = $this->table->generate();
echo $table;
this is the second code to go into the aside div(and goes with the second functiin above)
if (isset($dead)){
foreach($dead as $row)
{
$this->table->add_row(
$row->id,
$row->title,
$row->finish
);
}
}
$this->table->set_heading('ID', 'Title');
$table = $this->table->generate();
echo $table;
The last code only picks up the data from the first function no matter what i do.
To use a view twice on a page, or use two different views from one controller route, call $this->load->view()
once for each view, but passing $data
only once. That means that all of your model data is added to a single $data
variable, using different keys or array elements.
Example:
$this->data = array(
'people' => $this->SomeModel->list1(),
'dogs' => $this->SomeModel->list2() );
$this->load->view('list-view', $this->data);
$this->load->view('list-view');
$this->load->view('footer');
For many of my own sites, I use a special base controller that implements a view()
function to load each of my common page parts so that each controller route needs only to populate $this->data
and call $this->view('unique-part')
(as most pages will have a header, footer, sidebar, navigation bar, and then some unique view in the middle).
Example:
function page($p, $extra) {
$this->load->view('_parts/header', array_merge($this->data, $extra));
$this->load->view("$base/sidebar");
$this->load->view("$base/$p");
$this->load->view('_parts/footer');
}
Note that the header/footer are in a _parts folder, shared among other routes. CodeIgniter caches the view data so that the data will be available to any view after the first one that references it in a given route.
I think this is a job for a modular approach: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
Q. What is Modular HMVC, why should I use it?
A. Modular HMVC = Multiple MVC triads
This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it.
In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.
The differences between using a library and a “Modular HMVC” HMVC class is: 1) No need to get and use the CI instance within an HMVC class 2) HMVC classes are stored in a modules directory as opposed to the libraries directory.
Bonfire also uses HMVC.
精彩评论