remyremy
remyremy

Reputation: 3758

passing variable from controller to view with CodeIgniter

I'm trying to structure my controllers in several small functions and I'm struggling with the following code:

building controller:

public function index(){
   $data['title'] = 'my title part 1';
   $data['title'] .= 'my title part 2';
   $data = $this->function1();
   $data = $this->function2();
   $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;
}

buildingView

echo $title;
echo $block1;
echo $block2;
echo $end;

The problem is that the $data variable from function2 overwrites the one from from function1. How can I pass the data generated from within Index and both functions?

I've tried the code below but it doesn't work either:

$data[] = $this->function1();
$data[] = $this->function2();

EDIT: I edited functions 1 & 2 because they actually return an array of several variables.

Upvotes: 0

Views: 4131

Answers (2)

DFriend
DFriend

Reputation: 8964

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

The results of your functions will be accessed in the view with the vars $block1 and $block2.

These definitions work better for demonstration purposes

public function function1()
{
    return 'test1';
}

public function function2()
{
    return 'test2';
}

With the helper function defined as in your question's code

public function function1(){
    $data['block1'] = 'test1';
    return $data;
}

And assigning the return to a var that is then passed to the view

$data['func1'] = $this->function1();
$this->load->view('templates/default',$data);     

In the view do this to show the result

echo $func1['block1'];

It is very common and perfectly acceptable to pass arrays to views.

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

Passed to the view

$data['func2'] = $this->function2();
$this->load->view('templates/default',$data); 

Accessed in the view. This time using an iterator to display each item in the array.

foreach($func2 as $item)
{
   echo $item."<br>";
}

Or get each item one at a time

echo $func2['block1']."<br>";
echo $func2['block2']."<br>";
echo $func2['block3']."<br>";
echo $func2['block4']."<br>";

You can pass object instances to a view too. This version of function2() returns an object instance.

  public function function2()
  {
    $data = new stdClass();
    $data->block1 = 'test2';
    $data->block2 = 'test3';
    $data->block3 = 'test4';
    $data->block4 = 'test5';
    return $data;
  }

Assign the function return to an array that's passed to a view

$data['obj2'] = $this->function2(); 
$this->load->view('templates/default',$data);    

Using it in the view

echo $obj2->block1;
echo $obj2->block2;
...

Iterators (foreach) works on object instances too.

Upvotes: 1

Vinie
Vinie

Reputation: 2993

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

Upvotes: 1

Related Questions