function batch_test_perform_batch_operation

7.x batch.test.inc batch_test_perform_batch_operation(&$batch, $items_max)
6.x batch.test.inc batch_test_perform_batch_operation(&$batch, $items_max)
5.x batch.test.inc batch_test_perform_batch_operation(&$batch, $items_max)

This is our batch operation function, that gets called each time the batch executes. Notice the first argument is passed by reference, and it is our $batch variable. The other arguments are whatever we defined in the batch_set operation.

Some variables in batch to be aware of-- it's up to us to populate: $batch["results"]["current"] and $batch["results"]["total"] (current is the record number we are on, total is the current number of records.) !!!!! You MUST set $batch["results"]["finished"] = TRUE when the batch has completed.

Parameters

unknown_type $batch:

unknown_type $items_max:

File

modules/batch/batch.test.inc, line 120
This file contains functionality for testing out Batch's processing with a sample form which performs some arbitrary actions.

Code

function batch_test_perform_batch_operation(&$batch, $items_max) {

  // if this is our first time through, let's init our values.
  if (!isset($batch ["results"]["total"])) {
    // Our first time through.  Let's start up.
    $batch ["results"]["total"] = $items_max;
    $batch ["results"]["current"] = -1; // so that we start with zero below
    $batch ["results"]["finished"] = FALSE;

  }

  // Set an arbitrary limit of how many records we are going to examine each time the routine runs.
  // This should be whatever we believe we can safely accomplish before the routine times out.  Set low for safety.
  $limit = rand(5, 15);
  $c = $t = 0;

  for ($t = $batch ["results"]["current"] + 1; $t < $batch ["results"]["total"]; $t++) {


    // ....
    // ....
    // Here's where your code would go 
    // ....
    // ....

    $c++;
    // If we've gone past the limit, then break.
    if ($c >= $limit) {
      break;
    }

  }


  // Update our values.
  $batch ["results"]["current"] = $t;


  // Have we finished?
  if ($batch ["results"]["current"] >= $batch ["results"]["total"]) {
    $batch ["results"]["finished"] = TRUE;
  }


  ////////////////////////
  ////////////////////////
  //  !!!!!!
  // We are going to artificially slow down this script so we can see
  // the batch process operating.
  // Obviously, take this out in your own script.
  sleep(1); //sleep for 1 second.



}