1

I am trying to make a loop. Something like this:

I have 1000 records. Within this 1000 records it should take

  • first 200
  • then 201-400
  • then 401-600
  • then 600-800
  • then 801-1000

This is my code:

for($i=1; $i<=1000; $i++){
if($i > 200){
//$this->db->save(); //save some data
break;
}

echo $i."<br>";

if($i == 200){
  for($i=201; $i<=400; $i++) {
  //$this->db->save();  //save some data
  echo $i."<br>";
   }
}

if($i == 400) {
  for($i=401; $i<=600; $i++){
     echo $i."<br>";
   }
 }

Part 2:

$totalClientCount = 1000;      

$input_array    =    range(1, $totalClientCount);
$chunks         =    array_chunk($input_array, 200);

foreach ($chunks as $k => $chunk) { 

if ($k == 0) {  // 0-200
  $this->db->where('AccountNo', $id);
  //$this->db->limit($offset,$limit);
  $getClientDetails = $this->db->get('table');
  echo '<pre>';
  print_r($getClientDetails);    
}

This is where I am stuck. How do I execute this properly?

Blackbam
  • 12,200
  • 19
  • 71
  • 117
Mitukula
  • 109
  • 2
  • 13

3 Answers3

3

You should be using array_chunk() here. Let me show an example with 100 entries, with first 20, 21-40, 41-60 and so on.

$input_array    =    range(1, 100);
$chunks         =    array_chunk($input_array, 20);

This is what $chunks array will look like:

Array
(
   [0] => Array
    (
        [0] => 1
        [1] => 2
        ......
        [9] => 10
     ),
  [1] => Array
    (
        [0] => 21
        [1] => 22
        .......
        [2] => 40
     )
     .........
     .........
   [4] => Array
      (
        [0] => 81
        [1] => 82
        ......
        [19] => 100 
   )

Now loop through your $chunks array.

foreach ($chunks as $k => $chunk) {
    /* $k = 0 means first 20 items */
    if ($k == 0) {
        // your code here
    }
    foreach ($chunk as $i) {
        echo $i;
        echo '<br/>';
    }
}
Object Manipulator
  • 8,485
  • 2
  • 9
  • 25
  • thank you for ur reply. But it should not go to another array until complete of first array[0]. i have test it is loading all at once 1000 records.just i need 0-200 and save some data, then array[1] – Mitukula Apr 20 '17 at 05:42
  • sorry for above comments.Its run perfectly what i was expecting thank you guys. @Object Manipulation,@Nishant Nair,@Anushil Nandan – Mitukula Apr 20 '17 at 05:53
1

in case you are doing some task even after given range [200] you can use this:

$range = 200;
for($i=1; $i<=1000; $i++){
  //$this->db->save();
  if ($i%$range == 0) {
    //...
  }
}

basically here you are calling $this->db->save() every time till the end of loop and just using 201,401,601,801 to do something else, like printing current value of $i

in case you want to do separate tasks after every range:

$range = 200;
$fn = 0;
for($i=1; $i<=1000; $i++){
  $functionToCall = 'fn_'.$fn;
  $functionToCall();
  if ($i%$range == 0) {
    $fn = $i+1;
  }
}

function fn_0()
{
  //...
}

function fn_201()
{
  //...
}

function fn_401()
{
  //...
}

function fn_601()
{
  //...
}

function fn_801()
{
  //...
}

it's always a good practice to logically separate your functions.

Also if you have to just save 1000 times, do not use loop to insert. Try batch insert instead, something like insert multiple rows via a php array into mysql

Community
  • 1
  • 1
Anushil Nandan
  • 294
  • 2
  • 6
0

you can use array_splice. If you need to send the array with specific interval its the best option.

I have implemented it in my code for sending notification for 1000 ids at a time.

$sendIds = array_splice($your_array,0,199);
Nishant Nair
  • 1,936
  • 1
  • 10
  • 17