简体   繁体   English

将数据数组从控制器传递到View Codeigniter

[英]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. 我使用了array_push()但仍然无法正常工作。

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 据我了解,您正在尝试将“ list_workplace_type”添加为Menu数组中的另一个键,因此您需要对此进行更改

$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. 我必须假设get_wp_type()是一个辅助函数。

You have used $result->result_array() which will give you the observed result. 您使用了$ result-> result_array() ,它将为您提供观察到的结果。

What you need to use is $result->row_array() which will give you the result you are expecting. 您需要使用的是$ result-> row_array() ,它将为您提供预期的结果。

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. 在codeigniter用户指南中查找它们。 It's a good habit to get into. 养成良好的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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