Hi,

I'm trying to insert data into array after I retrieve it from database but I keep getting this error message:

'Fatal error: Cannot use object of type stdClass as array in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\DeliciousCupcakes\system\application\models\cupcake.php on line 22'

Please help me. The framework I'm using is codeigniter.

DefaultDirectory.php file

<?php
    if(!defined('BASEPATH'))
    {
        exit ("No direct access script allowed");
    }
?>


<?php
    class DefaultDirectory extends Controller
    {

        public function DefaultDirectory()
        {
            parent::Controller();
            $this->load->helper('url');
            $this->load->helper('html');
            $this->load->database();
            $this->load->model('cupcake');

        }

        public function index()
        {
            //default page
            if(!$this->uri->segment(3))
            {
                $this->show_web_page('home');
            }

            //webpage show according to what user clicks
            $valid_links = array(
                        'home',
                        'about',
                        'order',
                        'contact',
                        'faq'
                    );

            if(in_array($this->uri->segment(3), $valid_links))
            {
                if($this->uri->segment(3)=='order')
                {
                    //retrieve the products from database
                   $cupcake = new cupcake();
                   $cup_cakes = $cupcake->get_products('cupcake');
                   $this->load->view('order',$cup_cakes);

                }
                else
                {
                    $this->show_web_page($this->uri->segment(3));
                }
            }
        
        }

     

        public function show_web_page($viewpage)
        {
            $this->load->view('header');
            $this->load->view($viewpage);
            $this->load->view('footer');
        }
    }
?>

cupcake.php file

<?php
            if(!defined('BASEPATH'))exit("No direct script access allowed");
 ?>

<?
    class cupcake extends Model
    {
        public $table = 'product';
        public $cupcake_array = array();
            function cupcake()
            {
                parent::Model();

            }


            public function get_products($value)
            {
                $result = $this->db->get_where($this->table,array('Type'=>$value));
                $counter = 0;
                foreach($result->result() as $row)
                {   $cupcake_name = $row['Name'];
//The line below is where the error occurs
                    $this->cupcake_array[$counter]= $cupcake_name;
                    $counter++;
                }
                 return $this->cupcake_array;
            }
    }
    
?>

I figured it out. I have to use

$this->cupcake_array[$counter]= $row->Name;

instead of

$this->cupcake_array[$counter]= $row['Name'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.