0

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;
    }
1
  • You're trying to use an object as an array(yey, I can read errors, but really now). Since the returned value is an object you need to access the values like you'd do with an object so: $stdObject->value NOT $stdObject['value']. If you wish to deal with arrays you can convert the object to an array. [See here](stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php) for how to do that. Commented Jun 8, 2015 at 8:27

3 Answers 3

1

To use result of query as array say

 $result=$query->result_array();

because by default result is returned as object

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

yeah u r right... firstly i use for loop to show records from database..but now i want to change my for loop to foreach loop to show result.
@farkhund did you try these changes?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.