Problem

umaxim
umaxim used Ask the Experts™
on
I have code
<?
class Menu_model extends CI_Controller {
    
        function Menu_model(){
            $this->CI =& get_instance();
        }
        
        function select_all(){
            $q = $this->CI->db->query("Select * from navigation");
            if($q->num_rows()>0){
                $i = 0;
                foreach($q->result() as $row){
                    $data['name'] = "navigation_".$row['id'];
                    
                    $g1 = $this->CI->db->query("Select * from navigation_items where id='".$row['id']."'");
                    if($q1->num_rows()>0){
                        $data['html'] .= "";
                        foreach($q1->result() as $row1){
                            $data['html'] .= "<p>".$row1['name']."</p>";
                        }
                    }  
                $i++;
                $rdata[] = $data;
                }
              
            }
            return $rdata;
        }
        
}
?>

Open in new window


and i get error
Fatal error: Cannot use object of type stdClass as array in /var/www/vhosts/iwebpixels.com/subdomains/cl/httpdocs/application/models/menu_model.php on line 13

I use codeigniter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Assuming line 13 is this line

$data['name'] = "navigation_".$row['id'];

Then the error is caused by either $row or $data being an object rather than an array. Since $data has no global declared for it and this line is its first use, I would guess that $row is the problem. The previous line says

foreach($q->result() as $row){

so I would guess that $row is an object and therefore cannot be accessed via array notation. Try using $row->id instead like so

                foreach($q->result() as $row){
                    $data['name'] = "navigation_".$row->id;
 

Open in new window


If in doubt do print_r( $row ); exit;  between lines 12 and 13 and it should reveal the structure of $row and you can work from there. It may be that you need to access the 'id' property via an accessor method.
Change this

$data['name'] = "navigation_".$row['id'];

into this

$data['name'] = "navigation_".$row->id;

I'm not positive, but I think that's what it wants.

The error should then move to another line, if so, change the other references.

Author

Commented:
thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial