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?
Related
I have some PHP code in WordPress (running in the Shortcode Exec PHP plugin) that runs, and loads a variable and displays it in the page.
I have a caching plugin on the site (recently switched to WP-Rocket), and when the plugin is enabled on the whole site, that variable is cached as well, so it doesn't update every time the value changes and the page is reloaded.
How can I load the code/variable with AJAX (or some other method) so that it isn't cached?
Here is the PHP code I'm currently using:
include '/full/path/variables.php';
$countsrc = $totalcount;
$count = (int) $countsrc;
switch ($count)
{
case 0:
echo "There currently aren't any items in the list.";
break;
case 1:
echo "There is currently $count item in the list.";
break;
default:
echo "There are currently $count items in the list.";
}
Is there any way to use some method like Ajax to make sure that the variable isn't cached?
Any help/input would be really appreciated!
Thanks a lot!
I'm using this a particular code to dynamically load content on a webpage.
Problem is, I want to use it to load multiple things on the same page. How do I go about making this possible? I've already isolated the problem to the fact that the second instance of the code that runs replaces the entire URL instead of appending an additional ?="pageurl" but I'm stuck right there.
I've got a second script that uses the variable p instead of b so the browser knows to load p's content in a different location.
This is what the code does:
http://www.youtube.com/watch?v=Qg-EBdNaUbo
[edit]
Not sure why you guys can't see the link, it explains everything. I'm calling the script via a link Here is the link again youtube.com/watch?v=Qg-EBdNaUbo
CODE:
<?php
$pages_dir = 'pages';
if(!empty($_GET['b']))
{
$pages = scandir($pages_dir, 0);
unset($pages [0], $pages [1]);
$b = $_GET['b'];
if(in_array($b, $pages))
{
include($pages_dir.'/'.$b);
}
else
{
echo 'sorry page not found';
}
}
else
{
}
?>
Below is a simple example of code to fetch results from a REST XML API.
This is just a small portion I have extracted from my real PHP class for this question.
in the API URL which returns an XML document, I am curious about how I could fetch all the results from 1 page and then move on to fetch from the next page.
$this->api_page sets the page that the API returns data from.
Looking at the basic code below using SimpleXMLElement how could I for example return the data from 10 pages or all pages in the API starting at a page number, loading the results for that page and then fetching the next page an moving on.
Right now I am doing it with JavaScript and PHP by passing a Page number in the URL to my script using $_GET['page'] the problem with this is it requires a user to load the page and it's kind of sloppy.
My real API script will be ran from a Cron job on the server, so with that in mind, how could I fetch all pages?
I ask this question based on this example code below but also because it is a task that I often have to do on other projects and I don't know a good way of doing this?
<?php
$this->api_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
'&type=animals&limit=' .$this->api_limit.
'&startPage='. $this->api_page;
$xmlObj = new SimpleXMLElement($this->api_url, NULL, TRUE);
foreach($xmlObj->pet as $pet){
echo $pet->animalID;
echo $pet->orgID;
echo $pet->status;
// more fields from the Pet object that is returned from the API call
// Save results to my own Database
}
?>
Based on the assumption that you run on a pretty stable environment you could loop through the pages like this:
<?php
$this->base_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
'&type=animals&limit=' .$this->api_limit.
'&startPage=';
$start_page = $this->api_page;
$end_page = 10; //If you have a value for max pages.
// sometimes you might get the number of pages from the first returned XML and then you could update the $end_page inside the loop.
for ($counter = $start_page; $counter <= $end_page; $counter++) {
try {
$xmlObj = new SimpleXMLElement($this->base_url . $counter, NULL, TRUE);
foreach($xmlObj->pet as $pet){
echo $pet->animalID;
echo $pet->orgID;
echo $pet->status;
// more fields from the Pet object that is returned from the API call
// Save results to my own Database
}
} catch (Exception $e) {
// Something went wrong, possibly no more pages?
// Please Note! You wil also get an Exception if there is an error in the XML
// If so you should do some extra error handling
break;
}
}
?>
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>";
I am relatively new to CodeIgniter, so I'm not sure if this is just bad coding, or if it is a problem with how I'm using CodeIgniter's flash data. For context: the user submits a phrase in a simple HTML form. The phrase is compared against what should be typed in (pretty simple, right?). This correct phrase changes based upon what step in the activity they are on. When they get the text wrong, I am attempting to use flashdata to show the error message. Here are the controller portions, followed by the view:
//Get step number
$step = $this->input->post('step');
$correct_text = array(
1 => 'TESTPHRASE',...
...
//If user enters the correct text
$entered_text = strtoupper($this->input->post('entered_text'));
if ($entered_text == $correct_text[$step])
{
...
}
//If user enters the incorrect text
else
{
$data['step'] = $step;
$this->session->set_flashdata('entry_error', '<b>Sorry!</b>Your entry was incorrect. Be sure to carefully read the instructions!');
$this->load->view('template', $data);
}
Here is the view that only runs every other time.
<?php
if ($this->session->flashdata('entry_error'))
{ ?>
<div id="game_error">
<?php echo $this->session->flashdata('entry_error'); ?>
</div>
<?php } ?>
From the docs: CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
You are setting the flashdata and then trying to access it during the same request. It's not available until the next request which is why it seems like it's only working every other time.