Welcome Guest, Not a member yet? Register   Sign In
Beginner question
#1

[eluser]Bl4ckB1rD[/eluser]
Is it possible to send multiple variables to view file?

Example:
Code:
$data = 'something';
$otherdata = 'something else';
$this->load->view('myview',$data,$otherdata);

I'm aware i can use arrays but that wont solve my problem.

And another question...

Can i call certain function of controller within view file?

Example:

in controller class:

Code:
function myfunction {
return "somedata";
}

in view file:
Code:
<br>My function outputs:
&lt;?=$this->myfunction?&gt;


this syntax doesnt work, even if i specify controller name for example: $this->Mycontroller->myfunction or Mycontroller->myfunction

Any ideas?
#2

[eluser]Ben Edmunds[/eluser]
Please explain how an array will not work?

No, you do NOT want to call a method from the view. The controller does all the processing and gives the view all the data it needs. The view should not contain any logic.


Make sure you read the userguide and maybe read a little bit about the MVC design pattern.


If you have anymore questions after that we'll be glad to help.


And welcome to the CodeIgniter community!
#3

[eluser]The Casual Bot[/eluser]
put them into an array

ie

Code:
$data = array();
$data['data'];
$data['otherdata'];

then load you view file passing in the $data array

you can then use them in your view

eg
Code:
&lt;?=$data?&gt;
&lt;?=$otherdata?&gt;
#4

[eluser]Colin Williams[/eluser]
Use the humble load->vars() method

Code:
$this->load->vars('data', 'something');
$this->load->vars('otherdata', 'something else');
$this->load->view('myview');
#5

[eluser]Colin Williams[/eluser]
Quote:Can i call certain function of controller within view file?

Send a controller reference to the view

Code:
$this->load->vars('data', 'something');
$this->load->vars('otherdata', 'something else');

// Provide reference to view
$ci =& $this;
$this->load->vars('ci', $ci);

$this->load->view('myview');
#6

[eluser]Kyle Wiebers[/eluser]
[quote author="Bl4ckB1rD" date="1256773755"]Is it possible to send multiple variables to view file?

Example:
Code:
And another question...

Can i call certain function of controller within view file?

Any ideas?[/quote]

The easiest way to do this would be to use an AJAX request for the url.  

For example let's say you are trying to load controller/function from your current view.  Setup an AJAX request with JavaScript (or jQuery's AJAX library) and load the data dynamically.

If you are trying to load other view's into a view.  Consider storing the view into a variable and pass it to the view through the variable.  
[code]
$data['view'] = $this->load->view('myfile', '', true);
$this->load->view('viewa',$data);
#7

[eluser]Colin Williams[/eluser]
Kyle, AJAX is an awful idea for a variety of reasons. All you need is a reference to the controller object.
#8

[eluser]Bl4ckB1rD[/eluser]
Thank you very much, in the end i used multidimensional arrays, but this quick hack with passing controller reference to a view... it's more than great and useful! Thanks to everyone who took it's time to answer my question.




Theme © iAndrew 2016 - Forum software by © MyBB