Welcome Guest, Not a member yet? Register   Sign In
Using flush in controller, return echo before running script
#1

[eluser]Unknown[/eluser]
Hi,

I would first like to say hello, this is my first post here and hopefully one of many while I learn code igniter.

I have got a controller that on a form submit runs a php function, however I am trying to have it echo by using a flush that will echo to the user before the script is finished (as the script can take a few minutes to run) I am then using ignore_user_abort(); to ignore the user closing the window or leaving.

However after submitting the form it still does not echo until the script has finished and I am unsure why, here is my controller function:

Code:
function harvest()
{

  $this->data['title'] = "Harvest";

  if (!$this->ion_auth->logged_in())
  {
   redirect('account', 'refresh');
  }


  if ($this->form_validation->run() == true) {
  
   echo "We are now harvesting your users, we will email you the results.";
   ignore_user_abort(true);
   set_time_limit(0);
   flush();
  
   Now I have a load of script here that is doing my main function that takes around 10 minutes


  } else {

   //display the create user form
   //set the flash data error message if there is one
   $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

   $this->data['keyword'] = array(
    'name'  => 'keyword',
    'id'    => 'keyword',
    'type'  => 'text',
    'value' => $this->form_validation->set_value('keyword'),
   );
    
   $this->load->view('farm/harvest', $this->data);

   }

When I submit my form it just loads the page for a good few minutes then finally shows my echo as well as completing the action. I would ideally like it just to echo to the user that they are free to leave before running the script so they don't have to stick around for their email.

Any help is greatly appreciated,
Thanks,
Simon
#2

[eluser]CroNiX[/eluser]
You should use the output class instead of echoing from your controllers. It's what it's for.

But, the better solution to your problem is to hand that request off to an outside, asynchronous process, so your user will see the message immediately and the processing gets done in the background.

This can be as simple as using a socket call to send the data to a different controller or method and not waiting for the response.

We use this all of the time for handing off data to an external CRM, which takes time to process, after a user fills out a form and it works great. The end user shouldn't have to wait for processing.

Here is an (CI) example: http://blog.markturansky.com/archives/205




Theme © iAndrew 2016 - Forum software by © MyBB