gpt4 book ai didi

php - 试图获得非对象的属性? PHP/代码点火器

转载 作者:太空狗 更新时间:2023-10-29 14:42:21 25 4
gpt4 key购买 nike

我真的被这个问题吓坏了,它开始推迟我项目的其余部分,这真的让我很沮丧。

我正在尝试使用从数据库表中获取的值来填充下拉列表,这样如果将来用户想要向下拉列表中添加更多选项,他们可以将它们添加到数据库中的表中。

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

这是我收到的错误消息:

A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/submit.php Line Number: 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;
}

这是接收参数并将其传递到我的 View 的 Controller 函数。这很好用!

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

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

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

printr() 显示它包含这些值:

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

最佳答案

formdata 是数组的数组,而不是对象,因此只需更改您的 View 即可:

<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>

关于php - 试图获得非对象的属性? PHP/代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6847287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com