0

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;
            }
    }
    
?>
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by sunny124
0

I figured it out. I have to use

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

instead of

$this->cupcake_array[$counter]= $row['Name'];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.