php - Undefined CodeIgniter variable
Get the solution ↓↓↓Please, I don't know what am not getting right here...
This is my controller:
$result = $this->Career_model->quick_search($gender, $country); // get the data once and then check if data empty or not
if(!empty($result)){ // success
$data['result'] = $result;
}else{ // fail
$data = $this->session->set_flashdata('error','No data found');
}
redirect('pages/search_result',$data);
And here is the view:
if((isset($result)) && (!empty($result))): // checking if the result is empty
foreach ($result as $key => $value):
echo $value->name;
endforeach;
else:
echo "No result was found."; // this is the part that gets executed
endif;
But once I change my controller to this:
$result = $this->Career_model->quick_search($gender, $country); // get the data once and then check if data empty or not
if(!empty($result)){ // success
$data = $this->session->set_flashdata('error','No data found'); //this is the part that gets executed, meaning that my result is not empty.
}else{ // fail
$data['result'] = $result;
}
redirect('pages/search_result',$data);
And the view to this:
foreach ($result as $key => $value):
echo $value->name;
endforeach;
I only end up with anUndefined variable: result
error message. What am I doing wrong?
Answer
Solution:
Because when success$data = $this->session->set_flashdata('error','No data found')
it doesn't executeelse block
so$data['resuly']=undefined
When you will change your controller alter this line$data = $this->session->set_flashdata('error','No data found');
with$data['result'] =$result
Answer
Solution:
Thanks to everyone who tried to help me solve the above problem.
This is what I later did...
My new model:
function quick_search($country,$gender,$from, $to)
{
$this->db->join("about" , "account.ref_id = about.user_id");
$this->db->like('gender',$gender);
$this->db->or_like('country',$country);
$this->db->or_like('age',$from);
$this->db->where('age <=',$to);
$query = $this->db->get('account');
return $query->result_array();
}
My controller:
$result = $this->Career_model->quick_search($country, $gender, $from, $to); // get the data once and then check if data empty or not if(!empty($result)): // success
$data['result'] = $result;
else: // fail
$data = $this->session->set_flashdata('error','No data found');
endif;
$this->load->view('pages/search_result',$data);
My new view:
foreach ($result as $key => $value):
echo $value['name']." ".$value['gender'].'<br>';
endforeach;
Once again, thank you and happy coding!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.