0

In my model there are 2 functions to get data from database. Both return data as arrays of objects.One function is given below.

public function get_camaramens($data,$evdata)
{
$this->db->select('emp_position.employee_id');
$this->db->from('emp_position');
$this->db->where('emp_position.position','c');
$this->db->where('emp_position.employee_id NOT IN ('.implode(",",$data).')');
$query=$this->db->get('',$evdata);
return $query->result();
}

In my controller I accept this result as follows.

$sdata['camaramen_list']    = $this->emp_position_model>get_camaramens($data,$evdata['no_of_cams']);

The other function in the model is

public function get_camara_assistants($data,$sdata,$evdata)
{
$cdata = array();
foreach($sdata['camaramen_list'] as $row) {
$cdata[] = $row->employee_id;
}

$this->db->select('emp_position.employee_id');
$this->db->from('emp_position');
$this->db->where('emp_position.position','ca');
$this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$data).')');
$this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$cdata).')');
$query=$this->db->get('',$evdata);
return $query->result();
}

In my controller I want to add the result of the above function to the same array of objects $sdata. But if I put same name as follows it replace the previous array.

$sdata['camaramen_list']    = $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);

Can anyone tell me correct way please.

4 Answers 4

1

Try to understand the basic concept:

$sdata['one'] = 'some value';

and I want to add one more value in that array and I do it like:

$sdata['one'] = 'Some other value';   

// this value overrides the old value, because you are adding the values on same index, this will override already existing value.

So do it like:

$sdata['old']['one'] = 'Some other value'; 

add new value like:

$sdata['new']['one'] = 'Some other value'; 

In this case there are two diff index old, one. So no override is done here.

or save it on some diff index.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use the same variable, it could be override. There are two ways you can do that.

  1. array_merge at Controller

    You can merge two arrays by array_merge.

    $result1 = $this->emp_position_model>get_camaramens($data,$evdata['no_of_cams']);
    $result2 = $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);
    
    $combined_result = array_merge($result1, $result2);
    
  2. UNION select at Model

    $this->db->select('emp_position.employee_id');
    $this->db->from('emp_position');
    $this->db->where('emp_position.position','c');
    $this->db->where('emp_position.employee_id NOT IN ('.implode(",",$data).')');
    
    $compiled_1 = $this->db->get_compiled_select();
    
    
    $this->db->select('emp_position.employee_id');
    $this->db->from('emp_position');
    $this->db->where('emp_position.position','ca');
    $this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$data).')');
    $this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$cdata).')');
    
    $compiled_2 = $this->db->get_compiled_select();
    
    
    $query = $this->db->query('( ' .$compiled_2 .' ) UNION ALL ( '. $compiled_1 .' );' );
    

You can use $sdata['camaramen_list']['one'], $sdata['camaramen_list']['two'] if you wanna create new index. Otherwise, use my way instead.

Hope it would be help.

1 Comment

Thank u very much Benyi. I tried the first one. It is succeeded.
0

If both results are arrays, you can use array_merge to merge one or more arrays into a new one.

$sdata['camaramen_list'] = array_merge(
    $this->emp_position_model->get_camaramens($data,$evdata['no_of_cams']),
    $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);
);

var_dump($sdata['camaramen_list']);//the new array

Comments

0

Try this

$data['data1']= $this->emp_position_model->get_camaramens($data,$evdata['no_of_cams']);
$data2['data2']= $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);
$sdata['camaramen_list']=array_merge($data,$data2);

$this->load->view('your_view_page',$sdata['camaramen_list'] );

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.