Codeigniter:在数组中显示数据,错误:尝试获取非对象的属性

时间:2012-10-11 04:14:25

标签: arrays codeigniter

好的,我正在尝试在数组中显示数据。

我的观点:

echo form_open(base_url() . 'membersController/searchMember/');
echo form_label('Member ID: ', 'memID', array('for' => 'memID'));
echo form_input(array('type' => 'text', 
                      'name' => 'memID', 
                      'id' => 'memID', 
                      'value' => set_value('memID')));
echo form_submit(array('type' => 'submit', 'value' => 'Search'), 'Search') . "<br />";
echo form_close();
if (empty($data)) {

} else {
//displaying the array
}

我的控制器:

public function searchMember() {
    $memID = $this->input->post('memID');
    //Load membersModel
    $this->load->model('membersModel');
    //Access the getMemberData function to search for a member
    $data['memDetails'] = $this->membersModel->getMemberData($memID);

//以下行只是为了测试数组是否正在填充

    echo $data[0]->memFname;

    if (empty($data)) {
        echo "Hi";
        $message = "Member not found!";
        echo $message;
    } else {
        //Load view
        $this->load->view('memberView', $data);
    }
}

我的模特:

function getMemberData($memID) {
    //SQL Statement to get a Member from the database based on member ID
    $data = array();
    $this->db->select('*');
    $this->db->from('members');
    $this->db->where('memID', $memID);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data['memberdata'] = $row;
        }
    }
    $query->free_result();
    //return the array to the controller
    return $data;
}

我一直收到这些错误:

遇到PHP错误

严重性:注意

消息:未定义的偏移量:0

文件名:controllers / membersController.php

行号:24

遇到PHP错误

严重性:注意

消息:尝试获取非对象的属性

文件名:controllers / membersController.php

行号:24

1 个答案:

答案 0 :(得分:2)

进行这些更改

<强>模型

if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        $data[] = $row;
    }
}
$query->free_result();
//return the array to the controller
return $data;

<强>控制器

$data['data'] = $this->membersModel->getMemberData($memID);