试图获得非对象的属性? PHP /笨

问题描述 投票:6回答:2

我一直在用这个把头发拉出来,并开始推迟我的项目的其余部分,这真的让我失望。

我试图使用从数据库表中获取的值来填充下拉,以便将来用户想要向下拉添加更多选项时,可以将它们添加到数据库中的表中。

我正在使用Codeigniter平台(PHP)使用MVC设计模式。

这是我得到的错误消息:

遇到PHP错误严重性:通知消息:尝试获取非对象的属性文件名:views / submit.php行号:139

我的模型函数就是这里,它从名为“Staff”的表中检索行。这很好用!

function retrieve_values()
{
    $query = $this->db->get('staff');

    if ($query->num_rows() > 0) 
    { 
        //true if there are rows in the table
        return $query->result_array(); //returns an object of data
    }

    return false;
}

这是控制器函数,它接收参数并将其传递给我的视图。这很好用!

public function displayform()
{

    //Checks if a user is logged in, if they are not they get redirected -
    if ( $this->session->userdata('name') == FALSE || $this->session->userdata('access_level') == FALSE)
    {
        redirect ('site/index');// to home page
    }

    //Stores the returned array in instance called "formdata" which will be passed to the view to be used in pulldown menu
    $page['formdata']=$this->submit_model->retrieve_values();

    //This loads the form 
    //Instance of "page" in array "page" specifies the file name of the page to load
    $page['page'] = 'submit';
    $this->load->view('template', $page );

    return;
}

这是导致问题的视图的一部分:我使用foreach然后将数组的实例回显到选项中。

<select>
    <?php foreach ($formdata as $row) { ?>
        <option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
    <?php } ?>
</select>

变量printr()$formdata显示它包含以下值:

Array (
    [0] => Array (
        [staff_id] => 1
        [name] => Cardiology Nurse
    )
    [1] => Array (
        [staff_id] => 2
        [name] => Radiology Nurse
    )
    [2] => Array (
        [staff_id] => 3
        [name] => Scrub Nurse
    )
    [3] => Array (
        [staff_id] => 4
        [name] => Circulating Nurse
    )
    [4] => Array (
        [staff_id] => 5
        [name] => Nurse
    )
    [5] => Array (
        [staff_id] => 6
        [name] => Training Nurse
    )
    [6] => Array (
        [staff_id] => 7
        [name] => Physiologist
    )
    [7] => Array (
        [staff_id] => 8
        [name] => Radiographer
    )
    [8] => Array (
        [staff_id] => 9
        [name] => Consultant
    )
    [9] => Array (
        [staff_id] => 10
        [name] => Radiologist
    )
    [10] => Array (
        [staff_id] => 11
        [name] => Cardiologist
    )
    [11] => Array (
        [staff_id] => 12
        [name] => Anaethestist
    )
    [12] => Array (
        [staff_id] => 13
        [name] => Non-medical Staff
    )
)
php html arrays codeigniter foreach
2个回答
15
投票

formdata是一个数组的数组,而不是对象,所以只需在视图中更改:

<option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
// to
<option value="<?php echo $row['staff_id']; ?>"><?php echo $row['name']; ?></option>

3
投票

您已经使用了result_array,这意味着您将获得一个数组数组,而不是一个对象数组。您可以修改视图以使其具有以下内容:

<option value="<?php echo  $row['staff_id']; ?>">
    <?php echo  $row['name']; ?>
</option>

代替

<option value="<?php echo $row->staff_id; ?>">
    <?php echo $row->name; ?>
</option> 

或者你可以在模型中将$query->result_array()更改为$query->result()

© www.soinside.com 2019 - 2024. All rights reserved.