Can I set a variable in view file?
for exemple:
I have a controller: welcome.php
Its load:
$this->load->view('header');
$this->load->view('main');
$this->load->view('footer');
I need set a variable on file main.php and get on footer.php
Its possible?
You can pass a variable from one view to another just like you would from a controller to a view. You just have to load the view file that sets that variable before the view that uses that variable:
$this->load->view('main'); //load before
$this->load->view('footer'); //load after
Inside of main.php do $this->load->vars(array('your_variable'=>'it's value')); and you will be able to call it in the footer like you would any other variable. The only requirement is that main.php be loaded before footer.php.
It is not possible to set a variable in a view file and access it from another. There is no reason you should be setting variables in your view files anyway. The controller should be handling all of your application logic so you should be setting the variable there. I would recommend reading through the user guide or looking at some articles to get a better grasp of MVC principles.
To access the same variable in multiple views, pass it to each view you load.
// Set your variable
$data['variable'] = 'value';
// Pass variable to multiple views
$this->load->view('main', $data);
$this->load->view('footer', $data);
You can pass both array or object to your view, for example:
$data = new StdClass;
$data->title = "The Title";
$data->content = "The Content";
$this->load->view('main', $data);
or
$data = new SomeClass;
$this->load->view('main', $data);
Also, in each view you are able to pass a different data:
$data1 = array("key" => "val");
$data2 = $this->some_class->some_method($params);
$data3 = $this->another_class->another_method($params);
$this->load->view('navigation', $data1);
$this->load->view('main', $data2);
$this->load->view('footer', $data3);
Yes you can, to pass variable from controller you can type the variable data after the name of view, this is the example :
$data["vehicle"] = $vehicle;
$this->load->view('header',$data);
then in your view just call the name of variable :
<td>
<?php echo $vehicle ?>
</td>
Hope it can help
Related
I want to know what is the function of loadView in Laravel.
Because I have a hard time viewing my view_path.
public function loadView($view_path, $params) {
$view = View::make($view_path, $params);
$contents = $view->render();
return $contents;
}
The view path is the path to the view starting from the folder resources/views/ and without the .blade.php extension.
For example if you want to load the view /var/www/html/myProject/resources/views/front/welcome.blade.php then the view path is "front/welcome".
You should probably use "view($view_path, $params)" to load your views at the end of your controllers.
As in :
return view("front/welcome", $params);
Where did you find this loadView function ?
Is there any other best way to use session_data in website.
How i set session in my project:
$sess_array = array('id' => $row->user_id,'name'=>$row->user_name,'email'=>$row->email,'condition'=>'','balance'=>$row->balance,'did_alloted'=>$row->did_alloted,'create_date'=>$row->create_date);
$this->session->set_userdata('logged_in', $sess_array);
when it comes to controller:
$data['id'] = $session_data['id'];
$data['name'] = $session_data['name'];
$data['email'] = $session_data['email'];
$data['balance']=$session_data['balance'];
$data['did_alloted']=$session_data['did_alloted'];
$data['create_date']=$session_data['create_date'];
$this->load->view('san-reception', $data);
and in my view. I use <?php echo $name ?> to get session_data.
So is there any method by which i can directly access session_data in view without including as $data.
$this->session->userdata('logged_in'); will be available directly in views and hence you just need to assigned this to a variable and then use that as array like bellow.
$userdata=$this->session->userdata('logged_in');
now variable $userdata contain all array field that you set in controller and hence you can use it as $userdata['id'], $userdata['name'] etc
public function __construct()
{
parent::__construct();
$data['session_data']=$this->session->user_data('logged_in');
}
public function some_function(){
$data['dummy']="";
$this->load->view('someview3',$data)
}
public function some_function1(){
$data['dummy']="";
$this->load->view('someview1',$data)
}
public function some_function2(){
$data['dummy']="";
$this->load->view('someview2',$data)
}
If you assign that into the constructor then it will call by automatically whenever a function calls in the controller.
you can view it in you view page by,
print_r($session_data);
Im having a few problems with the form_dropdown function in CodeIgniter .... My application is in 2 parts, a user goes in, enters a form and submits it .... once its submitted a admin can go in and edit that persons form and then save it to database.
So, to display the dropdown in the initial form, im using the following ( all the options in the dropdown are coming from the database )
Model:
function get_salaries_dropdown()
{
$this->db->from($this->table_name);
$this->db->order_by('id');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0){
$return[''] = 'please select';
foreach($result->result_array() as $row){
$return[$row['id']] = $row['salaryrange'];
}
}
return $return;
}
Then in the Controller:
$data['salaries'] = $this->salary_expectation->get_salaries_dropdown();
Then finally the View:
<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries)); ?>
That bit works perfect in displaying the dropdown filled with values for the user to select.
So, when the user selects a value, then hits save, its saved to the database.
On the Edit page which the admin see's, im using the same code to display the dropdown filled with options, but how do i get it to automatically choose the one thats been selected by the user in the initial stage?
Cheers,
According to Codeigniter documentation
The first parameter will contain the
name of the field, the second
parameter will contain an associative
array of options, and the third
parameter will contain the value you
wish to be selected. You can also pass
an array of multiple items through the
third parameter, and CodeIgniter will
create a multiple select for you.
Your admin controller should have something like
$data['selected'] = $this->salary_expectation->get_salary_selected();
According to this, the admin view should be like this
<?php echo form_dropdown('salaries', $salaries, $selected_value); ?>
one nasty solution to select the <option> element of <select> generated by form_dropdown() function of the form_helper is using the post input sended.
I made this because any solutions I found doesn't display the value that the user select in the form neither set_selected nor set_vaule.
Well, in my controller I have:
$countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME
$data['countries']=$countries;
In my view:
$selected_country = $this->input->post('country');
echo form_dropdown('country',$countries,$selected_country);
And works fine !!! :)
The form_dropdown function has a third parameter for the selected option. Use it like this:
<?php echo form_dropdown('piece_type',
array(
'type1' => 'Firts type',
'type2' => 'Second option'
$selected_value,
'id = "piece_type"') ?>
I had the same problem but i have overcome on this problem using code igniter syntex.
Here is the solution.
Fisrt step
Before the loop initialize two arrays
$options = array();
$select = array();
Then in the loop write this instruction
foreach($result->result_array() as $row)
{
/////////Your Condition ////////////
if($row['id'] == $myarray['mycolumn'])
{
$options [$row['id']] = $row['salaryrange'];
$select = $row['id'] ;
}else{
$options [$row['id']] = $row['salaryrange'];
}
}
Now
echo form_dropdown('dropdown_name' , $options , $select);
It is working ok
For update case, you have pass corresponding value to view, if passing variable is like $ind_post(from controller ) then write this code like:
<?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,''); ?>
I have a model, view and controller not interacting correctly, and I do not know where the error lies.
First, the controller. According to the Code Igniter documentation, I'm passing variables correctly here.
function view() {
$html_head = array( 'title' => 'Estimate Management' );
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('html_head', $html_head);
$this->load->view('estimates/view', $estimates);
$this->load->view('html_foot');
}
The model (short and sweet):
function get_estimates() {
$query = $this->db->get('estimates')->result();
return $query;
}
And finally the view, just to print the data for initial development purposes:
<? print_r($estimates); ?>
Now it's undefined when I navigate to this page. However, I know that $query is defined, because it works when I run the model code directly in the view.
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('estimates/view', $estimates);
You're loading the return of $this->Estimatemodel->get_estimates() as the array of view variables. In other words, all the children of $estimates (assuming it can be treated as an array) are available in your view. But not the parent element.
The key here is when loading a view the second parameter needs to be an array of values, not just a single value.
$this->load->view('estimates/view', array('estimates' => $estimates));
That should get the result you're looking for, in fact, you're already doing that for the html header view. Even though that view only has one variable, it's passed as the single element of an array:
$html_head = array( 'title' => 'Estimate Management' );
$this->load->view('html_head', $html_head);
The documentation shows that the object you pass to the view must be an associative array.
$data = array(
'estimates' => $estimates
);
$this->load->view('estimates/view', $data);
Docs here
I have a controller with an index function as follows:
function index()
{
$this->load->model('products_model');
$data['product'] = $this->products_model->get(3); // 3 = product id
$data['product_no'] = 3;
$data['main_content'] = 'product_view';
//print_r($data['products']);
$this->load->view('includes/template', $data);
}
This is the get function in the products_model file
function get($id)
{
$results = $this->db->get_where('products', array('id' => $id))->result();
//get the first item
$result = $results[0];
return $result;
}
The products table contains fields such as name, price etc. Please can you tell me how to output variables from $data['product'] after it is passed into the view? I have tried so many things but nothing is working, even though the print_r (commented out) shows the data - it is not being passed into the view. I thought it may have been because the view calls a template file which references the main_content variable:
Template file contents:
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer'); ?>
but i tried creating a flat view file and still couldn't access the variables.
Many thanks,
your $data array is "ripped" into single variables within the view.
print $product;
print $product_no;
print $main_content;
Handling headers & footers this way sucks and get's unmanageable pretty quickly.
Try using my Template library, your data will be passed through to the product_view no problem.