简体   繁体   中英

Load data in a Codeigniter view

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?

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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