Batch API example - the simplest

Public

You can use this example to see how simple is using the batch api in Drupal 7

First create your function;

Get raw version
php
  1. function show_messsage() {
  2. drupal_set_message('hello world');
  3. }

Then add this code to call your function as many times you like.

Get raw version
php
  1. function modul_name_my_batch_func() {
  2. $operations = array();
  3. for ($i=1; $i<=5; $i++) // run this for five times
  4. {
  5. $operations[] = array('show_messsage', array()); // function name that we want to run
  6. }
  7. $batch = array(
  8. 'operations' => $operations,
  9. );
  10. return batch_set($batch);
  11. }
  12.  
  13. modul_name_my_batch_func();