views:

1780

answers:

2

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' => '</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.

+1  A: 

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.

Bruce Alderson
Ah I understand, no matter what I am doing in the second function only the first one matters because I am using $data twice
Brad
but if I do that, then the second function and resulting table will have no hook to what data to use.Most of my pages use 3 parts, a template. header footer, aside and the view I point to that I call content. I was trying to get away from that as it makes each page the same, and/or will create a monster file system. But I guess I could create a subfolder for each page and use the template method
Brad
I updated my answer to show how I handle header/footer ... and to mention that CodeIgniter caches view data for a given CGI request.
Bruce Alderson
A: 

I no longer see a comment link so I will thank you here Bruce. I didn't realize that you could send data to a div until you made that clear. Sorry it took me a while, my work takes me away from home at times. I do have the subfolders set up for the header and footer, along with the aside div. I see now what you are saying.

Brad