Andhika R.K.
Andhika R.K.

Reputation: 436

Passing Data Array from Controller to View Codeigniter

I'm trying to simplify my menu, that i will pass into my view, the problem is menu and submenu is not active. This is my controller:

parent::__construct();
    $this->menu = array(
      'menu'    => 'definition',
      'submenu' => 'workplace'
    );

And i have this in my controller method:

$data = array(
    $this->menu, // menu is right here
    'list_workplace_type' => get_wp_type()->result_array()
  );
var_dump($data);die;
$this->load->view('wp', $data);

And the result is like below:

array (size=2)
  0 => 
    array (size=2)
      'menu' => 'definition'
      'submenu' => 'workplace'
  'list_workplace_type' => 
    array (size=2)
      0 => 
        array (size=1)
          'szWorkplaceTypeName' => 'Kantor Pusat'
      1 => 
        array (size=1)
          'szWorkplaceTypeName' => 'Kantor Cabang'

What I expected is more like this:

array (size=2)
  'menu' => 'definition'
  'submenu' => 'workplace'
  'list_workplace_type' => 
    array (size=2)
      0 => 
        array (size=1)
          'workplaceTypeName' => 'Kantor Pusat'
      1 => 
        array (size=1)
          'workplaceTypeName' => 'Kantor Cabang'

I used array_push() but still doesn't work properly.

Upvotes: 0

Views: 88

Answers (2)

TigerTiger
TigerTiger

Reputation: 10806

What I understand is that you're trying to add 'list_workplace_type' as another key in the Menu array so you need to change this

$data = array(
    $this->menu, // menu is right here
    'list_workplace_type' => get_wp_type()->result_array()
  );

to

$data = $this->menu['list_workplace_type'] = get_wp_type()->result_array();

and then you can pass to view

$this->load->view('wp', $data);

Upvotes: 1

TimBrownlaw
TimBrownlaw

Reputation: 5507

I'll have to assume that get_wp_type() is a helper function.

You have used $result->result_array() which will give you the observed result.

What you need to use is $result->row_array() which will give you the result you are expecting.

So

$data = array(
    $this->menu, // menu is right here
    'list_workplace_type' => get_wp_type()->result_array()
  );

becomes

$data = array(
    $this->menu, // menu is right here
    'list_workplace_type' => get_wp_type()->row_array()
  );

Go and look them up in the codeigniter user guide. It's a good habit to get into.

Upvotes: 0

Related Questions