Welcome Guest, Not a member yet? Register   Sign In
Need help ot create a data matrix in Controller
#1

hi,
I am trying to generate a page in Codeigniter 3.1. 

Following is the code for Controller
Code:
public function list_products()
    {
        $this->load->model('masters/ProductCategoryCat_model', 'pmode');
        $this->load->model('masters/FactoryCat_model', 'fmode');

        $this->data['title'] = 'View Product Category List';
        $this->data['response_msg'] = $this->session->flashdata('response_msg');
        $this->data['response_success'] = $this->session->flashdata('response_success');

        $this->data['product_category'] = $this->pmode->getProdcuCategoryList();
        log_message('error','product_category:' . json_encode($this->data['product_category']));

        foreach ($this->data['product_category'] as $item_key => $item) {
            var_dump($this->data['product_category']);
            $factory_list = $this->fmode->getFactoryByProductId($item->id);
            log_message('error','factory_list:' . json_encode($factory_list));
            foreach($factory_list as $fact_key=>$factory)
            {
                var_dump($factory);
                $this->data['product_category'][$item_key]['factory'][$fact_key] = $factory->factory_name;
            }
        }
        log_message('error','data:' . json_encode($this->data));
        $this->renderTemplate('masters/product_category/product_category_list', $this->data);

    }


Whenever I try to run this code it generates following error:

An uncaught Exception was encountered
Type: Error
Message: Cannot use object of type stdClass as array


It seems error is at the following line:
$this->data['product_category'][$item_key]['factory'][$fact_key] = $factory->factory_name;


Values of $this->data['product_category']:
[{"id":"1","category_name":"PVC_BOND","category_description":"PVC SHOES ","status":"Active"},{"id":"2","category_name":"PVC_SLIPPER","category_description":"air blowing slippers ","status":"Active"},{"id":"3","category_name":"GUMBOOT","category_description":"PVC GUMBOOT ","status":"Active"},{"id":"4","category_name":"EVA","category_description":"MEN  & WOMEN EVA SLIPPERS ","status":"Active"},{"id":"5","category_name":"PU","category_description":"PU SLIPPERS ","status":"Active"},{"id":"6","category_name":"PVC_BELLY","category_description":"PVC LADIES BELLY ","status":"Active"},{"id":"7","category_name":"GAUTAM MADAAN","category_description":"","status":"Active"},{"id":"8","category_name":"TEST_TR-78.55","category_description":"test category","status":"Active"}]

And Value of $factory_list:

[{"factory_id":"1","factory_name":"RAMANAND ENTERPRISES  PLOT NO 13 14"},{"factory_id":"20","factory_name":"TEST58"}]

ERROR - 2020-08-21 16:09:06 --> Severity: error --> Exception: Cannot use object of type stdClass as array C:\Apache24\htdocs\inv\application\controllers\masters\Variety.php 72


What am I doing wrong here.

Can you help me please.

Regards
Reply
#2

(08-21-2020, 06:48 AM)arun67 Wrote: hi,
I am trying to generate a page in Codeigniter 3.1. 

Following is the code for Controller
Code:
public function list_products()
    {
        $this->load->model('masters/ProductCategoryCat_model', 'pmode');
        $this->load->model('masters/FactoryCat_model', 'fmode');

        $this->data['title'] = 'View Product Category List';
        $this->data['response_msg'] = $this->session->flashdata('response_msg');
        $this->data['response_success'] = $this->session->flashdata('response_success');

        $this->data['product_category'] = $this->pmode->getProdcuCategoryList();
        log_message('error','product_category:' . json_encode($this->data['product_category']));

        foreach ($this->data['product_category'] as $item_key => $item) {
            var_dump($this->data['product_category']);
            $factory_list = $this->fmode->getFactoryByProductId($item->id);
            log_message('error','factory_list:' . json_encode($factory_list));
            foreach($factory_list as $fact_key=>$factory)
            {
                var_dump($factory);
                $this->data['product_category'][$item_key]['factory'][$fact_key] = $factory->factory_name;
            }
        }
        log_message('error','data:' . json_encode($this->data));
        $this->renderTemplate('masters/product_category/product_category_list', $this->data);

    }


Whenever I try to run this code it generates following error:

An uncaught Exception was encountered
Type: Error
Message: Cannot use object of type stdClass as array


It seems error is at the following line:
$this->data['product_category'][$item_key]['factory'][$fact_key] = $factory->factory_name;


Values of $this->data['product_category']:
[{"id":"1","category_name":"PVC_BOND","category_description":"PVC SHOES ","status":"Active"},{"id":"2","category_name":"PVC_SLIPPER","category_description":"air blowing slippers ","status":"Active"},{"id":"3","category_name":"GUMBOOT","category_description":"PVC GUMBOOT ","status":"Active"},{"id":"4","category_name":"EVA","category_description":"MEN  & WOMEN EVA SLIPPERS ","status":"Active"},{"id":"5","category_name":"PU","category_description":"PU SLIPPERS ","status":"Active"},{"id":"6","category_name":"PVC_BELLY","category_description":"PVC LADIES BELLY ","status":"Active"},{"id":"7","category_name":"GAUTAM MADAAN","category_description":"","status":"Active"},{"id":"8","category_name":"TEST_TR-78.55","category_description":"test category","status":"Active"}]

And Value of $factory_list:

[{"factory_id":"1","factory_name":"RAMANAND ENTERPRISES  PLOT NO 13 14"},{"factory_id":"20","factory_name":"TEST58"}]

ERROR - 2020-08-21 16:09:06 --> Severity: error --> Exception: Cannot use object of type stdClass as array C:\Apache24\htdocs\inv\application\controllers\masters\Variety.php 72


What am I doing wrong here.

Can you help me please.

Regards

PHP Code:
$data = array();

$product_category json_decode('[{"id":"1","category_name":"PVC_BOND","category_description":"PVC SHOES ","status":"Active"},
{"id":"2","category_name":"PVC_SLIPPER","category_description":"air blowing slippers ","status":"Active"},
{"id":"3","category_name":"GUMBOOT","category_description":"PVC GUMBOOT ","status":"Active"},
{"id":"4","category_name":"EVA","category_description":"MEN  & WOMEN EVA SLIPPERS ","status":"Active"},
{"id":"5","category_name":"PU","category_description":"PU SLIPPERS ","status":"Active"},
{"id":"6","category_name":"PVC_BELLY","category_description":"PVC LADIES BELLY ","status":"Active"},
{"id":"7","category_name":"GAUTAM MADAAN","category_description":"","status":"Active"},
{"id":"8","category_name":"TEST_TR-78.55","category_description":"test category","status":"Active"}]'
);

$factory_list =  json_decode('[{"factory_id":"1","factory_name":"RAMANAND ENTERPRISES  PLOT NO 13 14"},{"factory_id":"20","factory_name":"TEST58"}]');

        foreach ($product_category as $item_key => $item) {

            foreach($factory_list as $fact_key=>$factory){

                $data['product_category'][$item_key]['factory'][$fact_key] = $factory->factory_name;
            }

        }


print_r($data); 
Reply
#3

Your using an object array when it is expecting an associated array.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB