0
votes

How to fix this error message?

Can you help?

Fatal error: Cannot use object of type stdClass as array in C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\views\pcategories.php on line 56

Fatal error: Cannot use object of type stdClass as array in C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\views\pcategories.php on line 56

A PHP Error was encountered Severity: Error Message: Cannot use object of type stdClass as array Filename: views/pcategories.php

views/pcategories.php

<div class="row-fluid">
                <div class="span12">
                    
                    <button type="button" class="add" onclick="location.href='<?php echo site_url('cpages/addparentctg'); ?>';">ADD PARENT CATEGORIES</button> 
                    
                    <div class="widget-box">
                        <div class="widget-title"><h5>Parent Categories</h5></div>
                        <div class="widget-content">
                        
                        <table border="0" style="width: 100%; height: 90px;">
                            <tr>
                                <td>PARENT NAME</td>
                                <td>DESCRIPTION</td>
                                <td>EDIT</td>
                                <td>DELETE</td>    
                            </td>
                            
                            <?php foreach ($posts as $post): ?>
                            
                            <tr>
                                <td><?php echo $post['ctgparent_name']; ?></td>
                                <td><?php echo $post['ctgparent_description']; ?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">DELETE</button></td>    
                            </td>    
                                                                                    
                            <?php endforeach; ?>    
                            
                        </table>            
                        </div>
                    </div>                    
                </div>
            </div> 

controllers/Cpages.php

public function pcategories() { 
    
            
        $data['posts'] = $this->Mpages->call_parentctg();

        //$data['ctgparent_name'] = $this->Mpages->call_parentctg();
        //$data['ctgparent_description'] = "Second";//$this->Mpages->retrieve_parentctg();
        
        $this->load->view('pcategories', $data); 
        
    } 

models/Mpages.php

<!-- begin snippet: js hide: false console: true babel: false -->
2

2 Answers

0
votes

CodeIgniter returns result rows as objects, not arrays. So you need to access it using -> . Like instead of $post['ctgparent_name']; use $post->ctgparent_name.

0
votes

This happens if you're trying to access properties of an object with $object['property'] notation instead you have to use $object->property.In your case you're doing $post['ctgparent_name'] and echo $post['ctgparent_description'] and you can change those to $post->ctgparent_name and $post->ctgparent_description respectively.

There is another solution if you're using codeigniter active records to fetch data from the database (as I suspect you do). If you're using $query->result() method, this will give you an object. You can use $query->result_array() instead so you now have an array of results you can play with $array['whatever'] notation.

Further, if you're using PDO or mysqli there are methods to get database result sets as an array. Refer the official documentation PDO result array, Mysql result array or Mysql associative array.