Use if/Else condition inside a while

Asked

Viewed 2,251 times

2

Good morning!! I’m in need of some help. In the code below, I submit a file .txt delimited by ";". However, within the While, in the array, I would like to include a condition, example:

while ( $linha = mysql_fetch_array ( $select ) ) { $conteudo = "

if{$_SESSION[nivelAcesso]==3{
$linha[sap];
$linha[descricao];
}
$linha[segmento]; 




    <?php

    header ( 'Content-type: text/plain' );
    header ( 'Content-Disposition: attachment; filename="EQUIPAMENTOS_DETALHADOS_' . $date . '.txt"' );

    $NOME_ARQUIVO = "EQUIPAMENTOS_DETALHADOS.txt";
    ?>
    <?php

    $select = mysql_query ( "SELECT materiais.sap,materiais.descricao,materiais.segmento,descricaoRegiao,estoque.serial, estoque.sim_card, estoque.cas_id ,DATE_FORMAT(estoque.data_cadastro,'%d/%m/%Y') as dataCadastro,Status_equipamento,estoque.notaFiscal from estoque inner join materiais on estoque.id_material=materiais.id inner join regioes_estoque on estoque.regional=regioes_estoque.regiao where materiais.almoxarifado='$_SESSION[almoxarifado]' and estoque.serial <> ' ' and estoque.serial not in (select serial from movimento)" ) or die ( mysql_error () );
    $saida = fopen ( "EQUIPAMENTOS_DETALHADOS.txt", "a+" );
    echo "COD.SAP;DESCRICAO;SEGMENTO;DESCRICAO_REIGAO;SERIAL;SIM_CARD;CAS_ID;DATA_CADASTRO;STATUS_EQUIPAMENTO;NOTA_FISCAL \n";
    while ( $linha = mysql_fetch_array ( $select ) ) {
        $conteudo = "$linha[sap];$linha[descricao];$linha[segmento];$linha[descricaoRegiao];$linha[serial];$linha[sim_card];$linha[cas_id];$linha[dataCadastro];$linha[Status_equipamento];$linha[notaFiscal]; \n";
        $result = fputs ( $saida, $conteudo );
        echo $conteudo;
    }
    ;
    fclose ( $saida );

    unlink ( $NOME_ARQUIVO );
    ?>
  • Tiago your code is very confused. Because there is PHP code inside the string $conteudo?

  • Jorge, The first example is what I would like to see if it is possible to do. I need to include a condition within the mysql_fetch_array, for when meeting this condition, it include two more information in the file .txt generated. I could understand?

  • James you can not by $conteudo = " and then putting code in, when you open the quotes you are starting a string, everything inside will be interpreted as such. You can concatenate string with a dot . thus: $conteudo = $conteudo. "algo mais a acrescentar"; or alternatively the simplified $conteudo .= "algo mais a acrescentar";. Try to learn a little more PHP and programming to understand these initial concepts.

1 answer

1


Just add the if within the cycle while and concatenate the data according to the result of the operation using the concatenation assignment operator .=.

To documentation PHP teaches you how to use builder if and also teaches how to use string operators.

while ( $linha = mysql_fetch_array ( $select ) ) {
   // utilizamos o operador "=" para atribuir um valor inicial a variável "conteudo"
   $conteudo = "$linha[sap];$linha[descricao];$linha[segmento];$linha[descricaoRegiao];$linha[serial];$linha[sim_card];$linha[cas_id];$linha[dataCadastro];$linha[Status_equipamento];$linha[notaFiscal];"

   // se o nível de acesso for igual a 3, 
   // então é concatenado uma string à variável "conteudo"
   if($_SESSION[nivelAcesso]==3) {
        $conteudo .= "$linha[sim_card];$linha[cas_id];";
   }

   // contatenamos novamente uma nova string à variável "conteudo"
   $conteudo .= '\n';

   $result = fputs ( $saida, $conteudo );
   echo $conteudo;
}

The concatenation assignment operator .= is the same thing as:

// ou seja, a variável é igual ao próprio valor
// concatenado com uma nova string.
$conteudo = 'Olá';
$conteudo = $conteudo . ' mundo';

echo $conteudo; // Olá mundo

Same result using concatenation assignment operator:

$conteudo = 'Olá';
$conteudo .= ' mundo';

echo $conteudo; // Olá mundo
  • Filipe, but the condition is only for $line information[sim_card]; $line[cas_id];. Being more right, I need it to include this information during white within the $content variable only when the $_SESSION[levelAccess]==3. I’m a little recent in PHP...

  • @Tiagomartins understood. I changed the answer, see if now solves the problem.

  • it worked here, it helped so much! Hug.

Browser other questions tagged

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