How can I reuse data passed from controller in a view ?
controller's function
public function display()
{
$data=ReadIntoDate();
$this->load->view('display',$data);
}
I would like to use $data in display.php in view. Thank you
You should pass an associative array.
So if you do it like this:
public function display()
{
$data['something'] = ReadIntoDate();
$this->load->view('display', $data);
}
You can call the data in the view like:
<h1><?php echo $something; ?></h1>
Hope it helped.
$data must be an associative array.
Controllter:
$data['blah'] = 'something';
view:
echo $blah;
Related
I'm using a function to generate some HTML in the view. I can not access the data that send form controller within that function.Following is the simplified code.
Controller
$data["var"] = "something";
$this->load->view("the_view",$data);
View
function some(){
global $var;
echo $var;
}
some(); //not working
echo $var; //working
I can move this function to the controller. Generate HTML in controller and send the generated HTML to view. But I like to keep HTML stuf in the view. How can I do this ?
You can rewrite your function like this
function some($var){
echo $var;
}
some($var);
Hope it will work
You have created an array named as data and var stored into that, so you need to this
function some($data){
echo $data["var"];
}
some($data);
And in case your data array is global, then
function some(){
global $data
echo $data["var"];
}
some();
<?php class pac{
public $q=5;
public function yo($q){
$v=$q+2;
print $v;
}
}
$a=new pac;
$a->yo($q);
?>
Hi guys ,i am beginner,sorry for stupid questions!
So the question is xD : how to use $q?? I need to put reference on it ??
You use $q but $q isn't defined. If you want to use the object variable $q you can use $a->q.
<?php
class pac{
public $q=5;
public function yo($q){
$v=$q+2;
print $v;
}
}
$a=new pac;
$a->yo($a->q);
?>
Another option is to use the object variable in your method instead of pass it as parameter:
<?php
class pac{
public $q=5;
public function yo(){
$v=$this->q+2;
print $v;
}
}
$a=new pac;
$a->yo();
?>
This is sending data to view in CodeIgniter:
public function index()
{
$data['header'] = "Home";
$this->load->view('admin_frontpage', $data);
}
And this is not:
public function index()
{
$this->data['header'] = "Home";
$this->load->view('admin_frontpage', $this->data);
}
Why?
In my view file I try to echo:
<?php echo $header; ?>
But only when using $data it is echoed. When using $this->data in controller, nothing is echoed out.
What am I doing wrong?
Most likely is $this->data not defined.
You need to define a data member in your class
private $data;
and initialize it with
$this->data = array();
or all at once
private $data = array();
See Classes and Objects and Properties for details.
$this->data is not defined in your controller. Remember the current page has no recollection of the name of the $data array. Every variable is instantiated as a seperate variable, just like when you are passing on the data array to 'admin_frontpage', the array is stripped out and every element of the array is instantiated as a variable (i.e. $this->data['header'] becomes $header)
I have a question. Ive made some mvc'ish structure for my website.
I want to be able to use my variables inside my view without assigning them.
Let me explain by an example, here's a small part of my controller :
class members extends controller{
public function _construct()
{
parent::__construct();
}
public function index()
{
$test = 'test variable';
$data['test2'] = 'test variable 2';
view::setTemplate('header');
view::setTemplate('homepage', $data);
view::setTemplate('footer');
}
}
in the setTemplate() function of my view class, I use extract($data) and then I include the view file. This way I can echo $test2 and get 'test variable 2' as output, as expected.
However, is there a way to make the view 'remember' the first $test variable without having to add it to the setTemplate function ?
DISCLAIMER: THIS CODE HAS NOT BEEN TESTED
You can use variable variables to define the variables within the setTemplate method and include the view directly from there.
<?php
function setTemplate($view, $data) {
if(is_array($data)){
foreach($data as $var => $val){
$$var = $val;
}
}
include('views/'.$view.'.php');
}
?>
as per title above, when I trigger the controller with a hyperlink, it does runs the controller function but I couldn't get the value of SESSION after redirect from controller. The code is as follows...
function langpref($lang){
$this->load->helper('url');
redirect(ABSOLUTE_PATH, 'location');
$this->session->set_userdata('cur_lang', 'xxx');
}
*Note: ABSOLUTE_PATH is a constant of the hyperlink, and I already load the SESSION library in the autoload file.
In my view file, I written the code as follows...
<?php echo $this->session->userdata('cur_lang');?>
and it doesn't print out the SESSION value.
First Approach: You cannot access session variables like that
<?php $ci =& get_instance(); ?>
<div>
<?php echo $ci->session->userdata('cur_lang') ?>
</div>
Second Approach: Another way you can do this is pass the session data to the view
On your controller
$data['userdata'] = $this->session->userdata;
$this->load->view('your/view', $data);
On your view
echo $userdata['cur_lang'];
Shouldn't that be:
function langpref($lang){
$this->load->helper('url');
$this->session->set_userdata('cur_lang', 'xxx');
redirect(ABSOLUTE_PATH, 'location');
}
And in your view:
<?php echo $this->session->userdata("cur_lang"); ?>
session is global variable. if you want to use it in class or function. you need to access it by passing session variable as function argument. or you need to use global command. such as;
class XXX{
public function processSession($_SESSION){
return $_SESSION['xx'];
}
}
or you could use global directive
class XXX{
public function processSession(){
global $_SESSION;
return $_SESSION['xx'];
}
}
other way is starting session in function
class XXX{
public function processSession(){
session_start();
$_SESSION['xx'] = 'aaaa';
return $_SESSION['xx'];
}
}
other way, you cannot access session variable in function or class function