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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端