Insert attempt before error (PHP)

2

I have a student enrollment system in a college system.

Part of the code where the error is or is not:

        while (($matricula = fgets($handle, 1024)) !== false) {
            $cont++;
            erro("Lendo matricula $cont de $qtde_matriculas");
            $matricula = trim($matricula);
            if(!is_numeric(trim(substr($matricula,0,5)))){
                erro("matricula $cont invalido [$matricula]");
            }else{
                $arrayReturn = buscarnafaculdade( $matricula );
                if($arrayReturn['status'] == "ERROR" || $arrayReturn == false){
                    erro(" ERRO servidor faculdade para inserir matricula [$matricula]");
                    $result = print_r($arrayReturn);
                    erro($result);
                }else{
                    erro("matricula [$matricula] inserido no no sistema!");
                }
            }
            $matricula = null;

It turns out that if an error occurs I jump to the next one. I'd like to change that.

I would like a number of attempts to be made and if I kept the error in:

if($arrayReturn['status'] == "ERROR" || $arrayReturn == false){
                    erro(" ERRO servidor faculdade para inserir matricula [$matricula]"); 

Then I would stop.

Is it possible?

    
asked by anonymous 19.11.2018 / 21:59

1 answer

2

In a "handy" way you can implement as follows:

$NUM_OF_ATTEMPTS = 5;
$attempts = 0;

while (($matricula = fgets($handle, 1024)) !== false) {
    $cont++;
    erro("Lendo matricula $cont de $qtde_matriculas");
    $matricula = trim($matricula);

    if(!is_numeric(trim(substr($matricula,0,5)))){
        erro("matricula $cont invalido [$matricula]");
    }else{
        do{
            $arrayReturn = buscarnafaculdade( $matricula );
            if($arrayReturn['status'] == "ERROR" || $arrayReturn == false){
                $attempts++;

                if ($attempts <= $NUM_OF_ATTEMPTS){
                    continue;
                }

                erro(" ERRO servidor faculdade para inserir matricula [$matricula]");
                $result = print_r($arrayReturn);
                erro($result);
            }else{
                erro("matricula [$matricula] inserido no no sistema!");
                $attempts = $NUM_OF_ATTEMPTS;
            }
        }while($attempts < $NUM_OF_ATTEMPTS);       

        $attempts = 0;
    }
}

$matricula = null;      

I did not test the code to see if there was any syntax error but the idea would be there.

    
19.11.2018 / 23:47