0

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.

1

2 Answers 2

1

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

1 Comment

i have read detailed guide about how to pass value from controller to view in codigniter cloudways.com/blog/how-to-pass-data-in-codeigniter
1
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.

2 Comments

that's good and I thought of that, however my 2 functions return an array of many variables. Is there a better way to do it? I edited my question
Passing back an array with many variables is fine - very common practice. No better way comes to mind. As an alternative you can use class objects. I've revised the answer to illustrate using classes.

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.