[eluser]smilie[/eluser]
Hi,
Goal: pass data to the view, so it can be used in view;
Code:
Code: $test = array('text' => 'some text');
$tmp_data['main_content_view'] = $this->load-view('content/profile',$test,TRUE);
In content/profile:
But I am getting error:
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined variable: test
Filename: content/profile.php
Line Number: 8
For some reason, it seems as my variable is not being received by the view?
Tho' I am not sure if this is some config / setting in CI or am I doing something wrong? :-)
Thanks!
Smilie
[eluser]esset[/eluser]
Could you try:
$test = array();
$test['text'] = "some text";
$this->load-view('content/profile', $test);
Should be exactly the same. Just curious.
[eluser]smilie[/eluser]
Hi esset,
New code:
Code: $test = array();
$test['text'] = "some tekst";
$tmp_data['main_content_view'] = $this->load->view('content/profile',$test,TRUE);
But still same error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: test
Filename: content/profile.php
Line Number: 8
Regards,
Smilie
[eluser]smilie[/eluser]
To be on the safe side, here is whole function that should be doing it:
Code: function index()
{
# Get info about the user and pre-fill the form
$test = array();
$test['text'] = "some tekst";
$tmp_data['main_content_view'] = $this->load->view('content/profile',$test,TRUE);
$tmp_data['main_header_view'] = $this->load->view('common/header','',TRUE);
$tmp_data['main_menu_view'] = $this->load->view('common/menu','',TRUE);
$tmp_data['main_submenu_view'] = $this->load->view('submenu/submenu_admin','',TRUE);
$this->load->view('home',$tmp_data);
}
Idea is to get some info from the database, to pass that info to the view (in which is form) to pre-fill the form with values from the DB (update of a profile on the site).
Regards,
Smilie
[eluser]KingSkippus[/eluser]
You're halfway there. You do indeed have to pass an array to the view as you've done in the top snipped of code. The mistake is that within the view, each of the array keys becomes a variable, and each associated value because the contents of the variable.
In short, in the second snipped of code, try this instead:
You should see "some text" appear on the page. The array variable passed to the view isn't visible, only the keys as variables themselves. To be honest, I actually prefer the syntax that follows, as it avoids declaring "dummy" variables and makes it, in my opinion, a little easier to me to see what's going on:
Code: $tmp_data['main_content_view'] = $this->load->view('content/profile', array(
// These are variables that will be available in the view as $text, $another_var, etc.
'text' => 'some text',
'another_var' => 'more stuff',
// ...
), TRUE);
But different strokes for different folks I guess, use whatever is easier for you.
[eluser]esset[/eluser]
Oh yes sorry. You can't access the variable that "transfers" the data.
Please try this in your view:
print_r($text);
or
echo $text
The array is converted for use in the template. So you could do this to achieve what you are asking for:
$test = array();
$test['test'] = array('text' => 'some text');
Then do:
print_r($test);
[eluser]smilie[/eluser]
Thanks KingSkippus & esset, that was exactly what did the trick!
No way I would have found this myself any time soon :-))
Thanks again!
Regards,
Smilie
[eluser]mddd[/eluser]
The compact() function in php is something many people don't know about.
It is used to make an array with simple key names. It can be very handy in this context.
Example:
Code: // some variables
$test = 'Some text';
$products = $this->product_model->get_newest(10);
// now give these variables to the view
$this->load->view('myview', compact('test','products'));
// $test and $products are now available in the view!
// because compact('test','products') gives you : array('test'=>$test, 'products'=>$products)
[eluser]Juan Velandia[/eluser]
Hello guys, I'm a newbie and I want to know what is the use for TRUE in:
$this->load->view('content/profile',$test,TRUE);
Thanks a lot!
[eluser]mddd[/eluser]
If you use TRUE as the 3rd parameter, the contents of the view are not sent to the browser, but returned from the function. That way you can do something with the contents. For instance:
Code: $viewcontent = $this->load->view('myview',$test,true);
// send the view as an email
$this->email->message($viewcontent);
$this->email->send();
(Of course you would set some more settings before sending, but that's the idea).
|