dougong9987 2014-01-23 17:24
浏览 64
已采纳

在Codeigniter视图中加载数据

I want to load data to my view, particularly a variable in my controller items. But in my controller I want to dynamically set what items is before it is push to the view.

I tried the code below but it doesn't work. The view loads up with an error "Undefined variable: items" .

public function index(){
    if ($this->input->post('filter'))
    {
        $search = $this->input->post('filter');
        $test=$this->upload_model->test_r(); 
        $data['items']=$test;
    }
    else
    {
        $data['items']=$one;
    }
    $data=array ('other'=>$othrs, 'links'=>$links);
    $this->load->view('gallery_view', $data);
}

How I would like this to work is that $data['items'] is set to $one by default, when the page loads up, but on the page I have a select box, so I want that if the select box is is changed that $data['items'] would be set to something else. But this is only if the select box is used, else, it should look up with the $data[items]=$one. The $data array has other values that need to be loaded in the view such as "others" and "links".

The select box on my view

<?php echo form_open(base_url().'page') ?>
<form class="form-inline" role="form">
    <select class="form-control" id="filter" name="filter" onchange="this.form.submit()">
        <option value="1">1</option>
        <option>2</option>
        <option>3</option>
    </select>
</form>
<?php echo form_close(); ?>  

The index controller function above is for my controller "Pages". The value from the select box is captured fine, when I do an echo on the value passed it shows up correctly.

The problem is getting the view data "items" to change depending on if the select box is used.

How do I fix this?

  • 写回答

1条回答 默认 最新

  • doutangshuan6473 2014-01-23 17:27
    关注

    Do this line before if condition

    $data=array ('other'=>$othrs, 'links'=>$links,'items'=>"");
    

    Remove below line in controller.

    $data=array ('other'=>$othrs, 'links'=>$links);
    

    Because you are re-assign $data after setting $data["items"]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?