douxi0098 2009-11-12 00:49
浏览 14
已采纳

一个视图上的2个Codeigniter控制器查询

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.

  • 写回答

2条回答 默认 最新

  • dr5648 2009-11-12 01:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数