How to get data from controller to view? - php

I need to pass data from controller to view. I used loop in controller which run more than 1 minute.
for($i=0;$i<$count;$i++){
$peicedata = getdata();
$ar=explode(",",$peicedata);
$data['firstname']=$ar[0];
$data['lastname']=$ar[1];
$data['email']=$ar[2];
$data['website']=$ar[3];
}
above function getdata() take around 10 second to get data back. when data get from getdata() i want to pass that data immidiate to view.

Are you passing the data to a view?
$this->load->view('view', $data);
http://codeigniter.com/user_guide/general/views.html

It seems you use CodeIgniter. You can pass data to template like this:
$this->load->view('show', $data);
show is the template. And please read User Guide for more details.
BTW, if your script runs too slow, you should check your code.

Related

Laravel: How can i pass data from controller to blade using yajra/laravel-datatables

I am using yajra/laravel-datatables. It's successfully installed. Now, how can i pass data Laravel Controller to blade??
$contacts = Contact::Where('is_deleted',0)->get();
This is the code. There are lots of data. For those reason, It takes lots of time to load in dataTable. I want to load and pass data to dataTable using yajra/laravel-datatables.
There are many option which you can use
compact method
$data = Contact::Where('is_deleted',0)->get();
return view('home',compact('data'))
with method
$data = Contact::Where('is_deleted',0)->get()
->with('data',$data);
For more information, read the documentation

Passing data to layout in Laravel 5.2 on each request

Hi I am building an application using laravel 5.2 I have a messaging system and I would like to populate some sections in my layout like 'Current Unread Messages Count' and a summary of the last 5 messages.
The way i was gonna go about it is to call a method get the data that I need and then pass the data to the layout and then render the view.
I understand this can be done with a view composer but I have no idea how. Any input would be greatly appreicated. Thanks in advance
Yes, you can do that with a view composer
Let's suppose you have a my_menu.blade.php view file and you want to pass some data to it. In a service provider's boot method do:
//every time the my_menu view is rendered, this callback will be called
View::composer('my_menu', function( $view )
{
$data = //get your data here
//pass the data to the view
$view->with( 'data', $data );
} );
Now, everytime the my_menu view will be rendered, Laravel will call your callback, fetch the data and pass the data to the view. So, in your view file, you can access the data with $data

Passing data from Views laravel 4.2

I do some calculations in the controller, and pass the data to my view like this.
return View::make('fend.clist', compact('detail_'));
From this view controller "fend.clist" I'd like to open another view (blade) along with the 'detail_' variable which basically has some data that I need to show there. I tried POSTing it, but I wasn't able to recieve the data completely.
How else can I pass data from one view to another view?
By using the sessions, first put:
Session::put(['name'=>'value']);
Then get:
Session::get('name');
You must nest sub-view to view and pass data to sub-view.
return View::make('fend.clist')->nest('fend.detail', compact('detail_'));
More info in docs

How to pass parameters to Codeigniter controller from view page

Hi I need to pass variable parameters to codeigniter controller without click function any link from view page. I have no idea how to pass data to controller from view with out click anything.
My view page name is rows.php, In view page I had variable as below
$rows = 10;
this rows value I need to send to controller, if I send this to controller will display data as per parameter.
My Controller
public function getRows($parameter)
{
after query
$this->load->view('rows',$data);
}
I would appreciate your help.
You can using helper without any clicks.
Helper
//some_helper
function abc($b)
{
$a=$b-10;
return $a;
}
View
$this->load->helper("some_helper"); //note that helper file name always should be postfixed by '_helper'
abc(20);
$rows = "10";
file_get_contents("yoursite.com/getRows/".$rows);
or
file_get_contents("yoursite.com/index.php/getRows/".$rows);
anyway, I don't see why you would like to do so, the view should preferebly not contain your php code but only the html or the variables you want to output.

how to pass a variable from one controller to the other in Code igniter

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. %#$)

Categories