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

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.

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 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.

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
}

Two controllers for one view, CodeIgniter?

Hey guys, I am new to CodeIgniter and need some help. I have a controller that formats the content area of a post. The problem is that I also need to create a sidebar that contains dynamic groups, and a right column that contains recent posts. This isn't hard, the problem I'm running into is that I want the sidebar, and right column on every page, and I don't want to recode the same bits to get the data in every controller.
What would be the best way to do this without copy/paste?
There are a lot of ways to do this.
1) Templating: This is my preference for most cases (because my templates are complex), I render my view into a variable using something like:
$content = $this->load->view('myview', $page_data, true);
Then I load it into the template parser (fyi you could load it into another view too) like this:
$this->load->library('parser');
$data = array(
'page_title' => 'My Page Title',
'page_content' => $content,
'side_bar' => side_bar(), // function which generates your side bar
'right_col' => right_col() // function which generates your right column
);
$this->parser->parse('my_template', $data);
Then your template is like:
<html>
<head>
<title>{page_title}</title>
</head>
<body>
<div>{side_bar}</div>
<div>{page_content}</div>
<div>{right_col}</div>
</body>
</html>
2) Load another view in your view: (assumes you menu is a view not a controller) Something like this:
<?php $this->load->view('menu_view'); ?>
3) PHP Includes: exactly how you would do it in plain PHP (just include a url which points to a controller which returns a menu), Something like this:
<?php include("/common/sidebar"); ?>
Codeigniter will render that page and then include it.
4) AJAX.. i use this if the content in the "template" content is less important, like banners, suggested related item lists and such.
Use PHP to generate a static HTML page, such as side_bar.html...
Then you can include it on other pages.
You could look into HMVC. It's especially suited for "widget"-type areas like you are talking about.
Essentially what you will do is create two full MVC structures - one for your sidebar and right column, including a controller, a model(if required), and a partial view. Then, you can call this controller directly from the main view to pull the required content in to the page.
To actually call it from within a view, just place the following in the markup wherever you want the sidebar to appear:
<?php echo modules::run('module/sidebar/index'); ?>
The index isn't required, but I put it there to demonstrate that you can call different methods using modules::run(). You can also pass an unlimited number of parameters to modules::run().
In code igniter, there is an optional third parameter to $this->load->view that lets you return a rendered view as a string, which can in turn be used for assignment. What you can do is create a master template, that has all the common parts, as a very simplified example:
<html>
<head>
</head>
<body>
<?php echo $sidebar; ?>
<?php echo $content; ?>
<?php echo $right_column; ?>
</body>
</html>
Then you can create a private function in your controller to populate the dynamic content of your common parts, and combine them with your content and master template:
private function BuildTemplate($view, $data) {
// Generate sidebar content
$sidebar_data['...'] = 'blah blah';
$master_data['sidebar'] = $this->load->view('sidebar', $sidebar_data, true);
// Generate right column data
$right_data['...'] = 'blah blah';
$master_data['right_column'] = $this->load->view('right_column', $right_data, true);
// Now load your content
$master_data['content'] = $this->load->view($view, $data, true);
// Merge it into the master template and return it
return $this->load->view('master' $master_data, true);
}
Then in your appropriate controller method:
public function index() {
$data['...'] = 'blah';
echo $this->BuildTemplate('index', $data);
}
Which will pull everything together for you. You can optionally add extra arguments to BuildTemplate if you want to add things like page specific titles or scripts.
I'm not sure if your problem is in the view, or in the (dynamic) data to be shown in the (common parts of) that view.
If it's the later (as seems to suggest the phrase 'I don't want to recode the same bits to get the data in every controller'), then you have several options. For example.
Put the logic to get the 'common' data in some function outside the controller, as a helper or inside some model, and call it from your controllers.
Make your controllers inherit your own custom controller, that implements that data gathering function.
Refactor your two controllers into a single controller, with different functions for each scenario.
1-Create a custom library class in library folder with the below code
if (!defined('BASEPATH')) exit('No direct script access allowed');
class LoadView{
function __construct(){
$this->CI =& get_instance();
}
function load_view($page){
$this->CI->load->view('header');
$this->CI->load->view('sidebar');
$this->CI->load->view($page);
$this->CI->load->view('footer');
}
}
2-Now load this library in your controller like this
$this->load->library('loadview');
3-Now call the library method and simply insert your page name and you don't have to include header,sidebar and footer again and again as they will be dynamically included by your library.
$this->loadview->load_view('about.php');

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.

Categories