简体   繁体   English

无法将类型为stdClass的对象用作数组

[英]Cannot use object of type stdClass as array

I want to show all records from database..but when i want to show records the browser show error and the error is: " 我想显示数据库中的所有记录。但是当我想显示记录时,浏览器显示错误,错误是:

A PHP Error was encountered 遇到PHP错误

Severity: Error 严重性:错误

Message: Cannot use object of type stdClass as array 消息:无法将类型为stdClass的对象用作数组

Filename: views/show_designation.php 文件名:views / show_designation.php

Line Number: 53 行号:53

Backtrace: " 回溯:

View 视图

<?php  print_r($designation); $i=1;foreach($designation as $desig): ?>
                                  <tr>
                                       <td><?php echo $i; ?></td>

                                       <td><?php echo $desig['name']; ?></td>  This is (LINE 53)

                                        <td><a href="<?php echo base_url('index.php/designation/delete_designation/'.$desig[$i]->dkey);?>"><input type="button" value="Delete" class="btn btn-danger"/></a>
                                        <td class="numeric"><a id= "<?php echo $desig[$i]->dkey; ?>"  onclick="edit_designation('<?php echo $designation[$i]->dkey; ?>')" data-target="#myModal" data-toggle="modal" style="color:#ffffff;" href="#myModal"><button class="btn blue" type="button">Edit</button>

                                  </tr>
                        <?php $i++;endforeach;?>

COntroller 控制器

public function get_designation()
    {
        $this->load->model('designation_model');
        $userlist=$this->designation_model->get_alldesignation_model();
        $data['designation']=$userlist;
        $this->load->view('show_designation',$data);
    }//edit_project

Model 模型

public function get_alldesignation_model()
    {
        $query = $this->db->get('designations');
        $result=$query->result();
        return $result;
    }

You are using array syntax where you need object syntax. 您在需要对象语法的地方使用数组语法。 Replace 更换

$desig['name']

with

$desig->name

Note: you will have the same problem in those lines referring to $desig[$i] . 注意:在那些引用$desig[$i]行中,您将遇到相同的问题。 I'm not sure what you're trying to do there, but it looks like you are blending some syntax from a for loop into your foreach loop. 我不确定您要在其中做什么,但似乎您正在将for循环中的某些语法混合到foreach循环中。 If I understand correctly, you simply need to drop the [$i] in each place you use it. 如果我理解正确,则只需将[$i]放在使用的每个位置。

To use result of query as array say 要将查询结果用作数组说

 $result=$query->result_array();

because by default result is returned as object 因为默认情况下,结果作为对象返回

Try this 尝试这个

in Model 模型中

public function get_alldesignation_model()
    {
        $query = $this->db->get('designations');
        $result=$query->result_array();//Store data as Array
        return $result;
    }

in view 鉴于

<?php 
    $i=1;
    foreach($designation as $desig)
{
    ?>

    <tr>
        <td><?php echo $i  ?></td>
        <td><?php echo $desig['name']; ?></td>
        <td><a href="<?php echo base_url('index.php/designation/delete_designation/'.$desig[$i]->dkey);?>">
                <input type="button" value="Delete" class="btn btn-danger"/>
            </a>
        <td class="numeric"><a id= "<?php echo $desig[$i]->dkey; ?>"  onclick="edit_designation('<?php echo $designation[$i]->dkey; ?>')" data-target="#myModal" data-toggle="modal" style="color:#ffffff;" href="#myModal"><button class="btn blue" type="button">Edit</button>

    </tr>
    <?php
    $i++;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM