0

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.

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I will prefer this answer. It is most probably correct way to get array you the user wants.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.