Welcome Guest, Not a member yet? Register   Sign In
Trying to get property of non-object
#1

Hello,

I am facing another error message here:



A PHP Error was encountered

Severity: Notice

Message:  Trying to get property of non-object

Filename:  views/pcategories.php

Line Number: 58

Backtrace:  

File: C:\Program Files\EasyPHP\www\CompanyGiondaCI\application\views\pcategories.php

Line: 58

Function: _error_handler

http://127.0.0.1/CompanyGiondaCI/index.p...tparentctg';">EDIT

------------------------------------------------

views/pcategories.php


PHP Code:
<?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; ?>

line 58: 

<td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">EDIT</button></td>

controllers/cpages.php

PHP Code:
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

PHP Code:
public function call_parentctg()
    {
        
        
$query $this->db->get('menu_parent');
        return 
$query->result_array();
                        
    } 


How to fix the error message?
" If I looks more intelligence please increase my reputation."
Reply
#2

(08-28-2016, 06:29 PM)davy_yg Wrote: Hello,

I am facing another error message here:



A PHP Error was encountered

Severity: Notice

Message:  Trying to get property of non-object

Filename:  views/pcategories.php

Line Number: 58

Backtrace:  

File: C:\Program Files\EasyPHP\www\CompanyGiondaCI\application\views\pcategories.php

Line: 58

Function: _error_handler

http://127.0.0.1/CompanyGiondaCI/index.p...tparentctg';">EDIT

------------------------------------------------

views/pcategories.php


PHP Code:
<?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; ?>

line 58: 

<td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">EDIT</button></td>

controllers/cpages.php

PHP Code:
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

PHP Code:
public function call_parentctg()
 {
 
 
$query $this->db->get('menu_parent');
 return 
$query->result_array();
 
 } 


How to fix the error message?

 It is because you trying to return an array, not object. Use ->result() instead of ->result_array().

CHEERS!
[Just a programmer] Cool [/Just a programmer]
Reply
#3

Fatal error: Cannot use object of type stdClass as array in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\pcategories.php on line 56
A PHP Error was encountered

Severity: Error

If I change call_parentctg() to return an array. I start to have this error message in my views:

Message: Cannot use object of type stdClass as array

Filename: views/pcategories.php

Line Number: 56

Backtrace:


<?php foreach ($posts as $post): ?>

<tr>
<td><?php echo $post['ctgparent_name']; ?></td> // line 56
" If I looks more intelligence please increase my reputation."
Reply
#4

(08-29-2016, 11:41 PM)davy_yg Wrote: Fatal error: Cannot use object of type stdClass as array in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\pcategories.php on line 56
A PHP Error was encountered

Severity: Error

If I change call_parentctg() to return an array.  I start to have this error message in my views:

Message: Cannot use object of type stdClass as array

Filename: views/pcategories.php

Line Number: 56

Backtrace:


<?php foreach ($posts as $post): ?>

<tr>
<td><?php echo $post['ctgparent_name']; ?></td>   // line 56

Make it $post->ctgparent_name and $post->ctgparent_description not $post['ctgparent_name'] and $post['ctgparent_description'] since the $posts variable is no longer array but object. Make yourself familiar with array and object because these are very useful in many aspects and commonly used in development.



Cheeeers!
[Just a programmer] Cool [/Just a programmer]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB