Passing Variable from Controller to View in Codeigniter

passing variable from controller to view with CodeIgniter

You are overriding $data variable. So for your condition use array_merge() and keep it on top in $data variable. Like below

public function index(){
$data1 = $this->function1();
$data2 = $this->function2();
$data = array_merge($data1,$data2);
$data['title'] = 'my title part 1';
$data['title2'] .= 'my title part 2';
$data['end'] .= 'end of this block';
$data['main_content'] = 'buildingView';
$this->load->view('templates/default',$data);
}

public function1(){
$data['block1'] = 'test1';
$data['blockA'] = 'testA';
$data['blockB'] = 'testB';
$data['blockC'] = 'testC';
return $data;
}

public function2(){
$data['block2'] = 'test2';
$data['block3'] = 'test3';
$data['block4'] = 'test4';
$data['block5'] = 'test5';
return $data;
}

Codeigniter sending data from controller to view

I have tried lots of deferent things to fix this. I have no idea why this keeps happening, but finally, I fixed my problem using ajax

    $(document).ready(function(){
$.ajax({
url:'<?php echo base_url('admin/report/index'); ?>',
success:function(data){
$('#same').html(data);
},
error: function(){
alert('Error');
}
});

Codeigniter passing data from controller to view

Ah, the $data array's keys are converted into variables: try var_dump($title); for example.

EDIT: this is done using extract.



Related Topics



Leave a reply



Submit