My project has several views which draw values out of the session->userdata array.
the whole project works flawlessly on my development machine, but when I copied the project up to a server, I find that $this->session->userdata is null.
This is the code which inserts some values into the session->userdata array:
It is called in the Controller.
$sessionArray = array(
'web_id' => $id,
'paperId' => $paperId,
'prelimId' => $prelimId,
'intakeId' => $intakeId,
'finalId' => $finalId,
);
$this->session->set_userdata($sessionArray);
In the view file I call
var_dump($this->session->userdata);
The value returned is null. As I mentioned before, the array is NOT null on my development computer (running WAMP). Does anyone know why?
In order to retrieve all session data as an ASSOC array, you could use the all_userdata() method. (Take a look at CodeIgniter user guide),
You can write that within your Controller and send the returned value to the view, as follows:
$data['userdata'] = $this->session->all_userdata();
$this->load->view('your/view.php', $data); // use $userdata in the view
Note: Using the session library directly inside the views may cause unexpected problems. Try managing your sessions in the Controller instead of the View.
Sorry for posting here in answer block first of all.......... Where you are assigning those values to $sessionArray. What i mean is in model or controller or in view file.
Are you calling var_dump($this->session->userdata) in view file.
Why don't you call that in controller and pass that as an array to view file via controller.
try like this in controller i will get user details in $login array from database for suppose when i submit username and password.
I will send those details to session array like this in model file
$this->session->set_userdata('logged_in_user', $login);
after that in controller if i want use user name from session array i will use like below.
$logged_in = $this->session->userdata("logged_in_user");
$model=array();
$model['logged_in_user']=$logged_in[0];
Once after i assigned to $model array i like to use that in view file so i will pass that $model array to view file from controller like this
$this->load->view('view_file_path',$model);
You can use that array in view file how ever you want from passed array.
Related
I am attempting to render and set variables to a view that does not belong to any controller. Here is an example of my folder structure:
app/
-->View/
-->ExternalReportViews/ (There is no ExternalReportViews Controller)
-->example_view.ctp
I am trying to pass a variable called $orders to the view, I have verified that this variable exists and contains data by debugging from the controller.
I have succesfully rendered the view using any of the following methods:
// Method 1
$this->render('/ExternalReportViews/example_view');
$this->set('orders', $orders);
// Method 2
$view = new View($this, false);
$view->viewPath = 'ExternalReportViews';
$view->render('example_view');
$view->set('orders', $orders);
// Method 3
$this->viewPath = 'ExternalReportViews';
$this->render('example_view');
$this->set('orders', $orders);
It seems that regardless of whether the set method is placed before or after the render in any of the above methods, the $orders variable is not passed to the rendered view.
When attempting to debug($orders) in the view, I see that the variable is undefined.
I could obviously bypass this problem by temporarily storing $orders in $this->Session, but that seems a bit messy.
The reason for all of this is that I will have a number of views that will be created on a per-report basis, and I would rather have them in a separate (sub)folder(s) for cleaner file management.
As it would happen, I just needed to change the variable name. I'm either hitting on a reserved keyword, or I've previously defined the variable in my beforeFilter method. Not sure which yet, but credit goes to scrowler for pointing that one out.
I have been trying to pass an array that is generated in view to controller.
Say, $abc=array(); How do I sent this array to controller, then process it and sent this array to another view?
Is it even feasible? Not getting any answer after googling! Please help.
I see no point to generate array from inside the view.
It depends how your framework passes the data between the layers.
For example the controller do pass the data to the View object.
I.e.
public function someMethodInController() {
$this->view->varName['arr_key'] = 'something';
}
But talking about view, it might not have a certain controller already instantiated (usually it's against the MVC paradigm)
So I see two major ways:
Instantiate a controller (I would not prefer this)
$controller = MyController();
$controller->abc = $abc;
Or use sessions. This way to transmit data is universal inbetween your domain, and does not care if you are using a certain pattern, or how do you transmit between layers.
$_SESSION['abc'] = $abc;
Once assigned, you can use it in the same session from each of your files.
I have just started learning Code Igniter .
I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?
Any help would be appreciated .
Thanks in Advance :)
It will depend on the circumstances. If you want to retain the data for some time, then session data would be the way to go. However, if you only need to use it once, flash data might be more appropriate.
First step would be to initialise the session library:
$this->load->library('session');
Then store the information in flash data:
$this->session->set_flashdata('item', $myVar);
Finally, in the second controller, fetch the data:
$myVar = $this->session->flashdata('item');
Obviously this would mean you'd have to either initialise the session library again from the second controller, or create your own base controller that loads the session library and have both of your controllers inherit from that one.
I think in codeigniter you can't pass variable, between two different controller. One obvious mechanism is to use session data.
Ok, here is something about MVC most will readily quote:
A Controller is for taking input, a model is for your logic, and, a view is for displaying.
Now, strictly speaking you shouldn't want to send data from a controller to another. I can't think of any cases where that is required.
But, if it is absolutely needed, then you could simply use redirect to just redirect to the other controller.
Something like:
// some first_cont.php code here
redirect('/second_cont/valuereciever/value1')
// some second_cont.php code here
public function valureciever($value){
echo $value; // will output value1
}
In Codeigniter there are many way to pass the value from one controller to other.
You can use codeigniter Session to pass the data from one controller to another controller.
For that you have to first include the library for session
$this->load->library('session');
Then You can set the flash data value using variable name.
// Set flash data
$this->session->set_flashdata('variable_name', 'Value');
Them you can get the value where you want by using the codeigniter session flashdata
// Get flash data
$this->session->flashdata('variable_name');
Second Option codeigniter allow you to redirect the url from controll with controller name, method name and value and then you can get the value in another controller.
// Passing the value
redirect('/another_controller_name/method_name/variable');
Then you can get the value in another controller
public function method_name($variable)
{
echo $variable;
}
That all....
If you are using session in the first controller then dont unset that session in first controller, instead store the value which you want in the other controller like,
$sess_array = array('value_name1' => 'value1', 'value_name2' => 'value2');
$this->session->set_userdata('session_name', $sess_array);
then reload this session in the other controller as
$session_data= $this->session->userdata('session_name');
$any_var_name = $session_data['value1'];
$any_var_name = $session_data['value2'];
this is how you can pass values from one controller to another....
Stick to sessions where you can. But there's an alternative (for Codeigniter3) that I do not highly recommend. You can also pass the data through the url. You use the url helper and the url segment method in the receiving controller.
sending controller method
redirect("controller2/method/datastring", 'refresh');
receiving controller method
$this->load->helper('url');
$data = $this->uri->segment(3);
This should work for the default url structure. For a url: website.com/controller/method/data
To get controller $this->uri->segment(1)
To get method $this->uri->segment(2)
The limitation of this technique is you can only send strings that are allowed in the url so you cannot use special characters (eg. %#$)
When I call a controller and it calls the model, model returns information from my database assigned to something in the controller.
But how does it "send" it to the view for rendering? How for example, when I send $data array to my_view.php. how does it get to that page so that, I am guessing, I can do things like use extract to get my individual variables.
I'm really asking at the php level, how would you send that data (so I can learn). How does that view know what I sent it?
Thanks.
You have to "send" that $data array to the view as the second parameter when you load it.
$data['user'] = array(
'name' => 'Tom Jones',
'gender' => 'male'
);
$this->load->view('blogview', $data);
Then, the contents of the array are accessed within the view by their corresponding key values
<?php echo $user['name']; ?>
Checkout out the docs for more details: http://codeigniter.com/user_guide/general/views.html
The general pattern of all php views is this:
function render_view($__filename, $__data) {
extract($__data);
include $__filename;
}
This is basically how CodeIgniter does it, but it uses a loader to find the view filename and includes output buffering options.
I am trying to pass a variable from a view (of mobile model) to a different controller (of inventory model), using the chtml:button with this code
echo CHtml::button(
'Sell It',
array('submit' => array('inventory/create', array('id'=>$data->id)))
);
Now how do I access the $id variable in the Inventory controller, so that I can prepopulate the create view with details corresponding to the passed 'id' variable of the mobile model.
In your inventory/create controller action do checking before getting the id like this :-
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$this->render('create',array('model'=>$inventory, 'id'=>$id));
}
else{
$this->render('create',array('model'=>$inventory);
}
If you are trying to come up an update/edit form with the values prefilled based on the Id passed then you should have to go through the CRUD options available within YII.. This is much better way to handle record updation and its easy too . See this topic for furhter info..
http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
In your inventory/create controller action do a test for $_GET['id'] something like:
$id = (#$_GET['id']) ? : DEFAULT_VALUE;
$this->render('create',array('model'=>$inventory, 'id'=>$id));
and then you pass the data through to the view by passing an array of variables you want to make available.
(you would want to filter the input better, this is just a sample -- using filter_input or some other method and define a default value and/or some test for it being null/invalid)
in your controller you can get the variable by giving an argument to your controller method like this:
public function actionCreate($id){
$id = isset($id)?$id:NULL; // Or whatever, you can access it like this.
}
You don't have to use $_GET, and yii has already done some security checks on the value.