未定义的代码点火器变量



拜托,我不知道这里有什么不对...

这是我的控制器:

$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);

这是视图:

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;

但是一旦我将控制器更改为:

$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);

对此的看法是:

foreach ($result as $key => $value):
echo $value->name;
endforeach;

我最终只会收到一条Undefined variable: result错误消息。我做错了什么?

因为当成功$data = $this->session->set_flashdata('error','No data found')时,它不会执行else block那么$data['resuly']=undefined

当您要更改控制器时,请使用$data['result'] =$result更改此行$data = $this->session->set_flashdata('error','No data found');

感谢所有试图帮助我解决上述问题的人。

这就是我后来所做的...

我的新模型:

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();
}

我的控制器:

$result = $this->Career_model->quick_search($country, $gender, $from, $to(; 获取数据一次,然后检查数据是否为空 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);

我的新观点:

foreach ($result as $key => $value):
echo $value['name']." ".$value['gender'].'<br>';
endforeach;

再次感谢您,祝您编码愉快!

相关内容

  • 没有找到相关文章

最新更新