Saving several array at once

Asked

Viewed 488 times

1

Well I have a function that is complex, because I need within the same function to do several for due to the complexity and amount of information.

I am consuming an API where I need to get monthly data from a certain person, and every day can have several items, ie changing in kids.

Pessoa 01
    MES 01
        PRODUTO 01
        PRODUTO 02
    MES 02
        PRODUTO 01
    MES 03
        PRODUTO 01
        PRODUTO 02
        PRODUTO 03
    MES 04

So I have to go through every data and save one by one. My function developed as follows:

$todosMembros = DB::select('select id from membros');

    foreach ($todosMembros as $membro){
        for($a = 1; $a < 13; $a++){ //AQUI preciso ir pois é mensal então tenho 12 messes
            $categorias = meuAPI; //AQUI preciso ir uma vez para cada membro
            $json_file = file_get_contents($categorias);
            $json_str = json_decode($json_file);
            $todasCategorias = $json_str->list;

            $tamanho = count($todasCategorias);

            if($tamanho == 0){//CASO Não haja dados para ele não faço nada

            }else{
                $tamnhoArray = count($todasCategorias); //QUANTIDADE DE PRODUTOS

                for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
                    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
                }
            }
        }
    }

My problem is that this saving however I am exceeding the waiting time of the browser, I mean it takes so long in the loops of the function that the time limit is exceeded, I checked in the database and this saving normally, after saving everything I list to verify if it happened all right, but nothing appears precisely by the limit.

My question is this: What is the best way to effect this data in the database, so as not to generate problems for my application

1 answer

1


set_time_limit

Usually this happens because of the php timeout, tries to set max Execution time to a higher value, or zero to disable and not stop until the script execution is finished. http://php.net/manual/en/function.set-time-limit.php

set_time_limit(0)

Times of time out simply because the server did not return any data, in which case could use the ob_flush(), so the browser will know it has had response and will not give timeout.

ob_flush()

As you go displaying content, usually calling echo, var_dump(), print(), and derivatives, all this is being stored in a buffer, as soon as the buffer reaches the limit size it pours flush() this content for the browser. The ob_flush() serves to "force" this dump, soon the content appears on time.

If you put an echo at the end of the last is

for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
    echo "Dados foram salvos<br>";
 }

you will realize that it will take a while to start to appear the texts, and when it appears will appear several lines at once, so why was everything in the buffer.

If you add the ob_flush()after the echo Voce will see that the text will be displayed line by line.

for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
    echo "Dados foram salvos<br>";
    ob_flush();
 }

What happens is when I call the echo the text is in the buffer, soon after the ob_flush() force the dump to the browser, as the only thing in the buffer was the text of the echo it is displayed on time.

  • How to use the ob_flush() ?

  • @Renanrodrigues edited the answer and added an explanation of ob_flush()

Browser other questions tagged

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