2

Controller

I have multiple functions from the model passing data to the view

I was doing this successfully like this:

$var1 = $this->model_name->function_name();
$var2 = $this->model_name->function_name();
$var3 = $this->model_name->function_name();

$data = $var1 + $var2 + $var3; 

$this->load->view('page_name', $data);

Now I am using the above method in other areas which is working flawlessly. However I am getting an error Fatal error: Unsupported operand types What happened? Why was it working an hour ago and suddenly broke without modification to the code..

2
  • Can you spot the line which causes the error based on the message you get? Is it $data = $var1 + $var2 + $var3; Commented Oct 11, 2012 at 22:53
  • Yes that was what the error was suggesting however the problem was in the model. One of the vars wasn't being returning properly which was throwing that exception. I added an If to ensure to check for num_rows and all is working again. Thanks Commented Oct 11, 2012 at 23:03

1 Answer 1

4

You should put multiple variables into array like this:

$var1 = $this->model_name->function_name();
$var2 = $this->model_name->function_name();
$var3 = $this->model_name->function_name();

$data = array(
               'var1' => $var1,
               'var2' => $var2,
               'var3' => $var3,
          );

$this->load->view('page_name', $data);

or as Rick suggested you can use this:

$data = array();
$data['var1'] = $this->model_name->function_name();
$data['var2'] = $this->model_name->function_name();
$data['var3'] = $this->model_name->function_name();

$this->load->view('page_name', $data);

More on array: PHP Array

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

3 Comments

just to expand on this, the "Fatal error: Unsupported operand types" means that one or all of your $var are not arrays but could be variables. In order to do "+", all variables must be arrays. What Otporan is doing is declaring the $var within the array rather than relying on "+".
You might also want to take into mind using variable names that actually mean something ie $data['userdata']=$this->model_name->function_name();
You can also use the compact function for less code. e.g. $this->load->view('view_path', compact('var1', 'var2', 'var3'));

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.