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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6847287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:27:15  来源:igfitidea点击:

Trying to get property of non-object? PHP/Codeigniter

phphtmlarrayscodeigniterforeach

提问by sqlmole

I have been literally pulling my hair out with this one and its beginning to delay the rest of my project and it really is getting me down.

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

I am trying to populate a pull down using values taken from a database table so that if in the future the user wants to add more options to the pull down they can add them to the table in the database.

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

I am using the Codeigniter platform (PHP) using MVC design pattern.

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

Here is the error message I am getting:

这是我收到的错误消息:

A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/submit.php Line Number: 139

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

My Model function is this here which retrieves the rows from the table called "Staff". This works fine!

我的模型函数在这里检索名为“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;
}

This is the controller function which receives the parameter and passes it to my view. This works fine!

这是接收参数并将其传递给我的视图的控制器函数。这工作正常!

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

This is the part of the view which is causing the problem: I am using a foreach and then echoing the instances of the array into the option.

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

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

printr()of the variable $formdatashows that it contains these values:

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

回答by Jarek Tkaczyk

formdata is an array of arrays, not objects, so simply change in your view:

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>

回答by cwallenpoole

You've used result_array, which means you're going to get an array of arrays, instead of an array of objects. You can either modify your view to have this:

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

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

instead of

代替

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

or you can change $query->result_array()to $query->result()in the model.

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