How to pass parameters to Codeigniter controller from view page - php

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.

Related

How to pass variables from view to controller without form in laravel php

i have these values in the view folder.
.blade file is
#foreach($nam as $ergebnisse)
<h3>Id:{{$ergebnisse->id}}
Name_Id:{{$ergebnisse->name_id}}
Geometrie_Id:{{$ergebnisse->geometrie_id}}
</h3>
<h3>
<li>{{$ergebnisse->probe}}</li>
<li>{{$ergebnisse->schnittgrosse}}</li>
<li>{{$ergebnisse->temperatur}}</li>
<li>{{$ergebnisse->zeit}}</li>
<li>{{$ergebnisse->aussehen}}</li>
<li>{{$ergebnisse->farbe}}</li>
<li>{{$ergebnisse->deformation}}</li>
<li>{{$ergebnisse->geruch}}</li>
<li>{{$ergebnisse->texture}}</li>
</h3>
#endforeach
now i want to make another query in terms of these parameters.so how can i pass these to the controller or is there any other way of generating the queries within the view and displaying them?
Your question is not very clear, so i have 2 possible solutions:
If you want to load another view you can do something like this:
You can send parameters with links no need to be a form.
so you can do something like Do stuff
This will send the id of your $nam object to the route you prefer.
your route will look like this:
Route::get('your-url/{$nam}', YourController#test);
And in your controller you have to catch this value
public function test(Nam $nam) {
return $nam;
}
If you want to load the same view and have 2 different queries, just
do the 2 different queries inside your controller and send them
trough seperatly.
So in your current controller:
public function index(){
$nam = Nam::all();
// this is your second query:
$nam2 = Nam::latest()->get();
return view('your-view', compact('name','nam2' //this is the second variable))
}

How to pass a data with redirect in codeigniter

In my controller i used this way. i want to pass a variable data to my index function of the controller through redirect
$in=1;
redirect(base_url()."home/index/".$in);
and my index function is
function index($in)
{
if($in==1)
{
}
}
But I'm getting some errors like undefined variables.
How can i solve this?
Use session to pass data while redirecting. There are a special method in CodeIgniter to do it called "set_flashdata"
$this->session->set_flashdata('in',1);
redirect("home/index");
Now you may get in at index controller like
function index()
{
$in = $this->session->flashdata('in');
if($in==1)
{
}
}
Remember this data will available only for redirect and lost on next page request. If you need stable data then you can use URL with parameter & GET $this->input->get('param1')
So in the controller you can have in one function :
$in=1;
redirect(base_url()."home/index/".$in);
And in the target function you can access the $in value like this :
$in = $this->uri->segment(3);
if(!is_numeric($in))
{
redirect();
}else{
if($in == 1){
}
}
I put segment(3) because on your example $in is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$in you'll have to use segment(4).
Hope that helps.
Use session to pass data while redirecting.There are two steps
Step 1 (Post Function):
$id = $_POST['id'];
$this->session->set_flashdata('data_name', $id);
redirect('login/form', 'refresh');
Step2 (Redirect Function):
$id_value = $this->session->flashdata('data_name');
If you want to complicate things, here's how:
On your routes.php file under application/config/routes.php, insert the code:
$route['home/index/(:any)'] = 'My_Controller/index/$1';
Then on your controller [My_Controller], do:
function index($in){
if($in==1)
{
...
}
}
Finally, pass any value with redirect:
$in=1;
redirect(base_url()."home/index/".$in);
Keep up the good work!
I appreciate that this is Codeigniter 3 question, but now in 2021 we have Codeigniter 4 and so I hope this will help anyone wondering the same.
CI4 has a new redirect function (which works differently to CI3 and so is not a like for like re-use) but actually comes with the withInput() function which does exactly what is needed.
So to redirect to any URL (non named-routed) you would use:
return redirect()->to($to)->withInput();
In your controller - I emphasise because it cannot be called from libraries or other places.
In the function where you are expecting old data you can helpfully use the new old() function. So if you had a key in your original post of FooBar then you could call old('FooBar'). old() is useful because it also escapes data by default.
If however, like me, you want to see the whole post then old() isn't helpful as the key is required. In that instance (and a bit of a cheat) you can do this instead:
print'<pre>';print_r($_SESSION['_ci_old_input']['post']);print'</pre>';
CI4 uses the same flash data methods behind the scenes that were given in the above answers and so we can just pull out the relevant session data.
To then escape the data simply wrap it in the new esc() function.
More info would be very helpful, as this should be working.
Things you can check:
Is your controller named home.php? Going to redirect(base_url()."home"); shows your home page?
Make your index function public.
public function index($in) {
....
}

calling a controller function from a view in codeigniter

I'm a newbie to codeigniter and I'm attempting to write a function that would basically save a name and url to session data whenever you visited a certain page, then report it back in a small widget on the screen.
It's supposed to work as a kind of history function for what pages have been accessed and in what order. So far when working with test data it works great! however I'm trying to figure out how I can call the "add" function on each page that is visited, so we can supply the name and url of that page that was visited. Is there any way to do this? Or is there any way to report back a set of variables such as a name and url for a page after you visit it?
For example: say I visit page1, page2, page3, and page6 and I want each of those to show up in my history function. On each of those pages I would load the history view, and I would want to call the history's controller function "add(name,url)" and fill it in something like this:
add('page1','page1.php')
But I know that you're not supposed to access the controller from the history because that's not the way it's supposed to be done, however I cannot think of any better way to do this. Any help would be appreciated.
I don't know why dont you call this on every controller.
but if you want to call a function of the current controller, you have to get the instance of the current controller this way:
<?php
$CI =& get_instance();
$CI->method($param);
?>
the easiest way to do this would be to put a method in the constructor of your class. that way it will always run first thing, no matter what you are doing. remember that anything you can do in the controller -- sessions, validation, etc -- you can do in a model.
function __construct() {
parent::__construct();
// load model that has page tracker methods
$this->load->model( 'pagetracker_m' );
// call the method to track the pages, and have it return the results
if ( ! $this->history = $this->pagetracker_m->_trackpage(); ) {
echo 'Error getting history ' ; }
} // end construct
function something() {
// pass history to $data, then use echo $history->firstpage, etc in view
$data['history'] = $this->history ;
// call your view etc
}

How to get data from controller to view?

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.

How to invoke method from codeigniter controller when the page loads?

Can someone please tell me how to invoke a method from the codeigniter controller when the page loads? What I want to do is to fetch some data from database and put it inside meta tags before page actually load. I obviously want to do it inside header. It should be something like this:
method, which fetches some data from db and returns it
<meta property="og:title" content="<?php echo $returnedValue; ?>"/>
Can someone give me a hand with this one? Thanks.
The page only loads after your controller loaded the view. So run your method before you call the view and add the result to the $data array, then load the view with the $data array as a parameter.
public function index()
{
$data['returnedValue'] = yourMethod();
$this->load->view('template', $data);
}
Now $returnedValue, in the view, holds the return value of your method. echo it in the view wherever you want to include it.

Categories