Undefined Variable error in View

2019-08-02 19:25发布

问题:

My controller is in this question: how to add pagination to this

But now, i'm getting undefined variable in my view, View has an input

<input name='query'/>

which i receive it in controller as,

$query = $this->input->get('query'); 

Now, i am trying to pass the same query string back into my view inside the link of pagination.

<a href="<php echo base_url(); ?>main/search/query=<?php echo $query; ?>&off=<?php echo $off=5;">1</a>

$off works but not $query. But i'm NOT saving the $query to the database. so, i cant use $data['query'] as well. totally confused.

回答1:

$off is not throwing an error because you are assigning the integer 5 to it.

You need to pass data to the view from inside the controller by sticking it in an array like

 $data['query']= $query;
 $this->load->view('myview', $data);

Then $query is available to the view. Not sure why you say you can't do this