Cannot use object of type stdClass as array

12.4k views Asked by At

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

Severity: Error

Message: Cannot use object of type stdClass as array

Filename: views/show_designation.php

Line Number: 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;
    }
3

There are 3 answers

0
splash58 On BEST ANSWER

To use result of query as array say

 $result=$query->result_array();

because by default result is returned as object

2
elixenide On

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]. 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. If I understand correctly, you simply need to drop the [$i] in each place you use it.

0
Abdulla Nilam On

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