我的控制器出错了。我试图使用来自模型函数的结果来调用我的模型中的另一个函数。我正在使用Codeigniter Framework。希望你能帮助我。感谢
控制器:
function photographer_campaign_details(){
$camp_id = $this->uri->segment(4);
$data['page_title'] = 'Photographer Campaign Details';
$adminId = $this->session->userdata('adminid');
$campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);
$data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
$data['campaign'] = $campaign;
$this->load->view('admin/photographer_campaign_details',$data);
}
我的模特:
function get_all_photographer_campaign_details($camp_id) {
$this->db->select('*');
$this->db->where('campaign_id',$camp_id);
$query = $this->db->get('ps_campaigns');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$data[] = $row;
}
return $data;
}
return array();
}
//get seller name by id
function get_seller_name_by_id($uid)
{
$this->db->select('firstname, lastname');
$this->db->where('id', $uid);
$query = $this->db->get('ps_users');
//return $query->row();
return $query->result_array();
}
错误来自控制器:未定义的索引:uid
答案 0 :(得分:1)
查看get_all_photographer_campaign_details
,如果未找到任何行,则返回一个空数组。
在您的控制器中,您永远不会检查是否找到了有效的条目。因此,当您在与某个条目不对应的网址中引用ID时,您会收到undefined index: uid
,因为$campaign
为空,且没有{{1}钥匙。尝试这样的事情:
uid
此外,如果您执行查找数据,则错误地返回数据。即function photographer_campaign_details(){
$camp_id = $this->uri->segment(4);
$data['page_title'] = 'Photographer Campaign Details';
$adminId = $this->session->userdata('adminid');
$campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);
if (!$campaign ){
show_404();
}
$data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
$data['campaign'] = $campaign;
$this->load->view('admin/photographer_campaign_details',$data);
}
中的这一位:
get_all_photographer_campaign_details
应该是这样的:
foreach ($query->result_array() as $row) {
$data[] = $row;
}
问题是您将行追加到 foreach ($query->result_array() as $row) {
$data = $row;
break;
}
中的一行,但您的控制器希望自己获取实际数据。即你的控制器期待这个:
$data
但是你要回来了:
[
'campaignid' => 1,
'uid' => 'ijerjeire'
]
请注意所有内容都被包裹的额外数组。基本上,当您的控制器只是期待结果时,您的模型将返回一组结果。如果只返回一个广告系列,我的上述建议将有效。如果不是这种情况,则需要调整控制器而不是模型方法。
重申我的另一点:确保并验证来自URL的用户输入。否则,您将返回PHP错误而不是404。