File creation for database data logging

Asked

Viewed 67 times

1

I have 13640 users and I am creating 1 email capture file for every 1000 users. With the code I have, it generates 13 files and has a surplus of 640 users that are left behind because it did not reach 1000 counter. So are 13 files instead of 14, the last that is not created would save not 1000, plus the rest (640).

The code I’m using to create the files inside the folder is this:

// Instrução até chegar na quantidade final de registros
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $contador++;

    if($contador == 1000){

        $excel      = new ExcelWriter("listas/report".rand(10000, 99999).".xls");

        $contador = 0;

    }

}

The $excel variable takes over a class to create these files each time the loop happens but does not create the last file because it does not reach 1000 records but 640. How could you edit this code to get the desired result?

1 answer

4

$contador = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   if( $contador++ % 1000 == 0 ) { // assim teremos um ExcelWriter no 1o item de cada 1000.
      $excel = new ExcelWriter("listas/report".rand(10000, 99999).".xls");
   }
   echo $row['email']; // troque pro campo correto da base de dados, apenas para teste
   $excel... // ponha aqui o método que escreve a linha no Excelwriter criado
             // pois essa parte do loop é executada para cada um dos itens, incluindo
             // os 640 finais.
}

This is just a sketch, I don’t know the details of the Excelwriter, but I believe it solves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.