简体   繁体   中英

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.

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM