Codeigniter: Run code only if in certain view - php

I need to add a 3rd party script (Optimizely) to my so I want to drop it in to my main template.
However, I only want it to run on one page, my /order page. How can I add this script to the head so that it checks, if View = Order then include this js file, otherwise, don't include it?

While loading the view, send it an additional variable having controller name.
In the view, then you can check if the variable's value is order, then you can echo the script.
Code will roughly look like this:
// in your controller
$data = array();
// some code that will fill $data
$data['controller'] = get_class($this);
$this->load->view('your_view', $data);
// now, in your view,
if($controller == 'Order')
echo "<script src='......'></script>";

Related

how to include a view file and the variables in another view in codeigniter

I create a view file called "Mainbar" and i want to display some other view files in that, like: slideshow products and ... .
if i add $this->load->view('slideshow'); in Mainbar page the variabales I used in slideshow function wont work. what is the best way to display view files into other view and pass the variables of them too
You can return view contents as data in the form of string.Using
$data= $this->load->view('slideshow', '', TRUE);
Then in another view just echo $data; will display view slideshow.
There is a third optional parameter lets you change the behavior of the method so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to TRUE (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
For more visit Codeigniter Views
I would declare an array that would hold the variables I want to display on the page.
So your code would look something like
$data['page_title'] = 'Why so serious';
$data['page_to_load'] = 'slideshow';
$this->load->view('Mainbar', $data);
Then in the Mainbar.php file you would load $page_to_load and the rest of that variables in your data array.
<?php $this->load->view($page_to_load); ?>

How to continue controller execution after load->view in CodeIgniter?

I have a form to add new product. When product is added I need to write notifications in database for users that would be interested in this product. What I want to do is to display success message and continue with saving notifications.
In plain php we would use combination of ob_start(), ob_end_flush() and ignore_user_abort() however I can not get to understand how to get this working in CodeIgniter.
Here is simple example:
if ($this->form_validation->run()){
$this->load->view('success', $data);
$x = 1;
$path = $_SERVER['DOCUMENT_ROOT'].'/test.txt';
$file = fopen($path,"w");
while($x <= 5) {
echo fwrite($file,"new product".$x);
$x++;
sleep(5);
}
fclose($file);
}
Thing is that I do not completelly understand how CI loads view files. In given example I have to wait 25 second (5*5) until view is loaded. So the question have to parts:
How to force loading view immediately?
How to set ignore_user_aborts() in CI?

cakephp specify Theme for an Element in Controller

In cakephp, how do we specify which theme to use for an Element. I am initializing View object in Controller. I need to pass the element content as ajax response.
Controller :
$view = new View($this);
$view->layout = 'theme2';
$view->theme = 'newNav';
foreach($ctps as $ctpName)
{
$ctp[] = $view->element($ctpName);
}
At first I thought of accessing it as
$ctp[] = $view->element('../Themed/new/Elements/'.$ctpName);
But it obviously does not take care about element's directory. As some of the elements are in app/View/Elements and some are in app/ViewThemed/new/Elements/ directory.
Please suggest.
As per Arilia's suggestion, I am now trying
$this->viewClass = "Theme";
$this->viewPath = 'Elements';
$view = new View($this);
$view->theme = "new";
$view->layout = "theme";
$ctp = $view->element('userprofile');
echo json_encode($ctp);
die;
I am making an ajax call to this code. and it returns
"Element Not Found: Elements/userprofile.ctp"
what I was trying to develop was to render the basic page at first so that it can be cached. and then with the ajax call fetch the parts of UI that are session dependent and then replace the respective elements on browser.
To implement this, I tried fetching cakephp elements in controller, json encoding them and echoing them back. The code above though it worked for a normal dynamic page, it did not work for ajax calls. Cakephp somehow misbehaved here.
Now, as a better alternative, I have moved the code to View itself and I am echoing the json encoded from there. and as expected it is working well. Code is as
app/View/Themed/new/Elements/get_ctps.ctp :
<?php
foreach($ctps as $ctpName)
{
$ctp[] = $view->element($ctpName);
}
echo json_encode($ctp);
die;
?>
Its not a usual case. However, it might be helpful for someone.

What if a view is an empty value while loading it in codeigniter?

I am building an application which requires loading view dynamically. The value of view is a variable and it depends on what the controller sets it. Sometimes my value is returned as empty or null value, that is I don't want any view to load. But then my code breaks at the same place and half of the screen goes blank. Is there any workaround for this?
Check for the value in the controller, if the value is empty don't load the view?
if($variable== something)
{
$this->load->view('myView');
} else {
do something else;
}
I found a solution for this myself. What I was doing earlier in my view was:
$this->load->view($viewname);
The value of the viewname used to come from controller as:
$data['viewname'] = 'dynamicview';
$this->load->vars($data);
What I am doing right now is, in my controller:
$data['viewname'] = $this->load->view('dynamicview',null,TRUE);
$this->load->vars($data);
And now in my view, I simply write:
echo $viewname;
Actually this passes view as data in the controller and then parses it in the view where we actually print it.
I hope this helps someone out there. Saved my day atleast...

Returning a resultset from one page to another in PHP

I have 2 php pages: query.php and result.php.
In query.php, I am executing a query (select) statement. It's returning a resultset
$rs = mysql_query($query);
Now I want to return this resultset from query.php to another page result.php and work with it. Like this:
In query.php:
return $rs
and in result.php:
$result = executeQuery($query) // we get the resultset in this variable
while ($row == mysql_fetch_array($result){
//do something
}
If the above is not recommended, please provide me with alternatives. But I want the query function and resultset in different pages.
You could just include results.php in your query.php page if you're just looking to keep the code separate in the source files but aren't actually required to redirect from one page to another:
In query.php:
$rs = mysql_query($query);
include "results.php";
In results.php:
while ($row == mysql_fetch_array($rs){
//do something
}
As far as trying to "return $rs" from one page to another that's not how PHP works. The return statement is only valid within a function. If you want to actually pass data from one PHP page to another and will be redirecting to that other page then you'll need to use either a session, a cookie, pass it in the URL (i.e. use GET) or use curl and add it as a POST var.
If this is really the way it must be, store the result set in a database somewhere or in a file and give each result a unique name. Then pass that name to the next page so it can be retrieved.
query.php will redirect to result.php?result_set=ab24sdfsdfklls for instance.
This has the added advantage that you can use the result_set as often as you want. Visitors can have multiple result sets during one visit. They can share the URL of the result set page with other people, etc.
Just be sure to eventually prune the data store as it will just keep on growing, but that's another matter entirely.

Categories