简体   繁体   English

在Codeigniter视图中加载数据

[英]Load data in a Codeigniter view

I want to load data to my view, particularly a variable in my controller items . 我想将数据加载到我的视图中,特别是我的控制器items的变量。 But in my controller I want to dynamically set what items is before it is push to the view. 但是在我的控制器中,我想在推送到视图之前动态设置哪些items

I tried the code below but it doesn't work. 我尝试了下面的代码,但它不起作用。 The view loads up with an error "Undefined variable: items" . 视图加载错误“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. 我希望这个工作原理是$data['items']默认设置为$one ,当页面加载时,但在页面上我有一个选择框,所以如果选择框是改变了$data['items']将被设置为其他东西。 But this is only if the select box is used, else, it should look up with the $data[items]=$one . 但这只是在使用选择框时,否则,它应该使用$data[items]=$one查找。 The $data array has other values that need to be loaded in the view such as "others" and "links". $ data数组具有需要在视图中加载的其他值,例如“others”和“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". 上面的索引控制器功能适用于我的控制器“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? 我该如何解决?

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"] 因为在设置$data["items"]后重新分配$data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM